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/components/ticketBooking/components/addServiceVariant/utils.ts
var utils_exports = {};
__export(utils_exports, {
  createCacheItem: () => createCacheItem,
  formatDataKey: () => formatDataKey,
  formatVariantGroupTitle: () => formatVariantGroupTitle,
  genValues: () => genValues,
  getHandleAddService: () => getHandleAddService,
  getHandleSetService: () => getHandleSetService,
  isBundle: () => isBundle,
  isOption: () => isOption,
  isVariant: () => isVariant,
  setHandleAddService: () => setHandleAddService,
  setHandleSetService: () => setHandleSetService
});
module.exports = __toCommonJS(utils_exports);
var import_utils = require("@pisell/utils");
var import_utils2 = require("../../../booking/utils");
var import_utils3 = require("../../../booking/info2/service/addService/utils");
var import_utils4 = require("../../../booking/info/service2/utils");
var formatVariantGroupTitle = (title, variant_group = []) => {
  try {
    const formattedTitle = variant_group == null ? void 0 : variant_group.map((item) => {
      const value = title[item == null ? void 0 : item.position_id];
      if (value) {
        return `${(item == null ? void 0 : item.name) || "-"}: ${value}`;
      }
      return "";
    }).filter(Boolean);
    return formattedTitle == null ? void 0 : formattedTitle.join(", ");
  } catch (error) {
    console.log(error);
  }
};
var createCacheItem = (detail, values, state) => {
  let cacheItem = {
    ...detail,
    product_id: detail.id,
    _id: (0, import_utils.getUniqueId)(),
    _key: values.key,
    _extend: {
      start_date: state.date.value,
      quantity: (values == null ? void 0 : values.quantity) || 1,
      price: detail.price,
      product_name: detail.title,
      other: values
    },
    new: 1
  };
  let _total = (0, import_utils2.getProductTotalPrice)(cacheItem);
  cacheItem._extend.total = _total;
  cacheItem._extend.origin_total = _total;
  cacheItem.currentDefaultTime = state.date.value.format("HH:mm");
  cacheItem = (0, import_utils4.isNormalProductByDurationSchedule)(cacheItem) ? cacheItem : (0, import_utils3.getProductExtend)({
    cacheItem,
    state,
    isDisabledTime: true
  });
  return cacheItem;
};
var formatDataKey = (data) => {
  var _a, _b, _c, _d;
  const _data = {
    ...data
  };
  let key = `${_data.product_id}`;
  let rowKey = key;
  if (_data.product_variant_id) {
    let str = `_${_data.product_variant_id}`;
    key += str;
    rowKey += str;
  }
  if ((_a = _data == null ? void 0 : _data.option) == null ? void 0 : _a.length) {
    for (const item of _data.option) {
      let str = `_${item.option_group_id}_${item.product_option_item_id}`;
      key += str;
      rowKey += `${str}_${item.quantity || item.num}`;
    }
  }
  if ((_b = _data == null ? void 0 : _data.bundle) == null ? void 0 : _b.length) {
    for (const item of _data.bundle) {
      let str = `_${item.group_id}_${item.bundle_product_id}`;
      key += str;
      rowKey += `${str}_${item.quantity || item.num}`;
      if (item == null ? void 0 : item.option) {
        for (const opt of item.option) {
          let str2 = `_${opt.option_group_id}_${opt.product_option_item_id}`;
          key += str2;
          rowKey += `${str2}_${opt.quantity || opt.num}`;
        }
      }
    }
  }
  if (_data == null ? void 0 : _data.session) {
    let str = `_${(_c = _data == null ? void 0 : _data.session) == null ? void 0 : _c.key}`;
    key += str;
    rowKey += str;
  }
  if (_data == null ? void 0 : _data.schedule) {
    let str = `_${(_d = _data == null ? void 0 : _data.schedule) == null ? void 0 : _d.key}`;
    key += str;
    rowKey += str;
  }
  _data.key = key;
  _data.rowKey = rowKey;
  return _data;
};
var isVariant = (item) => {
  return (item == null ? void 0 : item.variant_group_count) > 0 && (item == null ? void 0 : item.option_group_count) === 0;
};
var isOption = (item) => {
  var _a;
  return (item == null ? void 0 : item.option_group_count) > 0 || ((_a = item == null ? void 0 : item.option_group) == null ? void 0 : _a.length) > 0;
};
var isBundle = (item) => {
  return (item == null ? void 0 : item.bundle_group_count) > 0;
};
var genValues = (item, code) => {
  var _a, _b;
  let params = {
    bundle: [],
    option: [],
    // price,
    product_id: item.id,
    product_variant_id: 0,
    // product_option_string,
    quantity: 1,
    rowKey: "",
    session: null
  };
  if (((_a = item == null ? void 0 : item.variant_group) == null ? void 0 : _a.length) || ((_b = item == null ? void 0 : item.option_group) == null ? void 0 : _b.length)) {
    const variantItem = item.variant.find((item2) => item2.code === code || item2.barcode === code);
    const variant_group_title = formatVariantGroupTitle(
      variantItem == null ? void 0 : variantItem.title,
      (item == null ? void 0 : item.variant_group) || []
    );
    params.price = Number((variantItem == null ? void 0 : variantItem.price) || 0);
    params.product_variant_id = variantItem.id;
    params.product_option_string = variant_group_title;
  }
  return formatDataKey(params);
};
var handleSetService = null;
var setHandleSetService = (fn) => {
  handleSetService = fn;
};
var getHandleSetService = () => {
  return handleSetService;
};
var handleAddService = null;
var setHandleAddService = (fn) => {
  handleAddService = fn;
};
var getHandleAddService = () => {
  return handleAddService;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  createCacheItem,
  formatDataKey,
  formatVariantGroupTitle,
  genValues,
  getHandleAddService,
  getHandleSetService,
  isBundle,
  isOption,
  isVariant,
  setHandleAddService,
  setHandleSetService
});
