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/utils/buildCartHolderParams.ts
var buildCartHolderParams_exports = {};
__export(buildCartHolderParams_exports, {
  buildCartHolderOption: () => buildCartHolderOption,
  buildCartHolderParams: () => buildCartHolderParams,
  getAppointmentCartHolderConfig: () => getAppointmentCartHolderConfig,
  getSkuRelationFormId: () => getSkuRelationFormId,
  getSkuRelationFormRecordId: () => getSkuRelationFormRecordId,
  getVenueCartHolderConfig: () => getVenueCartHolderConfig,
  handleCartHolderFormSuccess: () => handleCartHolderFormSuccess,
  isHolderConfigEnabled: () => isHolderConfigEnabled,
  resolveCartItemHolderId: () => resolveCartItemHolderId,
  validateRequiredCartHolders: () => validateRequiredCartHolders
});
module.exports = __toCommonJS(buildCartHolderParams_exports);
var import_utils = require("../components/appointmentBooking/utils");
var isHolderConfigEnabled = (holderConfig) => (holderConfig == null ? void 0 : holderConfig.status) === "enable";
var toHolderRecordId = (value) => {
  if (value == null || value === "") return void 0;
  const id = Number(value);
  return Number.isNaN(id) ? void 0 : id;
};
var resolveOriginHolderId = (holder) => {
  const formRecord = holder == null ? void 0 : holder.form_record;
  if (Array.isArray(formRecord)) {
    for (const value of formRecord) {
      const id = toHolderRecordId(value);
      if (id != null) return id;
    }
  } else {
    const id = toHolderRecordId(formRecord);
    if (id != null) return id;
  }
  return toHolderRecordId(holder == null ? void 0 : holder.form_record_id);
};
var resolveCartItemHolderId = (item) => {
  var _a;
  const originHolder = (_a = item == null ? void 0 : item._origin) == null ? void 0 : _a.holder;
  if (originHolder && typeof originHolder === "object") {
    return resolveOriginHolderId(originHolder);
  }
  return toHolderRecordId(item == null ? void 0 : item.holder_id);
};
var getCurrentCustomerId = (utils) => {
  var _a;
  return (_a = (0, import_utils.getCustomer)(utils)) == null ? void 0 : _a.id;
};
var createCartHolderOption = (holder, customerId) => {
  return {
    key: holder == null ? void 0 : holder.form_record_id,
    id: holder == null ? void 0 : holder.form_record_id,
    label: holder == null ? void 0 : holder.main_field,
    name: holder == null ? void 0 : holder.main_field,
    username: holder == null ? void 0 : holder.main_field,
    type: "holder",
    _origin: {
      ...holder,
      customer_id: customerId
    }
  };
};
var buildCartHolderOption = (holder, utils) => {
  if (!holder) return {};
  return createCartHolderOption(holder, getCurrentCustomerId(utils));
};
var buildCartHolderOptions = (records, utils) => {
  const customerId = getCurrentCustomerId(utils);
  return records.map((record) => createCartHolderOption(record, customerId));
};
var getCartHolderTitle = (records, holderId) => {
  var _a;
  if (!holderId) return "";
  return (_a = records.find(
    (record) => Number(record == null ? void 0 : record.form_record_id) === holderId
  )) == null ? void 0 : _a.main_field;
};
var buildCartHolderParams = ({
  item,
  holderConfig = {},
  formData,
  translation,
  enabled = true,
  utils
}) => {
  var _a, _b, _c;
  if (!enabled || !isHolderConfigEnabled(holderConfig)) {
    return {
      isShowChangeHolder: false,
      holders: []
    };
  }
  const records = (formData == null ? void 0 : formData.records) || [];
  const holderId = resolveCartItemHolderId(item);
  return {
    holder_id: holderId,
    isErrorHolder: !!(!holderId && (holderConfig == null ? void 0 : holderConfig.required)),
    isShowChangeHolder: true,
    addHolderButtonText: translation((_c = (_b = (_a = formData == null ? void 0 : formData.form) == null ? void 0 : _a.form) == null ? void 0 : _b.setting) == null ? void 0 : _c.title),
    holder_title: getCartHolderTitle(records, holderId),
    holders: buildCartHolderOptions(records, utils)
  };
};
var getAppointmentCartHolderConfig = (item) => {
  var _a;
  return ((_a = item == null ? void 0 : item._productInit) == null ? void 0 : _a.holder_config) || {};
};
var getVenueCartHolderConfig = (item) => {
  var _a, _b;
  return ((_b = (_a = item == null ? void 0 : item._origin) == null ? void 0 : _a.product) == null ? void 0 : _b.holder_config) || {};
};
var validateRequiredCartHolders = ({
  items = [],
  getHolderConfig = getVenueCartHolderConfig
}) => {
  console.log(items, "items1111");
  const invalidItems = items.filter((item) => {
    const holderConfig = getHolderConfig(item);
    if (!isHolderConfigEnabled(holderConfig) || !(holderConfig == null ? void 0 : holderConfig.required)) {
      return false;
    }
    return !resolveCartItemHolderId(item);
  });
  return {
    hasError: invalidItems.length > 0,
    invalidItems
  };
};
var getSkuRelationFormId = (holderMap, formId) => {
  var _a, _b, _c, _d, _e;
  if (!formId) return 0;
  const formData = holderMap == null ? void 0 : holderMap[formId];
  if (((_b = (_a = formData == null ? void 0 : formData.form) == null ? void 0 : _a.form) == null ? void 0 : _b.form_type) !== "minors") return 0;
  const detail = (_d = (_c = formData == null ? void 0 : formData.form) == null ? void 0 : _c.field) == null ? void 0 : _d.find(
    (item) => (item == null ? void 0 : item.filed_type) === "form_to_form" && (item == null ? void 0 : item.status) === "hide"
  );
  return ((_e = detail == null ? void 0 : detail.setting) == null ? void 0 : _e.linkedTable) || 0;
};
var getSkuRelationFormRecordId = (res) => {
  var _a, _b;
  const formToFormValue = (_b = (_a = res == null ? void 0 : res.values) == null ? void 0 : _a.form_to_form_value) == null ? void 0 : _b[0];
  const formDataValue = (formToFormValue == null ? void 0 : formToFormValue.form_data_value) || [];
  return formDataValue[formDataValue.length - 1];
};
var handleCartHolderFormSuccess = ({
  res,
  formId,
  skuRelationFormId,
  utils,
  module: module2
}) => {
  var _a, _b, _c, _d, _e;
  let formRecordId = (_a = res == null ? void 0 : res.values) == null ? void 0 : _a.form_record_id;
  let resolvedFormId = (_b = res == null ? void 0 : res.values) == null ? void 0 : _b.form_id;
  if (formId && skuRelationFormId) {
    const customer = (0, import_utils.getCustomer)(utils);
    (_c = module2 == null ? void 0 : module2.refreshAllHolderFormRecords) == null ? void 0 : _c.call(module2, {
      customer_id: customer == null ? void 0 : customer.id
    });
    formRecordId = getSkuRelationFormRecordId(res);
    resolvedFormId = formId;
  } else if (formId) {
    (_d = module2 == null ? void 0 : module2.appendFormRecord) == null ? void 0 : _d.call(module2, {
      record: res == null ? void 0 : res.values
    });
  }
  return {
    formRecordId,
    mainField: (_e = res == null ? void 0 : res.values) == null ? void 0 : _e.main_field,
    formId: resolvedFormId
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildCartHolderOption,
  buildCartHolderParams,
  getAppointmentCartHolderConfig,
  getSkuRelationFormId,
  getSkuRelationFormRecordId,
  getVenueCartHolderConfig,
  handleCartHolderFormSuccess,
  isHolderConfigEnabled,
  resolveCartItemHolderId,
  validateRequiredCartHolders
});
