function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { formatHolderOptions, normalizeHolderIds } from "./formatHolderOptions";
/**
 * 从 bookingConfig 解析 Holder 展示相关开关（整单单一 config）。
 */
export function resolveHolderDisplayConfig(bookingConfig) {
  var _bookingConfig$config, _bookingConfig$holder;
  return {
    isFormSubject: (bookingConfig === null || bookingConfig === void 0 || (_bookingConfig$config = bookingConfig.config) === null || _bookingConfig$config === void 0 || (_bookingConfig$config = _bookingConfig$config.basic) === null || _bookingConfig$config === void 0 || (_bookingConfig$config = _bookingConfig$config.appointment_subject) === null || _bookingConfig$config === void 0 ? void 0 : _bookingConfig$config.type) === 'form',
    holderType: (bookingConfig === null || bookingConfig === void 0 || (_bookingConfig$holder = bookingConfig.holder) === null || _bookingConfig$holder === void 0 || (_bookingConfig$holder = _bookingConfig$holder.form) === null || _bookingConfig$holder === void 0 ? void 0 : _bookingConfig$holder.form_type) || 'minors'
  };
}

/**
 * 从 booking 行归一化 holder_id（与 LineItem Holders 读取路径一致）。
 */
export function pickHolderIdFromBooking(booking) {
  var _ref, _ref2, _ref3, _ref4, _ref5, _booking$_extend$merg, _booking$_extend, _booking$metadata, _booking$_extend2, _booking$_extend3, _booking$holder;
  return (_ref = (_ref2 = (_ref3 = (_ref4 = (_ref5 = (_booking$_extend$merg = booking === null || booking === void 0 || (_booking$_extend = booking._extend) === null || _booking$_extend === void 0 ? void 0 : _booking$_extend.mergedHolderIds) !== null && _booking$_extend$merg !== void 0 ? _booking$_extend$merg : booking === null || booking === void 0 ? void 0 : booking.holder_id) !== null && _ref5 !== void 0 ? _ref5 : booking === null || booking === void 0 || (_booking$metadata = booking.metadata) === null || _booking$metadata === void 0 ? void 0 : _booking$metadata.holder_id) !== null && _ref4 !== void 0 ? _ref4 : booking === null || booking === void 0 || (_booking$_extend2 = booking._extend) === null || _booking$_extend2 === void 0 || (_booking$_extend2 = _booking$_extend2.product) === null || _booking$_extend2 === void 0 || (_booking$_extend2 = _booking$_extend2.metadata) === null || _booking$_extend2 === void 0 ? void 0 : _booking$_extend2.holder_id) !== null && _ref3 !== void 0 ? _ref3 : booking === null || booking === void 0 || (_booking$_extend3 = booking._extend) === null || _booking$_extend3 === void 0 || (_booking$_extend3 = _booking$_extend3.product) === null || _booking$_extend3 === void 0 || (_booking$_extend3 = _booking$_extend3._extend) === null || _booking$_extend3 === void 0 ? void 0 : _booking$_extend3.holder_id) !== null && _ref2 !== void 0 ? _ref2 : // 已落库预约订单会按 OS booking 协议把 holder 写在 holder.form_record。
  booking === null || booking === void 0 || (_booking$holder = booking.holder) === null || _booking$holder === void 0 ? void 0 : _booking$holder.form_record) !== null && _ref !== void 0 ? _ref : null;
}

/**
 * 为购物车 booking 行附加 Holder 展示字段（仅 UI，不写 OS）。
 */
export function enrichCartBookingHolderDisplay(booking, ctx) {
  var _booking$_extend4, _ref6, _productLine$num;
  var holder_id = pickHolderIdFromBooking(booking);
  var productLine = (_booking$_extend4 = booking._extend) === null || _booking$_extend4 === void 0 ? void 0 : _booking$_extend4.product;
  var holderMaxCount = Number((_ref6 = (_productLine$num = productLine === null || productLine === void 0 ? void 0 : productLine.num) !== null && _productLine$num !== void 0 ? _productLine$num : productLine === null || productLine === void 0 ? void 0 : productLine.product_quantity) !== null && _ref6 !== void 0 ? _ref6 : normalizeHolderIds(holder_id).length) || 1;
  return _objectSpread(_objectSpread({}, booking), {}, {
    holder_id: holder_id,
    holderOptions: ctx.holderOptions,
    holders: ctx.holderOptions,
    isFormSubject: ctx.displayConfig.isFormSubject,
    holderType: ctx.displayConfig.holderType,
    holderMaxCount: holderMaxCount
  });
}

/**
 * 构建 getPetsData 请求参数（与 useBookingEditPetState 对齐）。
 */
export function buildHolderListRequestParams(input) {
  var _holder$form, _bookingConfig$config2, _holder$form2;
  var bookingConfig = input.bookingConfig,
    customerId = input.customerId;
  var holder = bookingConfig === null || bookingConfig === void 0 ? void 0 : bookingConfig.holder;
  if (!(holder !== null && holder !== void 0 && (_holder$form = holder.form) !== null && _holder$form !== void 0 && _holder$form.id) && !(holder !== null && holder !== void 0 && holder.form_id)) return null;
  var appointmentSubject = (bookingConfig === null || bookingConfig === void 0 || (_bookingConfig$config2 = bookingConfig.config) === null || _bookingConfig$config2 === void 0 || (_bookingConfig$config2 = _bookingConfig$config2.basic) === null || _bookingConfig$config2 === void 0 ? void 0 : _bookingConfig$config2.appointment_subject) || {};
  if (!appointmentSubject.form_id) return null;
  return {
    form_id: appointmentSubject.form_id,
    source_shop_customer: holder === null || holder === void 0 || (_holder$form2 = holder.form) === null || _holder$form2 === void 0 ? void 0 : _holder$form2.is_franchisor,
    customer_id: (holder === null || holder === void 0 ? void 0 : holder.data_range) === 'customer' ? customerId : undefined,
    skip: 1,
    num: 100
  };
}

/**
 * 构建 Holder 列表请求缓存 key，保证只有同一 form/customer 参数才复用缓存。
 */
export function buildHolderListRequestKey(params) {
  var _params$form_id, _params$source_shop_c, _params$customer_id, _params$skip, _params$num;
  if (!params) return null;
  return JSON.stringify({
    form_id: (_params$form_id = params.form_id) !== null && _params$form_id !== void 0 ? _params$form_id : null,
    source_shop_customer: (_params$source_shop_c = params.source_shop_customer) !== null && _params$source_shop_c !== void 0 ? _params$source_shop_c : null,
    customer_id: (_params$customer_id = params.customer_id) !== null && _params$customer_id !== void 0 ? _params$customer_id : null,
    skip: (_params$skip = params.skip) !== null && _params$skip !== void 0 ? _params$skip : 1,
    num: (_params$num = params.num) !== null && _params$num !== void 0 ? _params$num : 100
  });
}
export { formatHolderOptions };