var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/salesSdk/bookingEditService/useBookingEditPetState.ts
var useBookingEditPetState_exports = {};
__export(useBookingEditPetState_exports, {
  useBookingEditPetState: () => useBookingEditPetState
});
module.exports = __toCommonJS(useBookingEditPetState_exports);
var import_react = require("react");
var import_ahooks = require("ahooks");
var import_serve = require("../../../components/ticketBooking/serve");
var import_serve2 = require("../../../components/booking/info/pet/serve");
var import_utils = require("../../../components/booking/utils");
var import_utils2 = require("../../../utils");
var import_utils3 = require("../../../components/booking/utils");
var import_enrichCartLineHolderDisplay = require("../utils/enrichCartLineHolderDisplay");
function buildHolderFormDataConfig(holder) {
  var _a, _b;
  if (!holder) return null;
  return {
    ...holder,
    ...holder == null ? void 0 : holder.form,
    options: [],
    form_id: (_a = holder == null ? void 0 : holder.form) == null ? void 0 : _a.id,
    field: (_b = holder == null ? void 0 : holder.form) == null ? void 0 : _b.form_field,
    relation_type: "appointment_booking",
    form_record: []
  };
}
function getMinorLinkedTableId(formDataConfig) {
  var _a, _b;
  if ((formDataConfig == null ? void 0 : formDataConfig.form_type) !== "minors") return null;
  const detail = (_a = formDataConfig == null ? void 0 : formDataConfig.form_field) == null ? void 0 : _a.find(
    (item) => item.filed_type === "form_to_form" && item.status === "hide"
  );
  return ((_b = detail == null ? void 0 : detail.setting) == null ? void 0 : _b.linkedTable) ?? null;
}
function useBookingEditPetState(open, baseBookingState, setRequest, holderSeed, holderSeedRequestKey) {
  var _a, _b, _c, _d, _e, _f;
  const [pet, setPet] = (0, import_react.useState)({});
  const apis = (0, import_react.useMemo)(
    () => ({
      getPetsData: import_serve.getPetsData
    }),
    []
  );
  const dispatch = (0, import_ahooks.useMemoizedFn)((cmd) => {
    if (cmd.type === "setPet" && cmd.payload) {
      setPet((prev) => ({ ...prev, ...cmd.payload }));
    }
  });
  (0, import_react.useEffect)(() => {
    var _a2, _b2, _c2, _d2, _e2, _f2, _g;
    if (!open) {
      setPet({});
      return;
    }
    const holder = (_a2 = baseBookingState.bookingConfig) == null ? void 0 : _a2.holder;
    const formDataConfig = buildHolderFormDataConfig(holder);
    if (!formDataConfig) {
      setPet({});
      return;
    }
    setPet({ formData: [formDataConfig] });
    if (setRequest) {
      import_utils.request.setRequest(setRequest);
      import_utils2.request.setRequest(setRequest);
    }
    const appointmentSubject = ((_d2 = (_c2 = (_b2 = baseBookingState.bookingConfig) == null ? void 0 : _b2.config) == null ? void 0 : _c2.basic) == null ? void 0 : _d2.appointment_subject) || {};
    const customerId = (_f2 = (_e2 = baseBookingState.client) == null ? void 0 : _e2.value) == null ? void 0 : _f2.id;
    const isWalkInCustomer = (0, import_utils3.isWalkIn)(customerId);
    if ((holder == null ? void 0 : holder.data_range) === "customer" && isWalkInCustomer) {
      return;
    }
    const params = {
      form_id: appointmentSubject.form_id,
      source_shop_customer: (_g = holder == null ? void 0 : holder.form) == null ? void 0 : _g.is_franchisor,
      customer_id: (holder == null ? void 0 : holder.data_range) === "customer" ? customerId : void 0,
      skip: 1,
      num: 100
    };
    const requestKey = (0, import_enrichCartLineHolderDisplay.buildHolderListRequestKey)(params);
    if ((holderSeed == null ? void 0 : holderSeed.length) && holderSeedRequestKey === requestKey) {
      setPet((prev) => ({
        ...prev,
        holders: holderSeed
      }));
      return;
    }
    (0, import_serve.getPetsData)(params).then((data) => {
      setPet((prev) => ({
        ...prev,
        holders: (data == null ? void 0 : data.list) || []
      }));
    }).catch((err) => {
      console.warn("[SalesSdk] load holders failed", err);
    });
    const linkedTableId = getMinorLinkedTableId(formDataConfig);
    if (linkedTableId) {
      (0, import_serve2.getFormDetail)(linkedTableId).then((res) => {
        setPet((prev) => ({ ...prev, waiverForm: res }));
      }).catch((err) => {
        console.warn("[SalesSdk] load waiver form failed", err);
      });
    }
  }, [
    open,
    (_a = baseBookingState.bookingConfig) == null ? void 0 : _a.holder,
    (_d = (_c = (_b = baseBookingState.bookingConfig) == null ? void 0 : _b.config) == null ? void 0 : _c.basic) == null ? void 0 : _d.appointment_subject,
    (_f = (_e = baseBookingState.client) == null ? void 0 : _e.value) == null ? void 0 : _f.id,
    setRequest,
    holderSeed,
    holderSeedRequestKey
  ]);
  return { pet, dispatch, apis };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useBookingEditPetState
});
