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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var React = require('react');
var amisCore = require('amis-core');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var AsideNav = /** @class */ (function (_super) {
    tslib.__extends(AsideNav, _super);
    function AsideNav(props) {
        var _this = _super.call(this, props) || this;
        var isOpen = props.isOpen;
        var id = 1;
        _this.state = {
            navigations: amisCore.mapTree(props.navigations, function (item) {
                var isActive = typeof item.active === 'undefined'
                    ? props.isActive(item)
                    : item.active;
                return tslib.__assign(tslib.__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: amisCore.mapTree(props.navigations, function (item) {
                    var isActive = typeof item.active === 'undefined'
                        ? props.isActive(item)
                        : item.active;
                    return tslib.__assign(tslib.__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: amisCore.mapTree(this.state.navigations, function (item) { return (tslib.__assign(tslib.__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 = tslib.__rest(_b, ["renderLink", "isActive", "renderSubLinks", "classnames"]);
        var dom = renderLink(tslib.__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["default"].cloneElement(dom, {
                key: key
            });
        }
        return (_J$X_("li", tslib.__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["default"].cloneElement(prefix, tslib.__assign(tslib.__assign({}, prefix.props), { key: "".concat(index, "-prefix") })));
            }
            navigation.label &&
                links.push(_J$X_("li", { key: "".concat(index, "-label"), className: cx("AsideNav-label", navigation.className) },
                    _J$X_("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["default"].cloneElement(affix, tslib.__assign(tslib.__assign({}, affix.props), { key: "".concat(index, "-affix") })));
            }
        });
        return (_J$X_("nav", { className: cx("AsideNav", className) },
            _J$X_("ul", { className: cx("AsideNav-list") }, links)));
    };
    AsideNav.defaultProps = {
        renderLink: function (item) {
            var _a;
            return (_J$X_("a", tslib.__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 ? (_J$X_("ul", { className: cx('AsideNav-subList') },
                link.label ? (_J$X_("li", { key: "subHeader", className: cx('AsideNav-subHeader') }, renderLink(tslib.__assign(tslib.__assign({}, link), { children: undefined }), 'subHeader', {}, depth))) : null,
                link.children.map(function (link, key) {
                    return renderLink(link, key, {}, depth + 1);
                }))) : link.label && depth === 1 ? (_J$X_("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["default"].Component));
var AsideNav$1 = amisCore.themeable(AsideNav);

exports.AsideNav = AsideNav;
exports["default"] = AsideNav$1;
