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); }
import { getUniqueId } from "@pisell/utils";
import { getProductTotalPrice } from "../../../booking/utils";
import { getProductExtend } from "../../../booking/info2/service/addService/utils";
import { isNormalProductByDurationSchedule } from "../../../booking/info/service2/utils";
export var formatVariantGroupTitle = function formatVariantGroupTitle(title) {
  var variant_group = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  try {
    var formattedTitle = variant_group === null || variant_group === void 0 ? void 0 : variant_group.map(function (item) {
      // 根据position_id获取title中对应的值
      var value = title[item === null || item === void 0 ? void 0 : item.position_id];

      // 有值, 返回多规格名称和值 "尺码: S"
      if (value) {
        return "".concat((item === null || item === void 0 ? void 0 : item.name) || '-', ": ").concat(value);
      }

      // 没有值, 返回空字符串
      return '';
    }).filter(Boolean);
    return formattedTitle === null || formattedTitle === void 0 ? void 0 : formattedTitle.join(', ');
  } catch (error) {
    console.log(error);
  }
};
export var createCacheItem = function createCacheItem(detail, values, state) {
  var cacheItem = _objectSpread(_objectSpread({}, detail), {}, {
    product_id: detail.id,
    _id: getUniqueId(),
    _key: values.key,
    _extend: {
      start_date: state.date.value,
      quantity: (values === null || values === void 0 ? void 0 : values.quantity) || 1,
      price: detail.price,
      product_name: detail.title,
      other: values
    },
    new: 1
  });
  var _total = getProductTotalPrice(cacheItem);
  // 用于折扣 可修改
  cacheItem._extend.total = _total;
  // 用于储存原价
  cacheItem._extend.origin_total = _total;
  cacheItem.currentDefaultTime = state.date.value.format('HH:mm');
  cacheItem = isNormalProductByDurationSchedule(cacheItem) ? cacheItem : getProductExtend({
    cacheItem: cacheItem,
    state: state,
    isDisabledTime: true
  });
  return cacheItem;
};
export var formatDataKey = function formatDataKey(data) {
  var _data$option, _data$bundle;
  var _data = _objectSpread({}, data);
  var key = "".concat(_data.product_id);
  var rowKey = key;

  // 组合规格
  if (_data.product_variant_id) {
    var str = "_".concat(_data.product_variant_id);
    key += str;
    rowKey += str;
  }

  // 单规格
  if (_data !== null && _data !== void 0 && (_data$option = _data.option) !== null && _data$option !== void 0 && _data$option.length) {
    var _iterator = _createForOfIteratorHelper(_data.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 (_data !== null && _data !== void 0 && (_data$bundle = _data.bundle) !== null && _data$bundle !== void 0 && _data$bundle.length) {
    var _iterator2 = _createForOfIteratorHelper(_data.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 _str3 = "_".concat(opt.option_group_id, "_").concat(opt.product_option_item_id);
              key += _str3;
              rowKey += "".concat(_str3, "_").concat(opt.quantity || opt.num);
            }
          } catch (err) {
            _iterator3.e(err);
          } finally {
            _iterator3.f();
          }
        }
      }
    } catch (err) {
      _iterator2.e(err);
    } finally {
      _iterator2.f();
    }
  }

  // session time
  if (_data !== null && _data !== void 0 && _data.session) {
    var _data$session;
    var _str4 = "_".concat(_data === null || _data === void 0 || (_data$session = _data.session) === null || _data$session === void 0 ? void 0 : _data$session.key);
    key += _str4;
    rowKey += _str4;
  }
  if (_data !== null && _data !== void 0 && _data.schedule) {
    var _data$schedule;
    var _str5 = "_".concat(_data === null || _data === void 0 || (_data$schedule = _data.schedule) === null || _data$schedule === void 0 ? void 0 : _data$schedule.key);
    key += _str5;
    rowKey += _str5;
  }
  _data.key = key;
  _data.rowKey = rowKey;
  return _data;
};

// 纯组合规格
export var isVariant = function isVariant(item) {
  return (item === null || item === void 0 ? void 0 : item.variant_group_count) > 0 && (item === null || item === void 0 ? void 0 : item.option_group_count) === 0;
};

// 单规格
export var isOption = function isOption(item) {
  var _item$option_group;
  return (item === null || item === void 0 ? void 0 : item.option_group_count) > 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;
};

// 套餐
export var isBundle = function isBundle(item) {
  return (item === null || item === void 0 ? void 0 : item.bundle_group_count) > 0;
};
export var genValues = function genValues(item, code) {
  var _item$variant_group, _item$option_group2;
  var params = {
    bundle: [],
    option: [],
    // price,
    product_id: item.id,
    product_variant_id: 0,
    // product_option_string,
    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 (item) {
      return item.code === code || item.barcode === code;
    });

    // 格式化多规格分组标题
    var variant_group_title = formatVariantGroupTitle(variantItem === null || variantItem === void 0 ? void 0 : variantItem.title, (item === null || item === void 0 ? void 0 : item.variant_group) || []);
    params.price = Number((variantItem === null || variantItem === void 0 ? void 0 : variantItem.price) || 0);
    params.product_variant_id = variantItem.id;
    params.product_option_string = variant_group_title;
  }
  return formatDataKey(params);
};
var handleSetService = null;
export var setHandleSetService = function setHandleSetService(fn) {
  handleSetService = fn;
};
export var getHandleSetService = function getHandleSetService() {
  return handleSetService;
};

// 添加服务
var handleAddService = null;
export var setHandleAddService = function setHandleAddService(fn) {
  handleAddService = fn;
};
export var getHandleAddService = function getHandleAddService() {
  return handleAddService;
};