/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

import { __extends, __assign, __rest } from 'tslib';
import React__default from 'react';
import { mapTree, themeable } from 'amis-core';

/**
 * @file AsideNav
 * @description 左侧导航。
 * @author fex
 */
var AsideNav = /** @class */ (function (_super) {
    __extends(AsideNav, _super);
    function AsideNav(props) {
        var _this = _super.call(this, props) || this;
        var isOpen = props.isOpen;
        var id = 1;
        _this.state = {
            navigations: mapTree(props.navigations, function (item) {
                var isActive = typeof item.active === 'undefined'
                    ? props.isActive(item)
                    : item.active;
                return __assign(__assign({}, item), { id: id++, active: isActive, open: isActive || isOpen(item) });
            }, 1, true)
        };
        _this.renderLink = _this.renderLink.bind(_this);
        _this.toggleExpand = _this.toggleExpand.bind(_this);
        return _this;
    }
    AsideNav.prototype.componentDidUpdate = function (prevProps) {
        var props = this.props;
        var isOpen = prevProps.isOpen;
        if (prevProps.navigations !== props.navigations ||
            prevProps.isActive !== props.isActive) {
            var id_1 = 1;
            this.setState({
                navigations: mapTree(props.navigations, function (item) {
                    var isActive = typeof item.active === 'undefined'
                        ? props.isActive(item)
                        : item.active;
                    return __assign(__assign({}, item), { id: id_1++, active: isActive, open: isActive || isOpen(item) });
                }, 1, true)
            });
        }
    };
    AsideNav.prototype.toggleExpand = function (link, e) {
        if (e) {
            e.stopPropagation();
            e.preventDefault();
        }
        this.setState({
            navigations: mapTree(this.state.navigations, function (item) { return (__assign(__assign({}, item), { open: link.id === item.id ? !item.open : item.open })); }, 1, true)
        });
    };
    AsideNav.prototype.renderLink = function (link, key, props, depth) {
        var _a;
        if (props === void 0) { props = {}; }
        if (depth === void 0) { depth = 1; }
        var _b = this.props, renderLink = _b.renderLink; _b.isActive; var renderSubLinks = _b.renderSubLinks, cx = _b.classnames, others = __rest(_b, ["renderLink", "isActive", "renderSubLinks", "classnames"]);
        var dom = renderLink(__assign({ link: link, active: link.active, open: link.open, toggleExpand: this.toggleExpand, depth: depth, classnames: cx, subHeader: key === 'subHeader' }, others));
        if (!dom) {
            return;
        }
        else if (key === 'subHeader') {
            return React__default.cloneElement(dom, {
                key: key
            });
        }
        return (React__default.createElement("li", __assign({}, props, { key: key, className: cx("AsideNav-item", link.className, (_a = {},
                _a["is-open"] = link.open,
                _a["is-active"] = link.active,
                _a)) }),
            dom,
            renderSubLinks(link, this.renderLink, depth, this.props)));
    };
    AsideNav.prototype.render = function () {
        var _this = this;
        var navigations = this.state.navigations;
        var links = [];
        var _a = this.props, className = _a.className, cx = _a.classnames;
        navigations.forEach(function (navigation, index) {
            if (!Array.isArray(navigation.children)) {
                return;
            }
            if (navigation.prefix) {
                var prefix = typeof navigation.prefix === 'function'
                    ? navigation.prefix(_this.props)
                    : navigation.prefix;
                links.push(React__default.cloneElement(prefix, __assign(__assign({}, prefix.props), { key: "".concat(index, "-prefix") })));
            }
            navigation.label &&
                links.push(React__default.createElement("li", { key: "".concat(index, "-label"), className: cx("AsideNav-label", navigation.className) },
                    React__default.createElement("span", null, navigation.label)));
            navigation.children.forEach(function (item, key) {
                var link = _this.renderLink(item, "".concat(index, "-").concat(key));
                link && links.push(link);
            });
            if (navigation.affix) {
                var affix = typeof navigation.affix === 'function'
                    ? navigation.affix(_this.props)
                    : navigation.affix;
                links.push(React__default.cloneElement(affix, __assign(__assign({}, affix.props), { key: "".concat(index, "-affix") })));
            }
        });
        return (React__default.createElement("nav", { className: cx("AsideNav", className) },
            React__default.createElement("ul", { className: cx("AsideNav-list") }, links)));
    };
    AsideNav.defaultProps = {
        renderLink: function (item) {
            var _a;
            return (React__default.createElement("a", __assign({}, (_a = item.testIdBuilder) === null || _a === void 0 ? void 0 : _a.getTestId()), item.label));
        },
        renderSubLinks: function (link, renderLink, depth, _a) {
            var cx = _a.classnames;
            return link.children && link.children.length ? (React__default.createElement("ul", { className: cx('AsideNav-subList') },
                link.label ? (React__default.createElement("li", { key: "subHeader", className: cx('AsideNav-subHeader') }, renderLink(__assign(__assign({}, link), { children: undefined }), 'subHeader', {}, depth))) : null,
                link.children.map(function (link, key) {
                    return renderLink(link, key, {}, depth + 1);
                }))) : link.label && depth === 1 ? (React__default.createElement("div", { className: cx('AsideNav-tooltip') }, link.label)) : null;
        },
        isActive: function (link) { return link.open; },
        isOpen: function (item) {
            return item.children ? item.children.some(function (item) { return item.open; }) : false;
        }
    };
    return AsideNav;
}(React__default.Component));
var AsideNav$1 = themeable(AsideNav);

export { AsideNav, AsideNav$1 as default };
