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/info/hooks/useAddHolder.ts
var useAddHolder_exports = {};
__export(useAddHolder_exports, {
  default: () => useAddHolder_default,
  useFormDataConfig: () => useFormDataConfig
});
module.exports = __toCommonJS(useAddHolder_exports);
var import_utils = require("../../utils");
var import_react = require("react");
var import_serve = require("../pet/serve");
var import_useGetHolderList = __toESM(require("./useGetHolderList"));
var import_utils2 = require("../service2/utils");
var useFormDataConfig = (state) => {
  var _a;
  const formDataConfig = (0, import_react.useMemo)(() => {
    var _a2, _b, _c;
    let _holder = (_a2 = state == null ? void 0 : state.bookingConfig) == null ? void 0 : _a2.holder;
    if (!_holder) {
      return null;
    }
    return {
      ..._holder,
      ..._holder == null ? void 0 : _holder.form,
      options: [],
      form_id: (_b = _holder == null ? void 0 : _holder.form) == null ? void 0 : _b.id,
      field: (_c = _holder == null ? void 0 : _holder.form) == null ? void 0 : _c.form_field,
      relation_type: "appointment_booking",
      form_record: []
    };
  }, [(_a = state == null ? void 0 : state.bookingConfig) == null ? void 0 : _a.holder]);
  const isMinorAndWaiver = (0, import_react.useMemo)(() => {
    var _a2;
    if ((formDataConfig == null ? void 0 : formDataConfig.form_type) === "minors") {
      const detail = formDataConfig == null ? void 0 : formDataConfig.form_field.find((item) => item.filed_type === "form_to_form" && item.status === "hide");
      if (detail) {
        return (_a2 = detail == null ? void 0 : detail.setting) == null ? void 0 : _a2.linkedTable;
      }
      return false;
    }
    return false;
  }, [formDataConfig]);
  return {
    formDataConfig,
    isMinorAndWaiver
  };
};
var useAddHolder = (state, dispatch) => {
  const [waiverForm, setWaiverForm] = (0, import_react.useState)(null);
  const getHolders = (0, import_useGetHolderList.default)(state, dispatch);
  const { formDataConfig, isMinorAndWaiver } = useFormDataConfig(state);
  (0, import_react.useEffect)(() => {
    if (isMinorAndWaiver) {
      (0, import_serve.getFormDetail)(isMinorAndWaiver).then((res) => {
        setWaiverForm(res);
        dispatch({
          type: "setPet",
          payload: {
            waiverForm: res
          }
        });
      });
    }
  }, [isMinorAndWaiver]);
  const handleAddHolder = (products, currentItem) => {
    var _a, _b, _c, _d, _e;
    let _data = {
      formDataConfig: isMinorAndWaiver ? { ...waiverForm, customer_id: (_b = (_a = state.client) == null ? void 0 : _a.value) == null ? void 0 : _b.id, is_franchisor: formDataConfig.is_franchisor } : { ...formDataConfig }
    };
    const isWalkInBol = (0, import_utils.isWalkIn)((_d = (_c = state.client) == null ? void 0 : _c.value) == null ? void 0 : _d.id);
    if (formDataConfig.data_range === "customer" && !isWalkInBol) {
      _data.customerId = (_e = state.client) == null ? void 0 : _e.value.id;
    }
    state.action({
      type: "pisell1.goCustomFormAddData",
      data: _data,
      callback: async (res) => {
        var _a2, _b2;
        let obj = {};
        for (let item of res) {
          obj[item.key] = item.value;
        }
        if (isMinorAndWaiver) {
          const oldList = ((_a2 = state == null ? void 0 : state.pet) == null ? void 0 : _a2.holders) || [];
          const formValue = ((_b2 = (obj["form_to_form_value"] || []).find((item) => item.form_id === formDataConfig.form_id)) == null ? void 0 : _b2.form_data_value) || [];
          const selectId = Math.max(...formValue);
          const oldDetail = oldList.find((item) => item.form_record_id === selectId);
          await getHolders();
          if (formValue.length && !oldDetail) {
            const value = (0, import_utils2.handleChangeHolder)(products, currentItem, { id: selectId });
            dispatch({
              type: "setService",
              payload: {
                value
              }
            });
          }
        } else {
          dispatch({
            type: "setPet",
            payload: {
              holders: !isWalkInBol ? [obj, ...state.pet.holders] : state.pet.holders
            }
          });
        }
      }
    });
  };
  return {
    handleAddHolder,
    waiverForm
  };
};
var useAddHolder_default = useAddHolder;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useFormDataConfig
});
