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); }
var _excluded = ["children"],
  _excluded2 = ["children", "index", "item"];
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); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { memo } from 'react';

// 网格配置类型定义

// 解析网格配置字符串
export var parseGridConfig = function parseGridConfig(config) {
  return config.split(':').map(function (num) {
    return parseInt(num, 10);
  }).filter(function (num) {
    return !isNaN(num);
  });
};

// 计算网格列宽度
var calculateColumnWidth = function calculateColumnWidth(columns, index) {
  var total = columns.reduce(function (sum, col) {
    return sum + col;
  }, 0);
  var percentage = columns[index] / total * 100;
  return "".concat(percentage, "%");
};

// 创建网格组件的工厂函数
export var createGridComponents = function createGridComponents(layout) {
  var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
    _ref$tabSwitchMode = _ref.tabSwitchMode,
    tabSwitchMode = _ref$tabSwitchMode === void 0 ? 'anchor' : _ref$tabSwitchMode,
    _ref$gap = _ref.gap,
    gap = _ref$gap === void 0 ? 12 : _ref$gap;
  // 兼容性处理：当传入空值时，默认为单列布局
  if (!layout) {
    layout = GRID_LAYOUTS.SINGLE_COLUMN;
  }
  var columns = parseGridConfig(layout.config);
  var isGrid = layout.grid && columns.length > 1;

  // 网格容器组件
  var GridListComponent = /*#__PURE__*/memo(function (_ref2) {
    var children = _ref2.children,
      props = _objectWithoutProperties(_ref2, _excluded);
    if (!isGrid) {
      return /*#__PURE__*/React.createElement("div", _extends({
        className: "product-list-wrapper",
        style: {
          display: 'flex',
          flexDirection: 'column',
          gap: gap
        }
      }, props), children);
    }
    return /*#__PURE__*/React.createElement("div", _extends({
      className: "product-grid-wrapper",
      style: _objectSpread({
        display: 'flex',
        flexWrap: 'wrap',
        gap: gap
      }, props.style)
    }, props), children);
  });

  /**
   * @title: 网格项组件
   * @description:
   *   - anchor模式：分类项带有锚点id用于滚动定位
   *   - switch模式：分类项不渲染（已在上层过滤，这里做兜底处理）
   */
  var GridItemComponent = /*#__PURE__*/memo(function (_ref3) {
    var children = _ref3.children,
      index = _ref3.index,
      item = _ref3.item,
      props = _objectWithoutProperties(_ref3, _excluded2);
    // 非网格布局（单列）
    if (!isGrid) {
      // 分类项处理
      if (item && item.__typename === 'category') {
        // switch模式下不渲染分类项（兜底逻辑，数据层已过滤）
        if (tabSwitchMode === 'switch') {
          return null;
        }
        // anchor模式下带锚点id
        return /*#__PURE__*/React.createElement("div", _extends({
          id: "ps_category_".concat(item.id)
        }, props), children);
      }
      return /*#__PURE__*/React.createElement("div", props, children);
    }

    // 网格布局：分类项占据整行
    if (item && item.__typename === 'category') {
      // switch模式下不渲染分类项（兜底逻辑，数据层已过滤）
      if (tabSwitchMode === 'switch') {
        return null;
      }
      // anchor模式下带锚点id
      return /*#__PURE__*/React.createElement("div", _extends({
        id: "ps_category_".concat(item.id),
        style: _objectSpread({
          width: '100%',
          flexShrink: 0
        }, props.style)
      }, props), children);
    }
    var columnIndex = index % columns.length;
    var width = calculateColumnWidth(columns, columnIndex);
    return /*#__PURE__*/React.createElement("div", _extends({
      style: _objectSpread({
        width: "calc(".concat(width, " - ").concat((columns.length - 1) * gap / columns.length, "px)"),
        flexShrink: 0
      }, props.style)
    }, props), children);
  });
  GridListComponent.displayName = "GridListComponent_".concat(layout.config);
  GridItemComponent.displayName = "GridItemComponent_".concat(layout.config);
  return {
    List: GridListComponent,
    Item: GridItemComponent
  };
};

// 预定义的常用网格配置
export var GRID_LAYOUTS = {
  SINGLE_COLUMN: {
    grid: false,
    config: '12'
  },
  TWO_COLUMNS: {
    grid: true,
    config: '12:12'
  },
  THREE_COLUMNS: {
    grid: true,
    config: '8:8:8'
  },
  FOUR_COLUMNS: {
    grid: true,
    config: '6:6:6:6'
  },
  UNEQUAL_TWO: {
    grid: true,
    config: '8:16'
  },
  // 1:2 比例
  UNEQUAL_THREE: {
    grid: true,
    config: '6:12:6'
  } // 1:2:1 比例
};

// 便捷函数：创建常用网格组件
export var createTwoColumnGrid = function createTwoColumnGrid() {
  return createGridComponents(GRID_LAYOUTS.TWO_COLUMNS);
};

// 自定义网格配置创建函数
export var createCustomGrid = function createCustomGrid(config) {
  return createGridComponents({
    grid: true,
    config: config
  });
};