import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import react, { useImperativeHandle, useState } from "react";
import "@tachybase/schema";
import { InboxOutlined } from "@ant-design/icons";
import { Modal, Spin, Upload, message } from "antd";
import "lodash";
import { useTranslation } from "react-i18next";
import { useAPIClient } from "../../api-client/index.mjs";
import "../../schema-component/index.mjs";
import { useCollectionManager_deprecated as index_mjs_useCollectionManager_deprecated } from "../hooks/index.mjs";
import { useResourceActionContext as external_ResourceActionProvider_mjs_useResourceActionContext } from "../ResourceActionProvider.mjs";
const { Dragger } = Upload;
function useUploadProps(props) {
    const onChange = (param)=>{
        var _props_onChange;
        null == (_props_onChange = props.onChange) || _props_onChange.call(props, param);
    };
    const api = useAPIClient();
    return {
        ...props,
        customRequest (param) {
            let { action, data, file, filename, headers, onError, onProgress, onSuccess, withCredentials } = param;
            const formData = new FormData();
            if (data) Object.keys(data).forEach((key)=>{
                formData.append(key, data[key]);
            });
            formData.append(filename, file);
            api.axios.post(action, formData, {
                withCredentials,
                headers,
                onUploadProgress: (param)=>{
                    let { total, loaded } = param;
                    onProgress({
                        percent: Math.round(loaded / total * 100).toFixed(2)
                    }, file);
                }
            }).then((param)=>{
                let { data } = param;
                onSuccess(data, file);
            }).catch(onError).finally(()=>{});
            return {
                abort () {
                    console.log('upload progress is aborted.');
                }
            };
        },
        onChange
    };
}
const ImportUpload = (props)=>{
    const { t } = useTranslation();
    const { refreshCM } = index_mjs_useCollectionManager_deprecated();
    const { close } = props;
    const { refresh, state: { category } } = external_ResourceActionProvider_mjs_useResourceActionContext();
    const uploadProps = {
        multiple: false,
        action: '/collections:importMeta',
        data: (null == category ? void 0 : category[0]) && {
            category: category[0]
        } || {},
        async onChange (info) {
            if (info.fileList.length > 1) info.fileList.splice(0, info.fileList.length - 1);
            const { status } = info.file;
            if ('done' === status) {
                close();
                message.success("".concat(info.file.name, " ") + t('file uploaded successfully'));
                refresh();
                await refreshCM();
            } else if ('error' === status) message.error("".concat(info.file.name, " ") + t('file upload failed'));
        },
        onDrop (e) {
            console.log('Dropped files', e.dataTransfer.files);
        }
    };
    return /*#__PURE__*/ jsxs(Dragger, {
        ...useUploadProps(uploadProps),
        children: [
            /*#__PURE__*/ jsx("p", {
                className: "ant-upload-drag-icon",
                children: /*#__PURE__*/ jsx(InboxOutlined, {})
            }),
            /*#__PURE__*/ jsxs("p", {
                className: "ant-upload-text",
                children: [
                    " ",
                    t('Click or drag file to this area to upload')
                ]
            })
        ]
    });
};
const ImportCollectionMetaAction = /*#__PURE__*/ react.forwardRef((props, ref)=>{
    const { t } = useTranslation();
    const [dataTypes, setDataTypes] = useState([
        'required'
    ]);
    const [isModalOpen, setIsModalOpen] = useState(false);
    const [loading, setLoading] = useState(false);
    const showModal = ()=>{
        setIsModalOpen(true);
    };
    useImperativeHandle(ref, ()=>({
            showModal
        }));
    const handleCancel = ()=>{
        setIsModalOpen(false);
        setDataTypes([
            'required'
        ]);
    };
    return /*#__PURE__*/ jsx(Fragment, {
        children: /*#__PURE__*/ jsx(Modal, {
            title: t('Import'),
            width: 800,
            footer: void 0,
            open: isModalOpen,
            onOk: handleCancel,
            onCancel: handleCancel,
            children: /*#__PURE__*/ jsx(Spin, {
                spinning: loading,
                children: /*#__PURE__*/ jsx(ImportUpload, {
                    close: handleCancel
                })
            })
        })
    });
});
export { ImportCollectionMetaAction };
