const require_runtime = require("../../../../../../../_virtual/_rolldown/runtime.js");
const require_index = require("../../../../../../../locales/index.js");
const require_objectSpread2 = require("../../../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_index$1 = require("../../../../../../iconfont/index.js");
const require_useEngineContext = require("../../../../../../../hooks/useEngineContext.js");
const require_asyncToGenerator = require("../../../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_utils = require("../../../../../utils.js");
const require_serve = require("../../../../../serve.js");
const require_status = require("../../../../../status.js");
const require_download = require("../../utils/download.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _ant_design_icons = require("@ant-design/icons");
require("./index.less");
//#region src/components/table/Actions/component/ExportImport/components/ImportTable/index.tsx
const loadingKey = require_status.defaultOptions.importValue;
const ImportTable = (props) => {
	const { onChange, exportImport, errorStatus } = props;
	const { templateFileUrl, apiUrl, otherParams } = exportImport;
	const [file, setFile] = (0, react.useState)(null);
	const context = require_useEngineContext.default();
	require_utils.request.setRequest(context.appHelper.utils.request);
	const handleDownload = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			if (templateFileUrl) require_download.default(templateFileUrl);
			else {
				antd.message.loading({
					content: "Loading...",
					key: loadingKey
				});
				let { data } = yield require_serve.importTemplate(require_objectSpread2._objectSpread2({ url: apiUrl === null || apiUrl === void 0 ? void 0 : apiUrl.template }, otherParams));
				require_download.default(data === null || data === void 0 ? void 0 : data.url);
			}
			antd.message.success({
				content: require_index.getText("table-action-export-import-table-success-download"),
				key: loadingKey
			});
		});
		return function handleDownload() {
			return _ref.apply(this, arguments);
		};
	}();
	const fileType = (0, react.useMemo)(() => {
		if (file) return file.type.indexOf("/csv") !== -1 ? "csv" : "excel";
		return "";
	}, [file]);
	const fileName = (0, react.useMemo)(() => {
		if (file) return file.name;
		return "";
	}, [file]);
	const _handleSelectFile = (e) => {
		var _e$target;
		let _file = (e === null || e === void 0 || (_e$target = e.target) === null || _e$target === void 0 ? void 0 : _e$target.files) ? e.target.files[0] : null;
		if (_file) {
			if (_file.name.indexOf(".csv") === -1 && _file.name.indexOf(".xls") === -1 && _file.name.indexOf(".xlsx") === -1) return antd.message.error(require_index.getText("table-action-export-import-table-error-file"));
			onChange(_file);
			setFile(_file);
		}
	};
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, errorStatus ? /* @__PURE__ */ react.default.createElement("div", { className: "error-hint" }, /* @__PURE__ */ react.default.createElement(require_index$1.default, {
		type: "icontixingshixin",
		className: "error-icon"
	}), /* @__PURE__ */ react.default.createElement("div", { className: "text" }, errorStatus)) : null, /* @__PURE__ */ react.default.createElement("div", { className: "import-title" }, require_index.getText("table-action-export-import-table-text-click-download"), /* @__PURE__ */ react.default.createElement("a", { onClick: handleDownload }, require_index.getText("table-action-export-import-table-text-product-template"))), file ? /* @__PURE__ */ react.default.createElement("div", { className: "file-list" }, /* @__PURE__ */ react.default.createElement(require_index$1.default, {
		type: fileType === "csv" ? "icon-csv" : "icon-excel",
		className: "file-icon"
	}), /* @__PURE__ */ react.default.createElement("div", { className: "file-name" }, fileName), /* @__PURE__ */ react.default.createElement(antd.Button, null, require_index.getText("table-action-export-import-table-text-edit-file"), /* @__PURE__ */ react.default.createElement("input", {
		accept: ".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel",
		className: "upload-input",
		type: "file",
		onChange: _handleSelectFile,
		value: ""
	}))) : /* @__PURE__ */ react.default.createElement("div", { className: "default-view" }, /* @__PURE__ */ react.default.createElement("div", { className: "icon-wrap" }, /* @__PURE__ */ react.default.createElement(_ant_design_icons.InboxOutlined, null)), /* @__PURE__ */ react.default.createElement("p", { className: "text" }, require_index.getText("table-action-export-import-table-text-input-title")), /* @__PURE__ */ react.default.createElement("p", { className: "hint" }, require_index.getText("table-action-export-import-table-text-input-describe")), /* @__PURE__ */ react.default.createElement("input", {
		accept: ".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel",
		className: "upload-input",
		type: "file",
		onChange: _handleSelectFile,
		value: ""
	})));
};
//#endregion
exports.default = ImportTable;
