function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
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 _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { locales } from '@pisell/utils';

/**
 * 格式化 SKU 数据源，适配 SkuCard 组件
 */
export var formatSkuDataSource = function formatSkuDataSource(dataSource) {
  return {
    id: dataSource.id,
    title: dataSource.title,
    subtitle: dataSource.subtitle,
    cover: dataSource.cover,
    price: dataSource.price,
    original_price: dataSource.original_price,
    stock: dataSource.stock_quantity,
    sortKeys: ['title', 'subtitle', 'price']
  };
};

/**
 * Tab 项接口定义（与 List 组件保持一致）
 */

/**
 * 产品分类信息接口
 */

/**
 * 产品项接口
 */

/**
 * 根据产品数据生成分类 Tabs（Switch 模式）
 * 参考 TestResponsiveWrapper 的 generateSimpleTabItems 函数
 * 
 * @param products 产品列表
 * @param tabStyle tab 样式类型
 * @returns TabItem 数组
 */
export var generateProductTabs = function generateProductTabs(products) {
  var tabStyle = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'switch';
  if (!products || products.length === 0) {
    return [{
      key: 'all',
      label: locales.getText('pisell2.ticket-booking.all')
    }];
  }
  if (tabStyle === 'switch') {
    // 切换模式：提取产品的分类信息，不显示动态数量
    var categoryMap = new Map();

    // 收集所有分类
    products.forEach(function (product) {
      if (product.category && Array.isArray(product.category)) {
        product.category.forEach(function (cat) {
          if (!categoryMap.has(cat.id)) {
            categoryMap.set(cat.id, cat);
          }
        });
      }
    });

    // 按 sort 字段排序（降序，sort 越大越靠前）
    var sortedCategories = Array.from(categoryMap.values()).sort(function (a, b) {
      return (b.sort || 0) - (a.sort || 0);
    });

    // 生成 tabs
    var tabs = [{
      key: 'all',
      label: locales.getText('pisell2.ticket-booking.all')
    }];
    sortedCategories.forEach(function (category) {
      tabs.push({
        key: String(category.id),
        label: category.name,
        group: String(category.id)
      });
    });
    return tabs;
  } else {
    // 锚点模式：基于实际数据统计数量
    var categoryCount = products.reduce(function (acc, product) {
      if (product.category && Array.isArray(product.category)) {
        product.category.forEach(function (cat) {
          var key = String(cat.id);
          if (!acc[key]) {
            acc[key] = {
              count: 0,
              category: cat
            };
          }
          acc[key].count += 1;
        });
      }
      return acc;
    }, {});

    // 生成 tabs
    var _tabs = [{
      key: 'all',
      label: "".concat(locales.getText('pisell2.ticket-booking.all'), " (").concat(products.length, ")")
    }];

    // 按 sort 排序
    var sortedEntries = Object.entries(categoryCount).sort(function (_ref, _ref2) {
      var _ref3 = _slicedToArray(_ref, 2),
        a = _ref3[1];
      var _ref4 = _slicedToArray(_ref2, 2),
        b = _ref4[1];
      return (b.category.sort || 0) - (a.category.sort || 0);
    });
    sortedEntries.forEach(function (_ref5) {
      var _ref6 = _slicedToArray(_ref5, 2),
        key = _ref6[0],
        _ref6$ = _ref6[1],
        count = _ref6$.count,
        category = _ref6$.category;
      _tabs.push({
        key: key,
        label: "".concat(category.name, " (").concat(count, ")"),
        group: key
      });
    });
    return _tabs;
  }
};