var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import { jsx as _jsx } from "react/jsx-runtime";
import { Upload } from "antd";
import React, { useEffect, useState } from "react";
import { formatImageUrl, getToken, isImageUrl } from "@component/utils";
import axios from "axios";
import download from "downloadjs";
export default function PreviewFile(_a) {
    var { fileList } = _a, props = __rest(_a, ["fileList"]);
    const api = process.env.REACT_APP_API_URL;
    function onPreview(file) {
        return getFileUrl(api === null || api === void 0 ? void 0 : api.replace("/base", ""), `${file === null || file === void 0 ? void 0 : file.url}`).then((response) => {
            return response.data;
        }).then((blob) => {
            if (isImageUrl(file.name)) {
                const blobs = new Blob([blob], { type: "image/png" });
                const url = URL.createObjectURL(blobs);
                window.open(url, "_blank");
            }
            else {
                download(blob, file.name);
            }
        });
    }
    const [files, setFiles] = useState([]);
    useEffect(() => {
        Promise.all(fileList).then(res => {
            setFiles(res);
        }).catch(error => {
            console.log(error);
        });
    }, [fileList]);
    return (_jsx(Upload, { disabled: true, fileList: files, listType: "picture-card", onPreview: onPreview }));
}
//图片url请求接口并将文件流转为地址
function getFileUrl(api, path) {
    return axios.get(`${api}${path}`, {
        responseType: "blob",
        withCredentials: false,
        headers: {
            token: getToken()
        }
    });
}