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); }
import React, { useCallback, useEffect, useMemo } from 'react';
import classNames from 'classnames';
import { TabbarLevel } from "./components";
import { useActiveKey, useExpand } from "./hooks";
import { addDataSourceItemsByKey, getDefaultActiveKey } from "./utils";
import { DEFAULT_LEVEL_CONFIG, LEVEL_ADD_ITEMS } from "./constants";
import mock from "./mock";
import "./PisellTabbar.less";

/**
 * PisellTabbar - 多层级 Tabbar 组件
 *
 * 支持最多 3 层级的 Tab 导航，每层级可独立展开/收起
 *
 * @component
 * @example
 * ```tsx
 * <PisellTabbar
 *   dataSource={categoryData}
 *   value={[1, 10, 100]}
 *   onChange={(value) => console.log('当前选中:', value)}
 * />
 * ```
 */
var PisellTabbar = function PisellTabbar(props) {
  var _props$dataSource = props.dataSource,
    dataSource = _props$dataSource === void 0 ? props !== null && props !== void 0 && props.__designMode ? mock : mock : _props$dataSource,
    value = props.value,
    onChange = props.onChange,
    className = props.className,
    style = props.style,
    customLevelConfig = props.levelConfig,
    expandConfig = props.expandConfig,
    _props$maxLevel = props.maxLevel,
    maxLevel = _props$maxLevel === void 0 ? 3 : _props$maxLevel;

  // 激活的 key 管理
  var _useActiveKey = useActiveKey({
      initialActiveKey: value || getDefaultActiveKey(dataSource, 'id'),
      onChange: onChange
    }),
    setActiveKey = _useActiveKey.setActiveKey,
    getActiveKey = _useActiveKey.getActiveKey,
    _setActiveKey = _useActiveKey._setActiveKey,
    panelVisible = _useActiveKey.panelVisible,
    activeKey = _useActiveKey.activeKey;
  // 展开状态管理
  var _useExpand = useExpand(),
    expand = _useExpand.expand,
    toggleExpand = _useExpand.toggleExpand,
    getExpand = _useExpand.getExpand;
  console.log('panelVisiblepanelVisible', panelVisible);
  /**
   * 同步外部 value 到内部状态
   */
  useEffect(function () {
    if (value) {
      _setActiveKey(value);
    }
  }, [JSON.stringify(value)]);

  /**
   * 获取层级配置
   */
  var getLevelConfig = useCallback(function (level) {
    return _objectSpread(_objectSpread({}, DEFAULT_LEVEL_CONFIG[level]), customLevelConfig === null || customLevelConfig === void 0 ? void 0 : customLevelConfig[level]);
  }, [customLevelConfig]);

  /**
   * 处理一级 Tab 切换
   */
  var handleLevel1Change = useCallback(function (key, item) {
    if (item.childrenMaxLevel !== undefined && item.childrenMaxLevel < 2) {
      setActiveKey(0, key, 0);
    } else {
      var _item$children, _item$children$;
      setActiveKey(0, key, item !== null && item !== void 0 && (_item$children = item.children) !== null && _item$children !== void 0 && _item$children.length ? item === null || item === void 0 || (_item$children$ = item.children[0]) === null || _item$children$ === void 0 ? void 0 : _item$children$.id : undefined);
    }
  }, [setActiveKey]);

  /**
   * 处理二级 Tab 切换
   */
  var handleLevel2Change = useCallback(function (key, item) {
    var _item$children2;
    setActiveKey(1, key, item !== null && item !== void 0 && (_item$children2 = item.children) !== null && _item$children2 !== void 0 && _item$children2.length ? 0 : undefined);
  }, [setActiveKey]);

  /**
   * 处理三级 Tab 切换
   */
  var handleLevel3Change = useCallback(function (key) {
    setActiveKey(2, key);
  }, [setActiveKey]);

  /**
   * 渲染一级 Tabbar
   */
  var level1DataSource = useMemo(function () {
    return addDataSourceItemsByKey(dataSource, LEVEL_ADD_ITEMS.level1);
  }, [dataSource]);

  /**
   * 渲染二级 Tabbar
   */
  var renderLevel2 = useCallback(function (panelProps) {
    if (maxLevel < 2) return null;
    var childrenMaxLevel = panelProps.dataSource.childrenMaxLevel || 2;

    // 如果子级最大层级小于2，则渲染三级 Tabbar 样式
    if (childrenMaxLevel < 2) {
      return renderLevel3(panelProps, true);
    }
    var _dataSource = panelProps.dataSource.children || [];
    if (_dataSource.length) {
      _dataSource = addDataSourceItemsByKey(_dataSource, LEVEL_ADD_ITEMS.level2);
    }
    return /*#__PURE__*/React.createElement(TabbarLevel, {
      skuId: "sku-list-tabbar-".concat(panelProps.dataSource.id),
      dataSource: _dataSource,
      activePanelKey: getActiveKey(1),
      level: "level2",
      setActivePanelKey: handleLevel2Change,
      expand: getExpand('level2'),
      handleExpand: function handleExpand() {
        return toggleExpand('level2');
      },
      renderChildren: renderLevel3,
      levelConfig: getLevelConfig('level2'),
      expandConfig: expandConfig,
      positionId: getLevelConfig('level2').positionId,
      superTabsProps: {
        className: 'pisell-tabbar-level-2',
        style: {
          width: '104px'
        },
        gridProps: {
          direction: 'vertical',
          style: {
            padding: '16px 4px'
          },
          gap: 12
        },
        startSlot: null,
        scrollViewProps: {
          overflow: 'y',
          width: '100%',
          height: '100%',
          style: {
            padding: 0
          },
          scrollActionsConfig: {
            type: 'tbArrows',
            show: true
          }
        }
      }
    });
  }, [maxLevel, getActiveKey, handleLevel2Change, getExpand, toggleExpand, getLevelConfig, expandConfig]);

  /**
   * 渲染三级 Tabbar
   */
  var renderLevel3 = useCallback(function (panelProps2, isLevel2) {
    if (maxLevel < 3) return null;
    var _dataSource2 = panelProps2.dataSource.children || [];
    if (_dataSource2.length) {
      _dataSource2 = addDataSourceItemsByKey(_dataSource2, LEVEL_ADD_ITEMS.level3);
    }
    return /*#__PURE__*/React.createElement(TabbarLevel, {
      skuId: "sku-list-tabbar-".concat(panelProps2.dataSource.id),
      dataSource: _dataSource2,
      activePanelKey: getActiveKey(isLevel2 ? 1 : 2),
      level: 'level3',
      setActivePanelKey: isLevel2 ? handleLevel2Change : handleLevel3Change,
      expand: getExpand(isLevel2 ? 'level2' : 'level3'),
      handleExpand: function handleExpand() {
        return toggleExpand(isLevel2 ? 'level2' : 'level3');
      },
      levelConfig: getLevelConfig('level3'),
      expandConfig: expandConfig,
      positionId: getLevelConfig(isLevel2 ? 'level2' : 'level3').positionId
    });
  }, [maxLevel, getActiveKey, handleLevel3Change, getExpand, toggleExpand, getLevelConfig, expandConfig]);
  useEffect(function () {
    document.body.id = 'body';
  }, []);
  console.log('activeKeyactiveKey', activeKey);
  return dataSource.length ? /*#__PURE__*/React.createElement("div", {
    className: classNames('pisell-tabbar2', className),
    style: style
  }, /*#__PURE__*/React.createElement(TabbarLevel, {
    skuId: "sku-list-tabbar",
    dataSource: level1DataSource,
    activePanelKey: getActiveKey(0),
    level: "level1",
    setActivePanelKey: handleLevel1Change,
    expand: getExpand('level1'),
    handleExpand: function handleExpand() {
      return toggleExpand('level1');
    },
    renderChildren: renderLevel2,
    levelConfig: getLevelConfig('level1'),
    expandConfig: expandConfig,
    superTabsProps: {
      gridProps: {
        direction: 'vertical',
        style: {
          padding: '16px 4px'
        },
        gap: 12
      },
      panelStyle: {
        position: 'absolute',
        top: 40,
        right: 0,
        height: 'calc(100% - 40px)',
        transition: '0.3s ease-in-out'
      },
      startSlot: null,
      scrollViewProps: {
        overflow: 'y',
        width: '100%',
        height: '100%',
        style: {
          padding: 0
        },
        scrollActionsConfig: {
          type: 'tbArrows',
          show: true
        }
      }
    },
    positionId: getLevelConfig('level1').positionId,
    level2PositionId: getLevelConfig('level2').positionId,
    panelVisible: panelVisible
  })) : null;
};
PisellTabbar.displayName = 'PisellTabbar';
PisellTabbar.getDefaultActiveKey = getDefaultActiveKey;

// const Demo = () => {
//   return (
//     <div style={{ height: '100vh', width: '100%', display: 'flex' }}>
//       <div className="" style={{ height: '100%', width: 'calc(100% - 100px)' }}>
//         <div className="tabbar-level-3-wrap" style={{ width: '100%' }}></div>
//         <div className="" style={{ height: '400px' }}>
//           content
//         </div>
//       </div>
//       <div
//         className=""
//         style={{
//           height: '100%',
//           width: '100px',
//           display: 'flex',
//           alignItems: 'flex-start',
//         }}
//       >
//         <div
//           className="tabbar-level-2-wrap"
//           style={{
//             height: '100%',
//             position: 'relative',
//           }}
//         ></div>
//         <div className="" style={{ height: '100%' }}>
//           <PisellTabbar __designMode />
//         </div>
//       </div>
//     </div>
//   );
// };

export default PisellTabbar;