"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Tree = exports.TreeBase = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const core_1 = require("../core");
const list_1 = require("../list");
const utils_1 = require("../utils");
const context_1 = require("./context");
const treeIcon_1 = __importDefault(require("./treeIcon"));
const treeItem_1 = __importDefault(require("./treeItem"));
const treeText_1 = __importDefault(require("./treeText"));
const useTreeState_1 = require("./useTreeState");
exports.TreeBase = core_1.styled.divBox `
  display: flex;
  height: auto;
  align-content: flex-start;
  flex-direction: column;
`;
/** Displays a tree of tree link items and optional heading. Exposes some props to the children via context. */
exports.Tree = (0, core_1.vui)((props, ref) => {
    const { activeItemId, children, className, iconCollapse, iconExpand, items, onClickItem, size, variant } = props, rest = __rest(props, ["activeItemId", "children", "className", "iconCollapse", "iconExpand", "items", "onClickItem", "size", "variant"]);
    const styles = (0, core_1.useStyleConfig)('Tree', props);
    const { itemsInternal, setItemsInternal, activeItemIndex, setActiveItemIndex } = (0, useTreeState_1.useTreeState)(activeItemId);
    const context = (0, react_1.useMemo)(() => (0, utils_1.filterUndefined)({ size, variant }), [size, variant]);
    const iconCollapseInternal = iconCollapse ? iconCollapse : variant === 'default' ? 'falAngleRight' : 'falMinus';
    const iconExpandInternal = iconExpand ? iconExpand : variant === 'default' ? 'falAngleDown' : 'falPlus';
    const transformChildrenToItems = (children) => {
        const t = [];
        if (children) {
            ;
            react_1.Children.toArray(children).forEach((child, index) => {
                var _a;
                if ((_a = child === null || child === void 0 ? void 0 : child.props) === null || _a === void 0 ? void 0 : _a.text) {
                    let items = [];
                    if (child.props.children) {
                        items = transformChildrenToItems(child.props.children);
                    }
                    t.push({
                        id: child.props.id,
                        text: child.props.text,
                        isActive: child.props.isActive,
                        items: child.props.items || items,
                        isCollapsed: child.props.isCollapsed,
                        disabled: child.props.disabled,
                        onClickTreeItem: onClickTreeItem,
                    });
                }
            });
        }
        return t;
    };
    (0, react_1.useEffect)(() => {
        if (items) {
            setItemsInternal(items);
            return;
        }
        const t = [];
        if (children) {
            const transformed = transformChildrenToItems(children);
            t.push(...transformed);
        }
        setItemsInternal(t);
        if (activeItemId !== undefined && (t === null || t === void 0 ? void 0 : t.find((i) => i.id === activeItemId))) {
            setActiveItemIndex(activeItemId);
        }
    }, [children, activeItemId]);
    const onClickTreeItem = (id) => {
        setActiveItemIndex(id);
        if (onClickItem) {
            onClickItem(id);
        }
    };
    return ((0, jsx_runtime_1.jsx)(context_1.TreeProvider, { value: context, children: (0, jsx_runtime_1.jsx)(exports.TreeBase, Object.assign({ className: (0, utils_1.cs)('vui-tree', className), ref: ref }, styles.container, rest, { children: (0, jsx_runtime_1.jsx)(list_1.List, { role: "tree", w: 1, children: itemsInternal === null || itemsInternal === void 0 ? void 0 : itemsInternal.map((_a, index) => {
                    var { id, isActive } = _a, props = __rest(_a, ["id", "isActive"]);
                    return ((0, jsx_runtime_1.jsx)(treeItem_1.default, Object.assign({ activeItemIndex: activeItemIndex, iconCollapse: iconCollapseInternal, iconExpand: iconExpandInternal, id: id, isActive: isActive ? isActive : activeItemIndex ? activeItemIndex === id : false, isFirstLevel: true, onClickTreeItem: onClickTreeItem }, props), id !== null && id !== void 0 ? id : index));
                }) }) })) }));
});
exports.Tree.Icon = treeIcon_1.default;
exports.Tree.Item = treeItem_1.default;
exports.Tree.Text = treeText_1.default;
exports.Tree.displayName = 'Tree';
exports.default = exports.Tree;
//# sourceMappingURL=tree.js.map