"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
require("./index.scss");
var _index2 = require("../collapse-selector-item/index");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var CollapseSelector = function CollapseSelector(props) {
  var children = props.children;
  // 设置默认值为 null，之后通过 useEffect 设置为第一项
  var _React$useState = _react["default"].useState(null),
    activeKey = _React$useState[0],
    setActiveKey = _React$useState[1];
  var handleItemClick = function handleItemClick(key) {
    setActiveKey(activeKey === key ? null : key);
  };

  // 找到第一个子项的 key 并设置为默认打开
  (0, _react.useEffect)(function () {
    if (children) {
      var firstKey = null;
      _react.Children.forEach(children, function (child) {
        if (/*#__PURE__*/(0, _react.isValidElement)(child) && child.type === _index2.CollapseSelectorItem && !firstKey) {
          firstKey = child.props.key || '';
        }
      });
      if (firstKey) {
        setActiveKey(firstKey);
      }
    } else if (props.dataList && props.dataList.length > 0) {
      // 如果使用 dataList，找到第一个项目
      var _firstKey = props.dataList[0].key || '';
      if (_firstKey) {
        setActiveKey(_firstKey);
      }
    }
  }, [children, props.dataList]);

  // 处理低代码引擎中拖放的子组件
  if (children) {
    return /*#__PURE__*/(0, _react.createElement)('div', {
      className: 'collapse-selector'
    }, _react.Children.map(children, function (child) {
      if (/*#__PURE__*/(0, _react.isValidElement)(child) && child.type === _index2.CollapseSelectorItem) {
        var childKey = child.props.key || '';
        var isActive = activeKey === childKey;
        return /*#__PURE__*/(0, _react.createElement)('div', {
          key: childKey,
          className: 'collapse-selector-item'
        }, [/*#__PURE__*/(0, _react.createElement)('div', {
          className: 'collapse-selector-header',
          onClick: function onClick() {
            return handleItemClick(childKey);
          }
        }, [/*#__PURE__*/(0, _react.createElement)('div', {
          className: 'collapse-selector-title'
        }, [child.props.icon && /*#__PURE__*/(0, _react.createElement)('i', {
          className: "collapse-selector-icon " + child.props.icon
        }), /*#__PURE__*/(0, _react.createElement)('span', null, child.props.title)]), /*#__PURE__*/(0, _react.createElement)('div', {
          className: "collapse-selector-indicator " + (isActive ? 'active' : '')
        })]), isActive && /*#__PURE__*/(0, _react.createElement)('div', {
          className: 'collapse-selector-content'
        }, child)]);
      }
      return null;
    }));
  }

  // 兼容原有的 dataList 方式（可以删除或保留以保持向后兼容）
  var _props$dataList = props.dataList,
    dataList = _props$dataList === void 0 ? [] : _props$dataList;
  return /*#__PURE__*/(0, _react.createElement)('div', {
    className: 'collapse-selector'
  }, dataList.map(function (item) {
    return /*#__PURE__*/(0, _react.createElement)('div', {
      key: item.key,
      className: 'collapse-selector-item'
    }, [/*#__PURE__*/(0, _react.createElement)('div', {
      className: 'collapse-selector-header',
      onClick: function onClick() {
        return handleItemClick(item.key || '');
      }
    }, [/*#__PURE__*/(0, _react.createElement)('div', {
      className: 'collapse-selector-title'
    }, [item.icon && /*#__PURE__*/(0, _react.createElement)('i', {
      className: "collapse-selector-icon " + item.icon
    }), /*#__PURE__*/(0, _react.createElement)('span', null, item.title)]), /*#__PURE__*/(0, _react.createElement)('div', {
      className: "collapse-selector-indicator " + (activeKey === item.key ? 'active' : '')
    })]), item.isContent && activeKey === item.key && /*#__PURE__*/(0, _react.createElement)('div', {
      className: 'collapse-selector-content'
    }, /*#__PURE__*/(0, _react.createElement)(_index2.CollapseSelectorItem, {
      title: item.title,
      key: item.key
    }))]);
  }));
};
CollapseSelector.displayName = 'CollapseSelector';
var _default = exports["default"] = CollapseSelector;