var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pisellHierarchicalSummaryList/PisellHierarchicalSummaryList.tsx
var PisellHierarchicalSummaryList_exports = {};
__export(PisellHierarchicalSummaryList_exports, {
  PREFIX_CLS: () => PREFIX_CLS,
  PisellHierarchicalSummaryList: () => PisellHierarchicalSummaryList
});
module.exports = __toCommonJS(PisellHierarchicalSummaryList_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_components = require("./components");
var import_hooks = require("./hooks");
var import_PisellHierarchicalSummaryList = require("./PisellHierarchicalSummaryList.less");
var PREFIX_CLS = "pisell-hierarchical-summary-list";
function normalizeItems(items) {
  return items.filter((i) => !i.hidden).slice().sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
}
function walkForDefaultExpandedKeys(options) {
  const { items, defaultExpandedLevel, getKey } = options;
  if (!defaultExpandedLevel || defaultExpandedLevel <= 0) return [];
  const keys = [];
  const dfs = (list, level, parentPathKeys) => {
    if (level > defaultExpandedLevel) return;
    const normalized = normalizeItems(list);
    normalized.forEach((item, index) => {
      const key = item.key ?? getKey(item, { level, index, parentPathKeys });
      const children = normalizeItems(item.children ?? []);
      const hasChildren = children.length > 0;
      const canExpandThisLevel = level < defaultExpandedLevel;
      if (hasChildren && canExpandThisLevel) {
        keys.push(key);
        dfs(children, level + 1, [...parentPathKeys, key]);
      }
    });
  };
  dfs(items, 1, []);
  return keys;
}
var PisellHierarchicalSummaryList = import_react.default.memo(
  ({
    items,
    expandedKeys,
    defaultExpandedKeys,
    defaultExpandedLevel = 0,
    onExpandedKeysChange,
    accordion = false,
    maxRenderLevel,
    maxExpandLevel,
    indent = 16,
    rowGap = 8,
    padding,
    gaps,
    levelConfig,
    renderLabel,
    renderValue,
    getKey: getKeyProp,
    aggregate,
    className,
    style,
    renderRoot = true
  }) => {
    const rootStyle = (0, import_react.useMemo)(
      () => ({
        ...style,
        ...padding !== void 0 ? {
          padding: typeof padding === "number" ? `${padding}px` : padding
        } : {}
      }),
      [padding, style]
    );
    const getKey = (0, import_react.useCallback)(
      (item, ctx) => {
        if (item.key !== void 0 && item.key !== null) return item.key;
        if (getKeyProp) return getKeyProp(item, ctx);
        const parent = ctx.parentPathKeys.map(String).join("/");
        return `lvl${ctx.level}:${parent ? `${parent}/` : ""}${ctx.index}`;
      },
      [getKeyProp]
    );
    const computedDefaultExpandedKeys = (0, import_react.useMemo)(
      () => walkForDefaultExpandedKeys({ items, defaultExpandedLevel, getKey }),
      [defaultExpandedLevel, getKey, items]
    );
    const mergedDefaultExpandedKeys = (0, import_react.useMemo)(() => {
      const merged = /* @__PURE__ */ new Set();
      (defaultExpandedKeys ?? []).forEach((k) => merged.add(k));
      computedDefaultExpandedKeys.forEach((k) => merged.add(k));
      return Array.from(merged);
    }, [computedDefaultExpandedKeys, defaultExpandedKeys]);
    const expandedController = (0, import_hooks.useExpandedKeys)({
      expandedKeys,
      defaultExpandedKeys: mergedDefaultExpandedKeys,
      onExpandedKeysChange
    });
    const normalizedRootItems = (0, import_react.useMemo)(() => normalizeItems(items), [items]);
    const toggleExpanded = (0, import_react.useCallback)(
      (key, options) => {
        var _a;
        const prev = expandedController.expandedKeys;
        const set = new Set(prev);
        const isExpanded = set.has(key);
        if (isExpanded) {
          set.delete(key);
          expandedController.setExpandedKeys(Array.from(set));
          return;
        }
        if (accordion && ((_a = options == null ? void 0 : options.siblingKeys) == null ? void 0 : _a.length)) {
          options.siblingKeys.forEach((k) => set.delete(k));
        }
        set.add(key);
        expandedController.setExpandedKeys(Array.from(set));
      },
      [accordion, expandedController]
    );
    const rowsContent = normalizedRootItems.map((item, index) => {
      const rootLevel = index === 0 ? 1 : 2;
      return /* @__PURE__ */ import_react.default.createElement(
        import_components.Row,
        {
          key: item.key ?? getKey(item, { level: rootLevel, index, parentPathKeys: [] }),
          prefixCls: PREFIX_CLS,
          item,
          level: rootLevel,
          index,
          parentPathKeys: [],
          expandedKeySet: expandedController.expandedKeySet,
          toggleExpanded,
          getKey,
          levelConfig,
          indent,
          rowGap,
          gaps,
          maxRenderLevel,
          maxExpandLevel,
          aggregate,
          renderLabel,
          renderValue
        }
      );
    });
    if (renderRoot === false) {
      return /* @__PURE__ */ import_react.default.createElement("div", { style: rootStyle }, rowsContent);
    }
    return /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)(PREFIX_CLS, className), style: rootStyle }, rowsContent);
  }
);
PisellHierarchicalSummaryList.displayName = "PisellHierarchicalSummaryList";
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  PREFIX_CLS,
  PisellHierarchicalSummaryList
});
