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/index.tsx
var ExportImport_exports = {};
__export(ExportImport_exports, {
  default: () => ExportImport_default
});
module.exports = __toCommonJS(ExportImport_exports);
var import_ImportExport = __toESM(require("@pisell/icon/es/ImportExport"));
var import_ahooks = require("ahooks");
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_serve = require("../../../serve");
var import_status = require("../../../status");
var import_utils = require("../../../utils");
var import_ButtonIcon = __toESM(require("../ButtonIcon"));
var import_ExportFile = __toESM(require("./components/ExportFile"));
var import_ExportTable = __toESM(require("./components/ExportTable"));
var import_function = require("./components/ExportTable/function");
var import_ImportLog = __toESM(require("./components/ImportLog"));
var import_ImportTable = __toESM(require("./components/ImportTable"));
var import_hooks = require("../../../../../hooks");
var import_locale = require("../../../../../utils/locale");
var import_model = require("../../../model");
var import_index = require("./index.less");
var Com = {
  import: import_ImportTable.default,
  export: import_ExportTable.default,
  export_file: import_ExportFile.default,
  import_log: import_ImportLog.default
};
var ExportImport = ({ exportImport }) => {
  const [open, setOpen] = (0, import_react.useState)(false);
  const [file, setFile] = (0, import_react.useState)(null);
  const [type, setType] = (0, import_react.useState)(import_status.defaultOptions.exportType);
  const [value, setValue] = (0, import_react.useState)("");
  const [errorStatus, setErrorStatus] = (0, import_react.useState)();
  const [loading, setLoading] = (0, import_react.useState)(false);
  const { state } = (0, import_hooks.useSharedState)(import_model.Context);
  const { originResultDataSource, currentDataSource } = state;
  const context = (0, import_useEngineContext.default)();
  import_utils.request.setRequest(context.appHelper.utils.request);
  const _items = (0, import_react.useMemo)(() => {
    var _a;
    const result = (_a = Object.entries(import_status.exportImportOptions).map(([key, value2]) => {
      var _a2;
      if (Array.isArray(exportImport == null ? void 0 : exportImport.hiddenKeys) && ((_a2 = exportImport == null ? void 0 : exportImport.hiddenKeys) == null ? void 0 : _a2.includes(key))) {
        return;
      }
      return {
        key,
        label: value2
      };
    })) == null ? void 0 : _a.filter(Boolean);
    return result;
  }, [exportImport == null ? void 0 : exportImport.hiddenKeys]);
  const Content = (0, import_react.useMemo)(() => {
    if (!value) return null;
    return Com[value];
  }, [value]);
  const _footer = (0, import_react.useMemo)(() => {
    const types = import_status.defaultOptions.footerHintTypes;
    if (types.includes(value)) {
      return null;
    }
  }, [value]);
  const _width = (0, import_react.useMemo)(() => {
    const types = import_status.defaultOptions.footerHintTypes;
    return types.includes(value) ? 900 : import_status.defaultOptions.importValue ? 600 : 400;
  }, [value]);
  const _title = (0, import_react.useMemo)(() => {
    if (!value) return null;
    const types = import_status.defaultOptions.okShowButtonTypes;
    const lang = (0, import_locale.getCurrentLocale)();
    const isEn = lang === "en-US" || lang === "en";
    if (types.includes(value) && (exportImport == null ? void 0 : exportImport.title)) {
      return `${import_status.exportImportOptions[value]}${isEn ? " " : ""}${exportImport.title}`;
    }
    return import_status.exportImportOptions[value];
  }, [value, exportImport == null ? void 0 : exportImport.title]);
  const _okText = (0, import_react.useMemo)(() => {
    const types = import_status.defaultOptions.okShowButtonTypes;
    if (types.includes(value)) {
      return value === import_status.defaultOptions.exportValue ? (0, import_locales.getText)("table-action-export-import-button") : (0, import_locales.getText)("table-action-export-import-button-import");
    }
  }, [value]);
  const handleClick = (e) => {
    const value2 = e.key;
    setValue(value2);
    setOpen(true);
  };
  const onCancel = () => {
    setOpen(false);
    setValue("");
    setFile(null);
    setErrorStatus(null);
    setType(import_status.defaultOptions.exportType);
  };
  const _batchImport = (0, import_ahooks.useRequest)(import_serve.batchImport, {
    manual: true,
    debounceWait: 200,
    onSuccess: () => {
      setErrorStatus(null);
      import_antd.message.destroy();
      import_antd.message.success({
        content: (0, import_locales.getText)("table-action-export-import-import-success"),
        key: value
      });
      onCancel();
    },
    onError: (err) => {
      import_antd.message.destroy();
      setErrorStatus((err == null ? void 0 : err.message) || null);
    }
  });
  const onOk = async () => {
    const { apiUrl, otherParams } = exportImport;
    setLoading(true);
    try {
      if (value === import_status.defaultOptions.exportValue) {
        const _params = {
          type,
          originDataSource: originResultDataSource,
          currentDataSource
        };
        const result = await (0, import_function.batchExport)(_params, exportImport);
        if (result) {
          onCancel();
        }
      } else if (value === import_status.defaultOptions.importValue) {
        const formData = new FormData();
        formData.append("file", file);
        const formId = otherParams == null ? void 0 : otherParams.form_id;
        const formCode = otherParams == null ? void 0 : otherParams.code;
        if (formId) {
          formData.append("form_id", formId);
        }
        if (formCode) {
          formData.append("code", formCode);
        }
        formData.append("url", (apiUrl == null ? void 0 : apiUrl.import) || "");
        if (otherParams.import) {
          Object.entries(otherParams.import).forEach(([key, value2]) => {
            formData.append(key, value2);
          });
        }
        if (formData) {
          import_antd.message.loading({ content: "Loading...", key: value });
          await _batchImport.run(formData);
        }
      }
    } catch (error) {
    }
    setLoading(false);
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Dropdown,
    {
      menu: {
        items: _items,
        selectable: true,
        onClick: handleClick
      },
      placement: "bottomRight"
    },
    /* @__PURE__ */ import_react.default.createElement(import_ButtonIcon.default, { isActive: false }, /* @__PURE__ */ import_react.default.createElement(import_ImportExport.default, { style: { cursor: "pointer", fontSize: 20 } }))
  ), /* @__PURE__ */ import_react.default.createElement(
    import_antd.Modal,
    {
      title: _title,
      footer: _footer,
      width: _width,
      okText: _okText,
      open,
      className: "export-import-modal",
      onCancel,
      bodyStyle: { padding: "0px" },
      destroyOnClose: true,
      onOk,
      cancelText: (0, import_locales.getText)("table-action-export-import-cancel"),
      okButtonProps: {
        loading,
        disabled: value === import_status.defaultOptions.importValue && (!file || !!errorStatus)
      }
    },
    /* @__PURE__ */ import_react.default.createElement(
      Content,
      {
        exportImport,
        errorStatus,
        onChange: (e) => {
          if (value === import_status.defaultOptions.importValue) {
            setFile(e);
            setErrorStatus(null);
          } else if (value === import_status.defaultOptions.exportValue) {
            setType(e);
          }
        }
      }
    )
  ));
};
var ExportImport_default = ExportImport;
