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 _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
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); }
/**
 * 扫码商品条码预选规格纯函数（对齐 ticketBooking addServiceVariant/utils）。
 */

/** 套餐商品 */
export function isScanProductBundle(item) {
  return ((item === null || item === void 0 ? void 0 : item.bundle_group_count) || 0) > 0;
}

/** 单规格 / 选项组商品 */
export function isScanProductOption(item) {
  var _item$option_group;
  return ((item === null || item === void 0 ? void 0 : item.option_group_count) || 0) > 0 || ((item === null || item === void 0 || (_item$option_group = item.option_group) === null || _item$option_group === void 0 ? void 0 : _item$option_group.length) || 0) > 0;
}
function formatVariantGroupTitle(title) {
  var variantGroup = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  try {
    return variantGroup.map(function (group) {
      var value = title === null || title === void 0 ? void 0 : title[group === null || group === void 0 ? void 0 : group.position_id];
      return value ? "".concat((group === null || group === void 0 ? void 0 : group.name) || '-', ": ").concat(value) : '';
    }).filter(Boolean).join(', ');
  } catch (_unused) {
    return '';
  }
}
function formatDataKey(data) {
  var _next$option, _next$bundle;
  var next = _objectSpread({}, data);
  var key = "".concat(next.product_id);
  var rowKey = key;
  if (next.product_variant_id) {
    var str = "_".concat(next.product_variant_id);
    key += str;
    rowKey += str;
  }
  if (next !== null && next !== void 0 && (_next$option = next.option) !== null && _next$option !== void 0 && _next$option.length) {
    var _iterator = _createForOfIteratorHelper(next.option),
      _step;
    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var item = _step.value;
        var _str = "_".concat(item.option_group_id, "_").concat(item.product_option_item_id);
        key += _str;
        rowKey += "".concat(_str, "_").concat(item.quantity || item.num);
      }
    } catch (err) {
      _iterator.e(err);
    } finally {
      _iterator.f();
    }
  }
  if (next !== null && next !== void 0 && (_next$bundle = next.bundle) !== null && _next$bundle !== void 0 && _next$bundle.length) {
    var _iterator2 = _createForOfIteratorHelper(next.bundle),
      _step2;
    try {
      for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
        var _item = _step2.value;
        var _str2 = "_".concat(_item.group_id, "_").concat(_item.bundle_product_id);
        key += _str2;
        rowKey += "".concat(_str2, "_").concat(_item.quantity || _item.num);
        if (_item !== null && _item !== void 0 && _item.option) {
          var _iterator3 = _createForOfIteratorHelper(_item.option),
            _step3;
          try {
            for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
              var opt = _step3.value;
              var optStr = "_".concat(opt.option_group_id, "_").concat(opt.product_option_item_id);
              key += optStr;
              rowKey += "".concat(optStr, "_").concat(opt.quantity || opt.num);
            }
          } catch (err) {
            _iterator3.e(err);
          } finally {
            _iterator3.f();
          }
        }
      }
    } catch (err) {
      _iterator2.e(err);
    } finally {
      _iterator2.f();
    }
  }
  if (next !== null && next !== void 0 && next.session) {
    var _next$session;
    var _str3 = "_".concat((_next$session = next.session) === null || _next$session === void 0 ? void 0 : _next$session.key);
    key += _str3;
    rowKey += _str3;
  }
  if (next !== null && next !== void 0 && next.schedule) {
    var _next$schedule;
    var _str4 = "_".concat((_next$schedule = next.schedule) === null || _next$schedule === void 0 ? void 0 : _next$schedule.key);
    key += _str4;
    rowKey += _str4;
  }
  next.key = key;
  next.rowKey = rowKey;
  return next;
}

/**
 * 根据条码匹配 variant，生成 cartDetailValue（对齐 ticketBooking `genValues`）。
 *
 * @example
 * ```ts
 * const values = genScanProductValues(catalogItem, '6901234567890');
 * const itemWithSelection = { ...catalogItem, cartDetailValue: values };
 * ```
 */
export function genScanProductValues(item, code) {
  var _item$variant_group, _item$option_group2;
  var params = {
    bundle: [],
    option: [],
    product_id: item.id,
    product_variant_id: 0,
    quantity: 1,
    rowKey: '',
    session: null
  };
  if (item !== null && item !== void 0 && (_item$variant_group = item.variant_group) !== null && _item$variant_group !== void 0 && _item$variant_group.length || item !== null && item !== void 0 && (_item$option_group2 = item.option_group) !== null && _item$option_group2 !== void 0 && _item$option_group2.length) {
    var variantItem = (item.variant || []).find(function (v) {
      return (v === null || v === void 0 ? void 0 : v.code) === code || (v === null || v === void 0 ? void 0 : v.barcode) === code;
    });
    if (variantItem) {
      params.price = Number((variantItem === null || variantItem === void 0 ? void 0 : variantItem.price) || 0);
      params.product_variant_id = variantItem.id;
      params.product_option_string = formatVariantGroupTitle(variantItem === null || variantItem === void 0 ? void 0 : variantItem.title, (item === null || item === void 0 ? void 0 : item.variant_group) || []);
    }
  }
  return formatDataKey(params);
}

/**
 * 统计条码命中的 variant 数量（多命中时需弹窗选择）。
 */
export function countScanVariantMatches(item, code) {
  return ((item === null || item === void 0 ? void 0 : item.variant) || []).filter(function (v) {
    return (v === null || v === void 0 ? void 0 : v.code) === code || (v === null || v === void 0 ? void 0 : v.barcode) === code;
  }).length;
}