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