"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    ImportCollectionMetaAction: ()=>ImportCollectionMetaAction
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
require("@tachybase/schema");
const icons_namespaceObject = require("@ant-design/icons");
const external_antd_namespaceObject = require("antd");
require("lodash");
const external_react_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../../api-client/index.js");
require("../../schema-component/index.js");
const external_hooks_index_js_namespaceObject = require("../hooks/index.js");
const external_ResourceActionProvider_js_namespaceObject = require("../ResourceActionProvider.js");
const { Dragger } = external_antd_namespaceObject.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, index_js_namespaceObject.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, external_react_i18next_namespaceObject.useTranslation)();
    const { refreshCM } = (0, external_hooks_index_js_namespaceObject.useCollectionManager_deprecated)();
    const { close } = props;
    const { refresh, state: { category } } = (0, external_ResourceActionProvider_js_namespaceObject.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();
                external_antd_namespaceObject.message.success("".concat(info.file.name, " ") + t('file uploaded successfully'));
                refresh();
                await refreshCM();
            } else if ('error' === status) external_antd_namespaceObject.message.error("".concat(info.file.name, " ") + t('file upload failed'));
        },
        onDrop (e) {
            console.log('Dropped files', e.dataTransfer.files);
        }
    };
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(Dragger, {
        ...useUploadProps(uploadProps),
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("p", {
                className: "ant-upload-drag-icon",
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.InboxOutlined, {})
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("p", {
                className: "ant-upload-text",
                children: [
                    " ",
                    t('Click or drag file to this area to upload')
                ]
            })
        ]
    });
};
const ImportCollectionMetaAction = /*#__PURE__*/ external_react_default().forwardRef((props, ref)=>{
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const [dataTypes, setDataTypes] = (0, external_react_namespaceObject.useState)([
        'required'
    ]);
    const [isModalOpen, setIsModalOpen] = (0, external_react_namespaceObject.useState)(false);
    const [loading, setLoading] = (0, external_react_namespaceObject.useState)(false);
    const showModal = ()=>{
        setIsModalOpen(true);
    };
    (0, external_react_namespaceObject.useImperativeHandle)(ref, ()=>({
            showModal
        }));
    const handleCancel = ()=>{
        setIsModalOpen(false);
        setDataTypes([
            'required'
        ]);
    };
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(jsx_runtime_namespaceObject.Fragment, {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Modal, {
            title: t('Import'),
            width: 800,
            footer: void 0,
            open: isModalOpen,
            onOk: handleCancel,
            onCancel: handleCancel,
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Spin, {
                spinning: loading,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ImportUpload, {
                    close: handleCancel
                })
            })
        })
    });
});
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
