import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__ant_design_icons__ from "@ant-design/icons";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import "lodash";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../../api-client/index.mjs";
import "../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__ResourceActionProvider_mjs__ from "../ResourceActionProvider.mjs";
const { Dragger } = __WEBPACK_EXTERNAL_MODULE_antd__.Upload;
function useUploadProps(props) {
    const onChange = (param)=>{
        var _props_onChange;
        null === (_props_onChange = props.onChange) || void 0 === _props_onChange || _props_onChange.call(props, param);
    };
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.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 } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { refreshCM } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCollectionManager_deprecated)();
    const { close } = props;
    const { refresh, state: { category } } = (0, __WEBPACK_EXTERNAL_MODULE__ResourceActionProvider_mjs__.useResourceActionContext)();
    const uploadProps = {
        multiple: false,
        action: '/collections:importMeta',
        data: {
            category: (null == category ? void 0 : category[0]) || void 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();
                __WEBPACK_EXTERNAL_MODULE_antd__.message.success("".concat(info.file.name, " ") + t('file uploaded successfully'));
                refresh();
                await refreshCM();
            } else if ('error' === status) __WEBPACK_EXTERNAL_MODULE_antd__.message.error("".concat(info.file.name, " ") + t('file upload failed'));
        },
        onDrop (e) {
            console.log('Dropped files', e.dataTransfer.files);
        }
    };
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(Dragger, {
        ...useUploadProps(uploadProps),
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("p", {
                className: "ant-upload-drag-icon",
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.InboxOutlined, {})
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("p", {
                className: "ant-upload-text",
                children: [
                    " ",
                    t('Click or drag file to this area to upload')
                ]
            })
        ]
    });
};
const ImportCollectionMetaAction = /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].forwardRef((props, ref)=>{
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const [dataTypes, setDataTypes] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([
        'required'
    ]);
    const [isModalOpen, setIsModalOpen] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const [loading, setLoading] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const showModal = ()=>{
        setIsModalOpen(true);
    };
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useImperativeHandle)(ref, ()=>({
            showModal
        }));
    const handleCancel = ()=>{
        setIsModalOpen(false);
        setDataTypes([
            'required'
        ]);
    };
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Modal, {
            title: t('Import'),
            width: 800,
            footer: void 0,
            open: isModalOpen,
            onOk: handleCancel,
            onCancel: handleCancel,
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Spin, {
                spinning: loading,
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(ImportUpload, {
                    close: handleCancel
                })
            })
        })
    });
});
export { ImportCollectionMetaAction };
