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/booking/forms/forms.tsx
var forms_exports = {};
__export(forms_exports, {
  default: () => forms_default
});
module.exports = __toCommonJS(forms_exports);
var import_react = __toESM(require("react"));
var import_model = require("./model");
var import_single = __toESM(require("./single"));
var import_utils = require("@pisell/utils");
var import_format = require("./format");
var import_utils2 = require("../utils");
var import_index = require("./index.less");
var import_ahooks = require("ahooks");
var Forms = (0, import_model.Provider)((props) => {
  const {
    forms,
    value = [],
    modalOpen,
    onChange,
    action,
    customSelectProps,
    optionRender,
    hideTitle,
    addCallback,
    client,
    loading,
    addButtonProps,
    drawerProps,
    isMinorAndWaiver,
    waiverForm,
    addCallbackService,
    getHolders
  } = props;
  const onAction = (0, import_ahooks.useMemoizedFn)(
    async (formDataConfig, isAdd) => {
      let _data = {
        formDataConfig: isMinorAndWaiver ? { ...waiverForm, customer_id: client == null ? void 0 : client.id, is_franchisor: formDataConfig.is_franchisor } : { ...formDataConfig }
      };
      if (formDataConfig.data_range === "customer" && !(0, import_utils2.isWalkIn)(client == null ? void 0 : client.id)) {
        _data.customerId = client.id;
      }
      action({
        type: "pisell1.goCustomFormAddData",
        data: _data,
        callback: (res) => {
          if (isMinorAndWaiver) {
            addCallbackService == null ? void 0 : addCallbackService();
            getHolders == null ? void 0 : getHolders();
            return;
          }
          let obj = {};
          for (let item of res) {
            obj[item.key] = item.value;
          }
          let _val = [...value[formDataConfig.form_id] || []];
          _val.push(obj);
          if (isAdd) {
            onValueChange(formDataConfig, _val);
          }
          setTimeout(() => {
            addCallback == null ? void 0 : addCallback(formDataConfig, obj);
          }, 100);
        }
      });
    }
  );
  const onValueChange = (0, import_ahooks.useMemoizedFn)((form, val) => {
    onChange({
      values: {
        ...value,
        [form.form_id]: (0, import_format.formatAddFormValues)({
          clientId: client == null ? void 0 : client.id,
          value: val,
          data_range: form.data_range
        })
      },
      current: {
        field: form,
        value: val
      }
    });
  });
  const handleViewItem = (0, import_ahooks.useMemoizedFn)((props2) => {
    const { form, record } = props2;
    let _data = {
      record,
      formDataConfig: form
    };
    if (form.data_range === "customer" && !(0, import_utils2.isWalkIn)(client == null ? void 0 : client.id)) {
      _data.customerId = client.id;
    }
    action({
      type: "pisell1.goCustomFormDetail",
      data: _data,
      callback: (res) => {
        let cur = [...value[form.form_id] || []];
        if (res.type === "delete") {
          cur = cur.filter(
            (d) => d.form_record_id !== record.form_record_id
          );
        } else {
          let obj = {};
          for (let item of res) {
            obj[item.key] = item.value;
          }
          cur = cur.map(
            (d) => d.form_record_id === record.form_record_id ? obj : d
          );
        }
        onValueChange(form, cur);
      }
    });
  });
  return (0, import_react.useMemo)(() => {
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, (forms || []).map((d) => {
      var _a;
      return /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: "pisell-lowcode__booking-field-wrap",
          style: { padding: hideTitle ? 0 : "12px 0 0" },
          key: d.form_id
        },
        hideTitle ? null : /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__booking-field-title" }, d.form.title),
        /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__booking-field-com" }, /* @__PURE__ */ import_react.default.createElement(
          import_single.default,
          {
            modalOpen,
            form: d,
            watch: { value },
            mode: d.type,
            value: value[d.form_id] || [],
            onChange: (val) => {
              onValueChange(d, val);
            },
            drawerProps,
            onAdd: () => {
              onAction(d, true);
            },
            addButtonProps: {
              loading,
              // 如果表单是客户数据, 并且未选择客户  则禁止选择表单
              disabled: d.data_range === "customer" && (0, import_utils2.isWalkIn)(client == null ? void 0 : client.id),
              ...addButtonProps
            },
            customSelectProps: {
              handleViewItem: (record) => {
                handleViewItem({
                  form: d,
                  record
                });
              },
              handleCreateItem: () => {
                onAction(d, false);
              },
              createItemText: `${import_utils.locales.getText(
                "pisell2.text.create-new"
              )} ${((_a = d.form) == null ? void 0 : _a.title) || "-"}`,
              // addButtonProps: {
              // },
              ...customSelectProps || {}
            },
            optionRender
          }
        ))
      );
    }));
  }, [
    forms,
    hideTitle,
    modalOpen,
    value,
    loading,
    client == null ? void 0 : client.id,
    addButtonProps,
    customSelectProps,
    optionRender,
    drawerProps,
    addCallbackService
  ]);
});
var forms_default = Forms;
