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; }
import React, { useCallback, useMemo } from 'react';
import classNames from 'classnames';
import { Row } from "./components";
import { useExpandedKeys } from "./hooks";
import "./PisellHierarchicalSummaryList.less";

/** 根节点类名，供上层用同一 DOM 包裹列表与其它区块时使用 */
export var PREFIX_CLS = 'pisell-hierarchical-summary-list';
function normalizeItems(items) {
  return items.filter(function (i) {
    return !i.hidden;
  }).slice().sort(function (a, b) {
    var _a$order, _b$order;
    return ((_a$order = a.order) !== null && _a$order !== void 0 ? _a$order : 0) - ((_b$order = b.order) !== null && _b$order !== void 0 ? _b$order : 0);
  });
}
function walkForDefaultExpandedKeys(options) {
  var items = options.items,
    defaultExpandedLevel = options.defaultExpandedLevel,
    getKey = options.getKey;
  if (!defaultExpandedLevel || defaultExpandedLevel <= 0) return [];
  var keys = [];
  var dfs = function dfs(list, level, parentPathKeys) {
    if (level > defaultExpandedLevel) return;
    var normalized = normalizeItems(list);
    normalized.forEach(function (item, index) {
      var _item$key, _item$children;
      var key = (_item$key = item.key) !== null && _item$key !== void 0 ? _item$key : getKey(item, {
        level: level,
        index: index,
        parentPathKeys: parentPathKeys
      });
      var children = normalizeItems((_item$children = item.children) !== null && _item$children !== void 0 ? _item$children : []);
      var hasChildren = children.length > 0;
      var canExpandThisLevel = level < defaultExpandedLevel;
      if (hasChildren && canExpandThisLevel) {
        keys.push(key);
        dfs(children, level + 1, [].concat(_toConsumableArray(parentPathKeys), [key]));
      }
    });
  };
  dfs(items, 1, []);
  return keys;
}
export var PisellHierarchicalSummaryList = /*#__PURE__*/React.memo(function (_ref) {
  var items = _ref.items,
    expandedKeys = _ref.expandedKeys,
    defaultExpandedKeys = _ref.defaultExpandedKeys,
    _ref$defaultExpandedL = _ref.defaultExpandedLevel,
    defaultExpandedLevel = _ref$defaultExpandedL === void 0 ? 0 : _ref$defaultExpandedL,
    onExpandedKeysChange = _ref.onExpandedKeysChange,
    _ref$accordion = _ref.accordion,
    accordion = _ref$accordion === void 0 ? false : _ref$accordion,
    maxRenderLevel = _ref.maxRenderLevel,
    maxExpandLevel = _ref.maxExpandLevel,
    _ref$indent = _ref.indent,
    indent = _ref$indent === void 0 ? 16 : _ref$indent,
    _ref$rowGap = _ref.rowGap,
    rowGap = _ref$rowGap === void 0 ? 8 : _ref$rowGap,
    padding = _ref.padding,
    gaps = _ref.gaps,
    levelConfig = _ref.levelConfig,
    renderLabel = _ref.renderLabel,
    renderValue = _ref.renderValue,
    getKeyProp = _ref.getKey,
    aggregate = _ref.aggregate,
    className = _ref.className,
    style = _ref.style,
    _ref$renderRoot = _ref.renderRoot,
    renderRoot = _ref$renderRoot === void 0 ? true : _ref$renderRoot;
  var rootStyle = useMemo(function () {
    return _objectSpread(_objectSpread({}, style), padding !== undefined ? {
      padding: typeof padding === 'number' ? "".concat(padding, "px") : padding
    } : {});
  }, [padding, style]);
  var getKey = useCallback(function (item, ctx) {
    if (item.key !== undefined && item.key !== null) return item.key;
    if (getKeyProp) return getKeyProp(item, ctx);
    var parent = ctx.parentPathKeys.map(String).join('/');
    return "lvl".concat(ctx.level, ":").concat(parent ? "".concat(parent, "/") : '').concat(ctx.index);
  }, [getKeyProp]);
  var computedDefaultExpandedKeys = useMemo(function () {
    return walkForDefaultExpandedKeys({
      items: items,
      defaultExpandedLevel: defaultExpandedLevel,
      getKey: getKey
    });
  }, [defaultExpandedLevel, getKey, items]);
  var mergedDefaultExpandedKeys = useMemo(function () {
    var merged = new Set();
    (defaultExpandedKeys !== null && defaultExpandedKeys !== void 0 ? defaultExpandedKeys : []).forEach(function (k) {
      return merged.add(k);
    });
    computedDefaultExpandedKeys.forEach(function (k) {
      return merged.add(k);
    });
    return Array.from(merged);
  }, [computedDefaultExpandedKeys, defaultExpandedKeys]);
  var expandedController = useExpandedKeys({
    expandedKeys: expandedKeys,
    defaultExpandedKeys: mergedDefaultExpandedKeys,
    onExpandedKeysChange: onExpandedKeysChange
  });
  var normalizedRootItems = useMemo(function () {
    return normalizeItems(items);
  }, [items]);
  var toggleExpanded = useCallback(function (key, options) {
    var _options$siblingKeys;
    var prev = expandedController.expandedKeys;
    var set = new Set(prev);
    var isExpanded = set.has(key);
    if (isExpanded) {
      set.delete(key);
      expandedController.setExpandedKeys(Array.from(set));
      return;
    }
    if (accordion && options !== null && options !== void 0 && (_options$siblingKeys = options.siblingKeys) !== null && _options$siblingKeys !== void 0 && _options$siblingKeys.length) {
      options.siblingKeys.forEach(function (k) {
        return set.delete(k);
      });
    }
    set.add(key);
    expandedController.setExpandedKeys(Array.from(set));
  }, [accordion, expandedController]);
  var rowsContent = normalizedRootItems.map(function (item, index) {
    var _item$key2;
    var rootLevel = index === 0 ? 1 : 2;
    return /*#__PURE__*/React.createElement(Row, {
      key: (_item$key2 = item.key) !== null && _item$key2 !== void 0 ? _item$key2 : getKey(item, {
        level: rootLevel,
        index: index,
        parentPathKeys: []
      }),
      prefixCls: PREFIX_CLS,
      item: item,
      level: rootLevel,
      index: index,
      parentPathKeys: [],
      expandedKeySet: expandedController.expandedKeySet,
      toggleExpanded: toggleExpanded,
      getKey: getKey,
      levelConfig: levelConfig,
      indent: indent,
      rowGap: rowGap,
      gaps: gaps,
      maxRenderLevel: maxRenderLevel,
      maxExpandLevel: maxExpandLevel,
      aggregate: aggregate,
      renderLabel: renderLabel,
      renderValue: renderValue
    });
  });
  if (renderRoot === false) {
    return /*#__PURE__*/React.createElement("div", {
      style: rootStyle
    }, rowsContent);
  }
  return /*#__PURE__*/React.createElement("div", {
    className: classNames(PREFIX_CLS, className),
    style: rootStyle
  }, rowsContent);
});
PisellHierarchicalSummaryList.displayName = 'PisellHierarchicalSummaryList';