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/plus/salesSdk/hosts/HolderHostRenderer.tsx
var HolderHostRenderer_exports = {};
__export(HolderHostRenderer_exports, {
  HolderHostRenderer: () => HolderHostRenderer
});
module.exports = __toCommonJS(HolderHostRenderer_exports);
var import_react = __toESM(require("react"));
var import_ahooks = require("ahooks");
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var import_utils = require("../../../components/booking/utils");
var import_serve = require("../../../components/ticketBooking/serve");
var import_useSalesSdk = require("../hooks/useSalesSdk");
var import_SalesSdkSelectHolderModal = require("../components/SalesSdkSelectHolderModal");
var import_useBookingEditPetState = require("../bookingEditService/useBookingEditPetState");
var import_buildBookingEditDrawerState = require("../bookingEditService/buildBookingEditDrawerState");
var import_buildBookingEditDrawerState2 = require("../bookingEditService/buildBookingEditDrawerState");
var import_SalesSdkHostBridgesContext = require("../context/SalesSdkHostBridgesContext");
var import_SalesSdkHolderContext = require("../context/SalesSdkHolderContext");
var import_enrichCartLineHolderDisplay = require("../utils/enrichCartLineHolderDisplay");
var HolderHostRenderer = () => {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
  const bridges = (0, import_SalesSdkHostBridgesContext.useSalesSdkHostBridges)();
  const bridge = bridges.holder;
  const pending = (0, import_react.useSyncExternalStore)(bridge.subscribe, bridge.getSnapshot, () => null);
  const { bookingTicket, tempOrder } = (0, import_useSalesSdk.useSalesSdk)();
  const context = (0, import_useEngineContext.default)();
  const action = (_b = (_a = context == null ? void 0 : context.appHelper) == null ? void 0 : _a.utils) == null ? void 0 : _b.action;
  const open = !!pending;
  const payload = pending == null ? void 0 : pending.payload;
  const cacheItem = payload == null ? void 0 : payload.cacheItem;
  const customerId = (0, import_buildBookingEditDrawerState2.resolveBookingEditCustomerId)(
    tempOrder,
    (payload == null ? void 0 : payload.customerId) ?? ((_c = cacheItem == null ? void 0 : cacheItem._extend) == null ? void 0 : _c.customer_id),
    (_e = (_d = bookingTicket == null ? void 0 : bookingTicket.getOrderCustomerSnapshot) == null ? void 0 : _d.call(bookingTicket)) == null ? void 0 : _e.id
  );
  const bookingState = (0, import_react.useMemo)(() => {
    var _a2, _b2, _c2, _d2, _e2;
    if (!cacheItem || !bookingTicket) return null;
    const bookingConfig = ((_a2 = bookingTicket.getBookingConfig) == null ? void 0 : _a2.call(bookingTicket)) ?? null;
    const date = ((_b2 = bookingTicket.getBookingDate) == null ? void 0 : _b2.call(bookingTicket)) ?? ((_e2 = (_d2 = (_c2 = cacheItem == null ? void 0 : cacheItem._extend) == null ? void 0 : _c2.start_date) == null ? void 0 : _d2.format) == null ? void 0 : _e2.call(_d2, "YYYY-MM-DD")) ?? void 0;
    return (0, import_buildBookingEditDrawerState.buildBookingEditDrawerState)({
      cacheItem,
      bookingConfig,
      date: typeof date === "string" ? date : void 0,
      customerId,
      locale: void 0,
      resourcesOrigin: []
    });
  }, [cacheItem, bookingTicket, customerId]);
  const { pet, dispatch } = (0, import_useBookingEditPetState.useBookingEditPetState)(open, bookingState ?? {});
  const { reload: reloadHolderOptions, upsertHolderOptions } = (0, import_SalesSdkHolderContext.useSalesSdkHolderOptional)();
  const mergedState = (0, import_react.useMemo)(() => {
    if (!bookingState) return null;
    return { ...bookingState, pet: { ...bookingState.pet, ...pet } };
  }, [bookingState, pet]);
  const holderConfig = ((_f = mergedState == null ? void 0 : mergedState.bookingConfig) == null ? void 0 : _f.holder) || {};
  const maxSelectedCount = (payload == null ? void 0 : payload.maxSelectedCount) || (holderConfig == null ? void 0 : holderConfig.maxSelectedCount) || ((_g = cacheItem == null ? void 0 : cacheItem._extend) == null ? void 0 : _g.quantity) || 1;
  const { formDataConfig, isMinorAndWaiver } = (0, import_react.useMemo)(() => {
    var _a2, _b2, _c2, _d2, _e2;
    const holder = (_a2 = mergedState == null ? void 0 : mergedState.bookingConfig) == null ? void 0 : _a2.holder;
    if (!holder) {
      return { formDataConfig: null, isMinorAndWaiver: false };
    }
    const formDataConfig2 = {
      ...holder,
      ...holder == null ? void 0 : holder.form,
      options: [],
      form_id: (_b2 = holder == null ? void 0 : holder.form) == null ? void 0 : _b2.id,
      field: (_c2 = holder == null ? void 0 : holder.form) == null ? void 0 : _c2.form_field,
      relation_type: "appointment_booking",
      form_record: []
    };
    let isMinorAndWaiver2 = false;
    if ((formDataConfig2 == null ? void 0 : formDataConfig2.form_type) === "minors") {
      const detail = (_d2 = formDataConfig2 == null ? void 0 : formDataConfig2.form_field) == null ? void 0 : _d2.find(
        (item) => item.filed_type === "form_to_form" && item.status === "hide"
      );
      if (detail) {
        isMinorAndWaiver2 = (_e2 = detail == null ? void 0 : detail.setting) == null ? void 0 : _e2.linkedTable;
      }
    }
    return { formDataConfig: formDataConfig2, isMinorAndWaiver: isMinorAndWaiver2 };
  }, [(_h = mergedState == null ? void 0 : mergedState.bookingConfig) == null ? void 0 : _h.holder]);
  const refreshHoldersFromServer = (0, import_ahooks.useMemoizedFn)(async (state) => {
    var _a2, _b2;
    const params = (0, import_enrichCartLineHolderDisplay.buildHolderListRequestParams)({
      bookingConfig: state.bookingConfig,
      customerId: (_b2 = (_a2 = state.client) == null ? void 0 : _a2.value) == null ? void 0 : _b2.id
    });
    if (!params) return;
    const data = await (0, import_serve.getPetsData)(params);
    const list = (data == null ? void 0 : data.list) || [];
    dispatch({
      type: "setPet",
      payload: { holders: list }
    });
    upsertHolderOptions(list);
    reloadHolderOptions();
  });
  const handleAddHolder = (0, import_ahooks.useMemoizedFn)(() => {
    var _a2, _b2, _c2, _d2, _e2;
    if (!action || !mergedState) return;
    const waiverForm = (_a2 = mergedState.pet) == null ? void 0 : _a2.waiverForm;
    action({
      type: "pisell1.goCustomFormAddData",
      data: {
        zIndex: 1101,
        customerId: (_c2 = (_b2 = mergedState.client) == null ? void 0 : _b2.value) == null ? void 0 : _c2.id,
        formDataConfig: isMinorAndWaiver ? {
          ...waiverForm,
          customer_id: (_e2 = (_d2 = mergedState.client) == null ? void 0 : _d2.value) == null ? void 0 : _e2.id,
          is_franchisor: formDataConfig == null ? void 0 : formDataConfig.is_franchisor
        } : { ...formDataConfig || {} }
      },
      callback: async (res) => {
        var _a3, _b3, _c3;
        const obj = {};
        for (const item of res) {
          obj[item.key] = item.value;
        }
        const isWalkInBol = (0, import_utils.isWalkIn)((_b3 = (_a3 = mergedState.client) == null ? void 0 : _a3.value) == null ? void 0 : _b3.id);
        if (isMinorAndWaiver) {
          try {
            await refreshHoldersFromServer(mergedState);
          } catch (err) {
            console.warn("[SalesSdk] refresh holders after minor create failed", err);
          }
          return;
        }
        if (!isWalkInBol) {
          dispatch({
            type: "setPet",
            payload: { holders: [obj, ...((_c3 = mergedState.pet) == null ? void 0 : _c3.holders) || []] }
          });
          upsertHolderOptions([obj]);
        }
      }
    });
  });
  const handleConfirm = (0, import_ahooks.useMemoizedFn)((ids) => {
    reloadHolderOptions();
    bridge.complete(ids);
  });
  if (!open || !mergedState) {
    return null;
  }
  return /* @__PURE__ */ import_react.default.createElement(
    import_SalesSdkSelectHolderModal.SalesSdkSelectHolderModal,
    {
      open,
      holders: ((_i = mergedState.pet) == null ? void 0 : _i.holders) || [],
      bookingConfig: mergedState.bookingConfig,
      customerId,
      maxSelectedCount,
      onAddHolder: action ? handleAddHolder : void 0,
      onCancel: () => bridge.complete(null),
      onSkip: () => bridge.complete(null),
      onConfirm: handleConfirm
    }
  );
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  HolderHostRenderer
});
