import { getCurrentLocale } from "../../../../../utils/locale.js";
import { getText } from "../../../../../locales/index.js";
import useSharedState from "../../../../../hooks/useSharedState.js";
import useEngineContext from "../../../../../hooks/useEngineContext.js";
import { _asyncToGenerator } from "../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { Context } from "../../../model.js";
import { request } from "../../../utils.js";
import ButtonIcon from "../ButtonIcon/index.js";
import { batchImport } from "../../../serve.js";
import { defaultOptions, exportImportOptions } from "../../../status.js";
import ExportFile from "./components/ExportFile/index.js";
import ExportTable from "./components/ExportTable/index.js";
import { batchExport } from "./components/ExportTable/function.js";
import ImportLog from "./components/ImportLog/index.js";
import ImportTable from "./components/ImportTable/index.js";
import React, { useMemo, useState } from "react";
import { Dropdown, Modal, message } from "antd";
import { useRequest } from "ahooks";
import ImportExport from "@pisell/icon/es/ImportExport";
import "./index.less";
//#region src/components/table/Actions/component/ExportImport/index.tsx
const Com = {
	import: ImportTable,
	export: ExportTable,
	export_file: ExportFile,
	import_log: ImportLog
};
const ExportImport = ({ exportImport }) => {
	const [open, setOpen] = useState(false);
	const [file, setFile] = useState(null);
	const [type, setType] = useState(defaultOptions.exportType);
	const [value, setValue] = useState("");
	const [errorStatus, setErrorStatus] = useState();
	const [loading, setLoading] = useState(false);
	const { state } = useSharedState(Context);
	const { originResultDataSource, currentDataSource } = state;
	const context = useEngineContext();
	request.setRequest(context.appHelper.utils.request);
	const _items = useMemo(() => {
		var _Object$entries$map;
		return (_Object$entries$map = Object.entries(exportImportOptions).map(([key, value]) => {
			var _exportImport$hiddenK;
			if (Array.isArray(exportImport === null || exportImport === void 0 ? void 0 : exportImport.hiddenKeys) && (exportImport === null || exportImport === void 0 || (_exportImport$hiddenK = exportImport.hiddenKeys) === null || _exportImport$hiddenK === void 0 ? void 0 : _exportImport$hiddenK.includes(key))) return;
			return {
				key,
				label: value
			};
		})) === null || _Object$entries$map === void 0 ? void 0 : _Object$entries$map.filter(Boolean);
	}, [exportImport === null || exportImport === void 0 ? void 0 : exportImport.hiddenKeys]);
	const Content = useMemo(() => {
		return Com[value || defaultOptions.importValue];
	}, [value, defaultOptions.importValue]);
	const _footer = useMemo(() => {
		if (defaultOptions.footerHintTypes.includes(value)) return null;
	}, [value]);
	const _width = useMemo(() => {
		return defaultOptions.footerHintTypes.includes(value) ? 900 : defaultOptions.importValue ? 600 : 400;
	}, [value]);
	const _title = useMemo(() => {
		if (!value) return null;
		const types = defaultOptions.okShowButtonTypes;
		const lang = getCurrentLocale();
		const isEn = lang === "en-US" || lang === "en";
		if (types.includes(value) && (exportImport === null || exportImport === void 0 ? void 0 : exportImport.title)) return `${exportImportOptions[value]}${isEn ? " " : ""}${exportImport.title}`;
		return exportImportOptions[value];
	}, [value, exportImport === null || exportImport === void 0 ? void 0 : exportImport.title]);
	const _okText = useMemo(() => {
		if (defaultOptions.okShowButtonTypes.includes(value)) return value === defaultOptions.exportValue ? getText("table-action-export-import-button") : getText("table-action-export-import-button-import");
	}, [value]);
	const handleClick = (e) => {
		const value = e.key;
		setValue(value);
		setOpen(true);
	};
	const onCancel = () => {
		setOpen(false);
		setValue("");
		setFile(null);
		setErrorStatus(null);
		setType(defaultOptions.exportType);
	};
	const _batchImport = useRequest(batchImport, {
		manual: true,
		debounceWait: 200,
		onSuccess: () => {
			setErrorStatus(null);
			message.destroy();
			message.success({
				content: getText("table-action-export-import-import-success"),
				key: value
			});
			onCancel();
		},
		onError: (err) => {
			message.destroy();
			setErrorStatus((err === null || err === void 0 ? void 0 : err.message) || null);
		}
	});
	const onOk = function() {
		var _ref = _asyncToGenerator(function* () {
			const { apiUrl, otherParams } = exportImport;
			setLoading(true);
			try {
				if (value === defaultOptions.exportValue) {
					if (yield batchExport({
						type,
						originDataSource: originResultDataSource,
						currentDataSource
					}, exportImport)) onCancel();
				} else if (value === defaultOptions.importValue) {
					const formData = new FormData();
					formData.append("file", file);
					const formId = otherParams === null || otherParams === void 0 ? void 0 : otherParams.form_id;
					const formCode = otherParams === null || otherParams === void 0 ? void 0 : otherParams.code;
					if (formId) formData.append("form_id", formId);
					if (formCode) formData.append("code", formCode);
					formData.append("url", (apiUrl === null || apiUrl === void 0 ? void 0 : apiUrl.import) || "");
					if (otherParams.import) Object.entries(otherParams.import).forEach(([key, value]) => {
						formData.append(key, value);
					});
					if (formData) {
						message.loading({
							content: "Loading...",
							key: value
						});
						yield _batchImport.run(formData);
					}
				}
			} catch (error) {}
			setLoading(false);
		});
		return function onOk() {
			return _ref.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Dropdown, {
		menu: {
			items: _items,
			selectable: true,
			onClick: handleClick
		},
		placement: "bottomRight"
	}, /* @__PURE__ */ React.createElement(ButtonIcon, { isActive: false }, /* @__PURE__ */ React.createElement(ImportExport, { style: {
		cursor: "pointer",
		fontSize: 20
	} }))), /* @__PURE__ */ React.createElement(Modal, {
		title: _title,
		footer: _footer,
		width: _width,
		okText: _okText,
		open,
		className: "export-import-modal",
		onCancel,
		bodyStyle: { padding: "0px" },
		destroyOnClose: true,
		onOk,
		cancelText: getText("table-action-export-import-cancel"),
		okButtonProps: {
			loading,
			disabled: value === defaultOptions.importValue && (!file || !!errorStatus)
		}
	}, /* @__PURE__ */ React.createElement(Content, {
		exportImport,
		errorStatus,
		onChange: (e) => {
			if (value === defaultOptions.importValue) {
				setFile(e);
				setErrorStatus(null);
			} else if (value === defaultOptions.exportValue) setType(e);
		}
	})));
};
//#endregion
export { ExportImport as default };
