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 dayjs from 'dayjs';

/**
 * 平面图 / 预约详情里唤起 TicketBooking 时，与宿主 `pisell1.handleOpenEditBooking` 对齐的公共配置。
 * 新建（仅 createModeConfig）与编辑（order_id + 可选 createModeConfig 覆盖）共用同一套 booking 侧参数。
 */

/**
 * 组装 `createModeConfig` 中与商品来源、展示、业务码相关的字段（不含 `initialModalState`）。
 */
export function buildReservationTicketBookingCreateModeConfigBase(input) {
  var _associated_menu_list;
  var reservationBiz = input.reservationBiz,
    associated_menu_list_ids = input.associated_menu_list_ids;
  var menuIds = (_associated_menu_list = associated_menu_list_ids === null || associated_menu_list_ids === void 0 ? void 0 : associated_menu_list_ids.filter(function (id) {
    return typeof id === 'number' && Number.isFinite(id) && id > 0;
  })) !== null && _associated_menu_list !== void 0 ? _associated_menu_list : [];
  return _objectSpread(_objectSpread({
    bookingConfigParams: {
      item_type: 'appointment_booking',
      sub_type: 'food',
      source_type: 'pos'
    },
    productDisplayConfig: 'sku-list-a5',
    businessCode: reservationBiz
  }, menuIds.length > 0 ? {
    associated_menu_list_ids: menuIds
  } : {}), reservationBiz === 'dine_in' ? {
    order_type: 'table-order'
  } : {});
}

/**
 * 从桌位行组装 booking.resources 所需的资源映射。
 */
export function buildResourcePayloadFromReservationRow(row) {
  var _row$capacityLabel, _ref, _row$main_field;
  var relationIdNum = Number(row.id);
  var relationId = Number.isFinite(relationIdNum) ? relationIdNum : 0;
  var formIdNum = Number(row.form_id);
  var formId = Number.isFinite(formIdNum) ? formIdNum : 0;
  var cap = Number((_row$capacityLabel = row.capacityLabel) !== null && _row$capacityLabel !== void 0 ? _row$capacityLabel : 0);
  var capacity = Number.isFinite(cap) ? cap : 0;
  var resourceName = (_ref = (_row$main_field = row.main_field) !== null && _row$main_field !== void 0 ? _row$main_field : row.code) !== null && _ref !== void 0 ? _ref : '';
  return _defineProperty({}, String(formId), [{
    id: 0,
    shop_id: 0,
    schedule_event_id: 0,
    category: 'basic',
    form_id: formId,
    parent_relation_id: 0,
    relation_type: 'form',
    relation_id: relationId,
    capacity: capacity,
    like_status: 'common',
    metadata: resourceName ? {
      resource_name: resourceName
    } : null,
    created_at: null,
    updated_at: null,
    deleted_at: null
  }]);
}
function buildInitialExtendCapacityFromRuleProduct(product) {
  var capacitys = product === null || product === void 0 ? void 0 : product.capacity;
  if ((capacitys === null || capacitys === void 0 ? void 0 : capacitys.type) !== 'custom' || !Array.isArray(capacitys.custom)) {
    return [{
      id: 0,
      value: 1,
      name: ''
    }];
  }
  return capacitys.custom.map(function (d) {
    return {
      id: d.id,
      value: d.min || 1,
      name: d.name
    };
  });
}
export function buildServiceItemFromReservationRuleProduct(product, resource) {
  var _product$duration, _ref3, _product$title, _ref4, _product$cover, _product$id;
  var duration = Number(product === null || product === void 0 || (_product$duration = product.duration) === null || _product$duration === void 0 ? void 0 : _product$duration.value) || 0;
  var price = Number(product === null || product === void 0 ? void 0 : product.price) || 0;
  var start = dayjs();
  var end = duration > 0 ? start.add(duration, 'minute') : start;
  var title = (_ref3 = (_product$title = product === null || product === void 0 ? void 0 : product.title) !== null && _product$title !== void 0 ? _product$title : product === null || product === void 0 ? void 0 : product.name) !== null && _ref3 !== void 0 ? _ref3 : '';
  var cover = (_ref4 = (_product$cover = product === null || product === void 0 ? void 0 : product.cover) !== null && _product$cover !== void 0 ? _product$cover : product === null || product === void 0 ? void 0 : product.main_image) !== null && _ref4 !== void 0 ? _ref4 : '';
  return _objectSpread(_objectSpread({}, product), {}, {
    _id: "pre_".concat((_product$id = product === null || product === void 0 ? void 0 : product.id) !== null && _product$id !== void 0 ? _product$id : 'unknown', "_").concat(Date.now()),
    id: product === null || product === void 0 ? void 0 : product.id,
    product_id: product === null || product === void 0 ? void 0 : product.id,
    title: title,
    quantity: 1,
    price: price,
    cover: cover,
    duration: duration,
    _extend: {
      capacity: buildInitialExtendCapacityFromRuleProduct(product),
      duration: duration,
      price: price,
      total: price,
      origin_total: price,
      quantity: 1,
      product_name: title,
      startDate: start,
      start_date: start,
      endDate: end,
      end_date: end,
      resource: resource,
      start_time: start.format('HH:mm')
    }
  });
}
export function buildReservationTicketBookingCreateModeConfigForRow(input) {
  var reservationBiz = input.reservationBiz,
    associated_menu_list_ids = input.associated_menu_list_ids,
    reservationRuleProduct = input.reservationRuleProduct,
    row = input.row;
  var biz = reservationBiz != null && String(reservationBiz).trim() !== '' ? String(reservationBiz).trim() : '';
  if (!biz) return null;
  var createModeConfig = buildReservationTicketBookingCreateModeConfigBase({
    reservationBiz: biz,
    associated_menu_list_ids: associated_menu_list_ids
  });
  if (biz !== 'dine_in') {
    return createModeConfig;
  }
  if (!reservationRuleProduct || !row) {
    return null;
  }
  var resourcePayload = buildResourcePayloadFromReservationRow(row);
  buildServiceItemFromReservationRuleProduct(reservationRuleProduct, resourcePayload);
  createModeConfig.reservationRuleProduct = reservationRuleProduct;
  createModeConfig.row = row;
  createModeConfig.appointment_status = 'started';
  return createModeConfig;
}

/**
 * 派发宿主的「新建预约」action。
 *
 * dine_in 走 `pisell1.handleOpenCachedBigSale` + 新结构 createModeConfig；
 * 其它业务仍走 `pisell1.handleOpenEditBooking` + 完整 createModeConfig。
 */
export function openNewReservationBookingFromHost(input) {
  var appHelperUtils = input.appHelperUtils,
    reservationBiz = input.reservationBiz,
    associatedMenuListIds = input.associatedMenuListIds,
    reservationRuleProduct = input.reservationRuleProduct,
    row = input.row;
  if (typeof (appHelperUtils === null || appHelperUtils === void 0 ? void 0 : appHelperUtils.action) !== 'function') return false;
  var createModeConfig = buildReservationTicketBookingCreateModeConfigForRow({
    reservationBiz: reservationBiz,
    associated_menu_list_ids: associatedMenuListIds,
    reservationRuleProduct: reservationRuleProduct,
    row: row
  });
  if (!createModeConfig) return false;
  var biz = reservationBiz != null && String(reservationBiz).trim() !== '' ? String(reservationBiz).trim() : '';
  var isDineIn = biz === 'dine_in';
  appHelperUtils.action({
    type: isDineIn ? 'pisell1.handleOpenCachedBigSale' : 'pisell1.handleOpenEditBooking',
    data: {
      createModeConfig: createModeConfig
    },
    callback: function callback() {}
  });
  return true;
}