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/walletList/tempHolderSelector/index.tsx
var tempHolderSelector_exports = {};
__export(tempHolderSelector_exports, {
  default: () => TempHolderSelector,
  getAvatarText: () => getAvatarText
});
module.exports = __toCommonJS(tempHolderSelector_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_materials = require("@pisell/materials");
var import_ahooks = require("ahooks");
var import_materials2 = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_serve = require("../serve");
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var import_index = require("./index.less");
var ACol = import_antd.Col;
var ARow = import_antd.Row;
var getAvatarText = (name) => {
  if (!name) return "";
  const words = name.trim().split(/\s+/);
  const initials = words.slice(0, 2).map((word) => word.charAt(0).toUpperCase()).join("");
  return initials;
};
function TempHolderSelector(props) {
  var _a;
  const [value, setValue] = (0, import_react.useState)(props.value);
  const { visible, onCancel, onConfirm, confirmLoading } = props;
  const context = (0, import_useEngineContext.default)();
  const _getHolderList = (0, import_ahooks.useRequest)(import_serve.getHolderList, {
    defaultParams: [
      {
        skip: 1,
        num: 100,
        form_id: props.formId,
        customer_id: props.customerId
      }
    ],
    onSuccess: (res) => {
      if (Array.isArray(res.list) && res.list.length > 0) {
        setValue(res.list[0].form_record_id);
      }
    }
  });
  const onAddHolder = async () => {
    var _a2, _b, _c, _d, _e, _f;
    let _formForFilling = props.formId;
    const { data: formConfig } = await (0, import_serve.getFormConfig)(props.formId);
    if (((_a2 = formConfig == null ? void 0 : formConfig.form) == null ? void 0 : _a2.form_type) === "minors" && Array.isArray(formConfig == null ? void 0 : formConfig.field)) {
      const detail = (_b = formConfig == null ? void 0 : formConfig.field) == null ? void 0 : _b.find(
        (item) => item.filed_type === "form_to_form" && item.status === "hide"
      );
      if (detail) {
        _formForFilling = (_c = detail == null ? void 0 : detail.setting) == null ? void 0 : _c.linkedTable;
      }
    }
    (_f = (_e = (_d = context == null ? void 0 : context.appHelper) == null ? void 0 : _d.utils) == null ? void 0 : _e.action) == null ? void 0 : _f.call(_e, {
      type: "pisell1.form",
      data: {
        form_id: _formForFilling,
        form_title: import_utils.locales.getText("page.wallet.tempHolderSelector.create"),
        isV2API: true
      },
      callback: (res) => {
        if ((res == null ? void 0 : res.status) === "success") {
          if (_formForFilling !== props.formId) {
            _getHolderList.refresh();
          } else {
            _getHolderList.mutate((prev) => ({
              ...prev,
              list: [res.values, ...prev.list]
            }));
            setValue(res.values.form_record_id);
          }
        }
      }
    });
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_materials2.PisellModal,
    {
      title: import_utils.locales.getText("page.wallet.tempHolderSelector.title"),
      platform: (0, import_utils.isMobile)() ? "h5" : "pc",
      visible,
      onClose: onCancel,
      onOk: () => {
      },
      onCancel,
      footer: /* @__PURE__ */ import_react.default.createElement(
        import_antd.Button,
        {
          type: "primary",
          block: true,
          onClick: () => {
            onConfirm(value);
          },
          disabled: !value,
          loading: confirmLoading
        },
        import_utils.locales.getText("page.wallet.tempHolderSelector.confirm")
      ),
      width: 640,
      bodyStyle: { padding: 0 },
      headerDivider: false,
      destroyOnClose: true,
      className: "temp-holder-selector-modal"
    },
    /* @__PURE__ */ import_react.default.createElement(import_materials.Spin, { spinning: _getHolderList.loading }, /* @__PURE__ */ import_react.default.createElement("div", { className: "temp-holder-selector-content" }, /* @__PURE__ */ import_react.default.createElement(ARow, { gutter: [16, 16] }, /* @__PURE__ */ import_react.default.createElement(ACol, { span: 8, xs: { span: 12 }, sm: { span: 8 } }, /* @__PURE__ */ import_react.default.createElement("div", { className: "temp-holder-selector-block-wrapper" }, /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: "temp-holder-selector-block temp-holder-selector-block-create",
        onClick: onAddHolder
      },
      /* @__PURE__ */ import_react.default.createElement("div", { className: "temp-holder-selector-block-avatar" }, /* @__PURE__ */ import_react.default.createElement(import_materials.PisellAvatar, { useTextAvatar: true, size: "xxl" }, /* @__PURE__ */ import_react.default.createElement(import_materials.Icon, { type: "pisell2-plus", style: { fontSize: "24px" } }))),
      /* @__PURE__ */ import_react.default.createElement("div", { className: "temp-holder-selector-block-name" }, import_utils.locales.getText("page.wallet.tempHolderSelector.create"))
    ))), Array.isArray((_a = _getHolderList.data) == null ? void 0 : _a.list) && _getHolderList.data.list.map((item) => /* @__PURE__ */ import_react.default.createElement(ACol, { span: 8, xs: { span: 12 }, sm: { span: 8 }, key: item.id }, /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: `temp-holder-selector-block-wrapper ${value === item.form_record_id ? "temp-holder-selector-block-wrapper-active" : ""}`
      },
      /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: "temp-holder-selector-block",
          onClick: () => {
            setValue(item.form_record_id);
          }
        },
        /* @__PURE__ */ import_react.default.createElement("div", { className: "temp-holder-selector-block-avatar" }, /* @__PURE__ */ import_react.default.createElement(import_materials.PisellAvatar, { useTextAvatar: true, size: "xxl" }, getAvatarText(item.main_field))),
        /* @__PURE__ */ import_react.default.createElement("div", { className: "temp-holder-selector-block-name" }, item.main_field)
      )
    ))))))
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getAvatarText
});
