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 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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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; }
/**
 * PisellTabbar 工具函数
 */

import { getText } from "../../../locales";

/**
 * 默认的特殊项配置
 */
var defaultItems = {
  all: {
    id: 0,
    shop_id: 9,
    parent_id: 0,
    name: 'pisell-tabbar-all',
    slug: 'All',
    icon: ''
  },
  search: {
    id: -1,
    shop_id: 9,
    parent_id: 0,
    name: 'pisell-tabbar-search',
    slug: 'Search',
    key: 'search',
    icon: '',
    customClick: true
  },
  expand: {
    id: -2,
    shop_id: 9,
    parent_id: 0,
    key: 'expand',
    icon: 'pisell2-expand-01',
    name: 'pisell-tabbar-expand',
    slug: 'expand',
    customClick: true
  }
};

/**
 * 向数据源添加特殊项（如 all、search、expand 按钮）
 * 
 * @param dataSource - 原始数据源
 * @param keys - 要添加的特殊项 key 数组
 * @returns 添加了特殊项的新数据源
 * 
 * @example
 * ```tsx
 * const newData = addDataSourceItemsByKey(categoryData, ['expand', 'all']);
 * // 返回: [expandItem, allItem, ...categoryData]
 * ```
 */
export var addDataSourceItemsByKey = function addDataSourceItemsByKey(dataSource, keys) {
  return [].concat(_toConsumableArray(keys.map(function (key) {
    return _objectSpread(_objectSpread({}, defaultItems[key]), {}, {
      name: getText(defaultItems[key].name)
    });
  })), _toConsumableArray(dataSource));
};

/**
 * 获取默认激活的 key 数组
 * 
 * @param dataSource - 数据源
 * @param rowKey - 用作唯一标识的字段名
 * @returns 默认激活的 key 数组（多层级）
 * 
 * @description
 * 默认选中第一项，如果第一项有子级，同时选中第一个子级
 * 
 * @example
 * ```tsx
 * const defaultKeys = getDefaultActiveKey(categoryData, 'id');
 * // 返回: [1, 0] (第一层级的第一项ID，第二层级的第一项ID)
 * ```
 */
export var getDefaultActiveKey = function getDefaultActiveKey(dataSource, rowKey) {
  var _activeKey = [];
  if (dataSource.length) {
    var _level1Item$children;
    var level1Item = dataSource[0];
    // 添加第一层级的第一项
    _activeKey.push(String(level1Item[rowKey]));

    // 如果第一项有子级，添加第二层级的第一项
    if ((_level1Item$children = level1Item.children) !== null && _level1Item$children !== void 0 && _level1Item$children.length) {
      if (level1Item.childrenMaxLevel !== undefined && level1Item.childrenMaxLevel < 2) {
        _activeKey.push(String(0));
      } else {
        var _level2Item$children;
        var level2Item = level1Item.children[0];
        _activeKey.push(String(level2Item[rowKey]));
        if ((_level2Item$children = level2Item.children) !== null && _level2Item$children !== void 0 && _level2Item$children.length) {
          _activeKey.push(String(0));
        }
      }
    }
  }
  return _activeKey;
};