const require_runtime = require("../../../../../_virtual/_rolldown/runtime.js");
const require_locale = require("../../../../../utils/locale.js");
const require_index = require("../../../../../locales/index.js");
const require_useSharedState = require("../../../../../hooks/useSharedState.js");
const require_useEngineContext = require("../../../../../hooks/useEngineContext.js");
const require_asyncToGenerator = require("../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_model = require("../../../model.js");
const require_utils = require("../../../utils.js");
const require_index$1 = require("../ButtonIcon/index.js");
const require_serve = require("../../../serve.js");
const require_status = require("../../../status.js");
const require_index$2 = require("./components/ExportFile/index.js");
const require_index$3 = require("./components/ExportTable/index.js");
const require_function = require("./components/ExportTable/function.js");
const require_index$4 = require("./components/ImportLog/index.js");
const require_index$5 = require("./components/ImportTable/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let ahooks = require("ahooks");
let _pisell_icon_es_ImportExport = require("@pisell/icon/es/ImportExport");
_pisell_icon_es_ImportExport = require_runtime.__toESM(_pisell_icon_es_ImportExport);
require("./index.less");
//#region src/components/table/Actions/component/ExportImport/index.tsx
const Com = {
	import: require_index$5.default,
	export: require_index$3.default,
	export_file: require_index$2.default,
	import_log: require_index$4.default
};
const ExportImport = ({ exportImport }) => {
	const [open, setOpen] = (0, react.useState)(false);
	const [file, setFile] = (0, react.useState)(null);
	const [type, setType] = (0, react.useState)(require_status.defaultOptions.exportType);
	const [value, setValue] = (0, react.useState)("");
	const [errorStatus, setErrorStatus] = (0, react.useState)();
	const [loading, setLoading] = (0, react.useState)(false);
	const { state } = require_useSharedState.default(require_model.Context);
	const { originResultDataSource, currentDataSource } = state;
	const context = require_useEngineContext.default();
	require_utils.request.setRequest(context.appHelper.utils.request);
	const _items = (0, react.useMemo)(() => {
		var _Object$entries$map;
		return (_Object$entries$map = Object.entries(require_status.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 = (0, react.useMemo)(() => {
		return Com[value || require_status.defaultOptions.importValue];
	}, [value, require_status.defaultOptions.importValue]);
	const _footer = (0, react.useMemo)(() => {
		if (require_status.defaultOptions.footerHintTypes.includes(value)) return null;
	}, [value]);
	const _width = (0, react.useMemo)(() => {
		return require_status.defaultOptions.footerHintTypes.includes(value) ? 900 : require_status.defaultOptions.importValue ? 600 : 400;
	}, [value]);
	const _title = (0, react.useMemo)(() => {
		if (!value) return null;
		const types = require_status.defaultOptions.okShowButtonTypes;
		const lang = require_locale.getCurrentLocale();
		const isEn = lang === "en-US" || lang === "en";
		if (types.includes(value) && (exportImport === null || exportImport === void 0 ? void 0 : exportImport.title)) return `${require_status.exportImportOptions[value]}${isEn ? " " : ""}${exportImport.title}`;
		return require_status.exportImportOptions[value];
	}, [value, exportImport === null || exportImport === void 0 ? void 0 : exportImport.title]);
	const _okText = (0, react.useMemo)(() => {
		if (require_status.defaultOptions.okShowButtonTypes.includes(value)) return value === require_status.defaultOptions.exportValue ? require_index.getText("table-action-export-import-button") : require_index.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(require_status.defaultOptions.exportType);
	};
	const _batchImport = (0, ahooks.useRequest)(require_serve.batchImport, {
		manual: true,
		debounceWait: 200,
		onSuccess: () => {
			setErrorStatus(null);
			antd.message.destroy();
			antd.message.success({
				content: require_index.getText("table-action-export-import-import-success"),
				key: value
			});
			onCancel();
		},
		onError: (err) => {
			antd.message.destroy();
			setErrorStatus((err === null || err === void 0 ? void 0 : err.message) || null);
		}
	});
	const onOk = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			const { apiUrl, otherParams } = exportImport;
			setLoading(true);
			try {
				if (value === require_status.defaultOptions.exportValue) {
					if (yield require_function.batchExport({
						type,
						originDataSource: originResultDataSource,
						currentDataSource
					}, exportImport)) onCancel();
				} else if (value === require_status.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) {
						antd.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.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(antd.Dropdown, {
		menu: {
			items: _items,
			selectable: true,
			onClick: handleClick
		},
		placement: "bottomRight"
	}, /* @__PURE__ */ react.default.createElement(require_index$1.default, { isActive: false }, /* @__PURE__ */ react.default.createElement(_pisell_icon_es_ImportExport.default, { style: {
		cursor: "pointer",
		fontSize: 20
	} }))), /* @__PURE__ */ react.default.createElement(antd.Modal, {
		title: _title,
		footer: _footer,
		width: _width,
		okText: _okText,
		open,
		className: "export-import-modal",
		onCancel,
		bodyStyle: { padding: "0px" },
		destroyOnClose: true,
		onOk,
		cancelText: require_index.getText("table-action-export-import-cancel"),
		okButtonProps: {
			loading,
			disabled: value === require_status.defaultOptions.importValue && (!file || !!errorStatus)
		}
	}, /* @__PURE__ */ react.default.createElement(Content, {
		exportImport,
		errorStatus,
		onChange: (e) => {
			if (value === require_status.defaultOptions.importValue) {
				setFile(e);
				setErrorStatus(null);
			} else if (value === require_status.defaultOptions.exportValue) setType(e);
		}
	})));
};
//#endregion
exports.default = ExportImport;
