var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/table/Actions/component/ExportImport/components/ImportTable/index.tsx
var ImportTable_exports = {};
__export(ImportTable_exports, {
  default: () => ImportTable_default
});
module.exports = __toCommonJS(ImportTable_exports);
var import_icons = require("@ant-design/icons");
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_useEngineContext = __toESM(require("../../../../../../../hooks/useEngineContext"));
var import_locales = require("../../../../../../../locales/index");
var import_iconfont = __toESM(require("../../../../../../iconfont"));
var import_serve = require("../../../../../serve");
var import_status = require("../../../../../status");
var import_utils = require("../../../../../utils");
var import_download = __toESM(require("../../utils/download"));
var import_index = require("./index.less");
var loadingKey = import_status.defaultOptions.importValue;
var ImportTable = (props) => {
  const { onChange, exportImport, errorStatus } = props;
  const { templateFileUrl, apiUrl, otherParams } = exportImport;
  const [file, setFile] = (0, import_react.useState)(null);
  const context = (0, import_useEngineContext.default)();
  import_utils.request.setRequest(context.appHelper.utils.request);
  const handleDownload = async () => {
    if (templateFileUrl) {
      (0, import_download.default)(templateFileUrl);
    } else {
      import_antd.message.loading({ content: "Loading...", key: loadingKey });
      const params = {
        url: apiUrl == null ? void 0 : apiUrl.template,
        ...otherParams
      };
      let { data } = await (0, import_serve.importTemplate)(params);
      (0, import_download.default)(data == null ? void 0 : data.url);
    }
    import_antd.message.success({
      content: (0, import_locales.getText)("table-action-export-import-table-success-download"),
      key: loadingKey
    });
  };
  const fileType = (0, import_react.useMemo)(() => {
    if (file) {
      return file.type.indexOf("/csv") !== -1 ? "csv" : "excel";
    }
    return "";
  }, [file]);
  const fileName = (0, import_react.useMemo)(() => {
    if (file) {
      return file.name;
    }
    return "";
  }, [file]);
  const _handleSelectFile = (e) => {
    var _a;
    let _file = ((_a = e == null ? void 0 : e.target) == null ? void 0 : _a.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 import_antd.message.error(
          (0, import_locales.getText)("table-action-export-import-table-error-file")
        );
      }
      onChange(_file);
      setFile(_file);
    }
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, errorStatus ? /* @__PURE__ */ import_react.default.createElement("div", { className: "error-hint" }, /* @__PURE__ */ import_react.default.createElement(import_iconfont.default, { type: "icontixingshixin", className: "error-icon" }), /* @__PURE__ */ import_react.default.createElement("div", { className: "text" }, errorStatus)) : null, /* @__PURE__ */ import_react.default.createElement("div", { className: "import-title" }, (0, import_locales.getText)("table-action-export-import-table-text-click-download"), /* @__PURE__ */ import_react.default.createElement("a", { onClick: handleDownload }, (0, import_locales.getText)("table-action-export-import-table-text-product-template"))), file ? /* @__PURE__ */ import_react.default.createElement("div", { className: "file-list" }, /* @__PURE__ */ import_react.default.createElement(
    import_iconfont.default,
    {
      type: fileType === "csv" ? "icon-csv" : "icon-excel",
      className: "file-icon"
    }
  ), /* @__PURE__ */ import_react.default.createElement("div", { className: "file-name" }, fileName), /* @__PURE__ */ import_react.default.createElement(import_antd.Button, null, (0, import_locales.getText)("table-action-export-import-table-text-edit-file"), /* @__PURE__ */ import_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__ */ import_react.default.createElement("div", { className: "default-view" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "icon-wrap" }, /* @__PURE__ */ import_react.default.createElement(import_icons.InboxOutlined, null)), /* @__PURE__ */ import_react.default.createElement("p", { className: "text" }, (0, import_locales.getText)("table-action-export-import-table-text-input-title")), /* @__PURE__ */ import_react.default.createElement("p", { className: "hint" }, (0, import_locales.getText)("table-action-export-import-table-text-input-describe")), /* @__PURE__ */ import_react.default.createElement(
    "input",
    {
      accept: ".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel",
      className: "upload-input",
      type: "file",
      onChange: _handleSelectFile,
      value: ""
    }
  )));
};
var ImportTable_default = ImportTable;
