import { __assign, __rest } from "tslib";
import React, { useState } from 'react';
import { Nav } from '@alicloud/console-components';
import classnames from 'classnames';
import Header from './Header';
var PrimaryMenu = function (props) {
    var className = props.className, rest = __rest(props, ["className"]);
    return (React.createElement(Nav, __assign({ className: classnames("lp-primary-menu", className), openMode: "multiple" }, rest, { type: "normal", direction: "ver", activeDirection: null, mode: "inline", triggerType: "click", inlineIndent: 16, popupAlign: "follow", hasArrow: true })));
};
var SecondaryMenu = function (props) {
    var className = props.className, rest = __rest(props, ["className"]);
    return (React.createElement(Nav, __assign({}, rest, { type: "line", className: classnames("lp-sencondary-menu", className), direction: "ver", activeDirection: "right", triggerType: "click", inlineIndent: 8 })));
};
function mapItemToJSX(item) {
    var key = item.key, label = item.label, _a = item.disabled, disabled = _a === void 0 ? false : _a, _b = item.visible, visible = _b === void 0 ? true : _b, render = item.render, subItems = item.children, navProps = item.navProps;
    if (visible) {
        if (subItems) {
            return (React.createElement(Nav.SubNav, __assign({ key: key, label: label }, navProps), subItems.map(mapItemToJSX)));
        }
        return (React.createElement(Nav.Item, __assign({ key: key, disabled: disabled }, navProps), render ? render(item) : label));
    }
    return null;
}
function getPathMap(item, path, container) {
    if (path === void 0) { path = []; }
    if (container === void 0) { container = {}; }
    if (item.children) {
        path.push(item.key);
        item.children.forEach(function (subItem) { return getPathMap(subItem, path, container); });
    }
    else {
        container[item.key] = path;
    }
}
var useOpenKeys = function (activeKey, defaultActiveKey, dataSource) {
    var pathMap = {};
    dataSource && dataSource.forEach(function (subItem) { return getPathMap(subItem, [], pathMap); });
    var openKeys_ = undefined;
    var defaultOpenKeys = undefined;
    if (activeKey) {
        openKeys_ = Array.isArray(activeKey) ? activeKey.map(function (item) { return pathMap[item]; }).join().split(",") : pathMap[activeKey];
    }
    if (defaultActiveKey) {
        defaultOpenKeys = Array.isArray(defaultActiveKey) ? defaultActiveKey.map(function (item) { return pathMap[item]; }).join().split(",") : pathMap[defaultActiveKey];
    }
    return { openKeys_: openKeys_, defaultOpenKeys: defaultOpenKeys };
};
var NavMenu = function (_a) {
    var _b = _a.type, type = _b === void 0 ? 'primary' : _b, header = _a.header, headers = _a.headers, onSelectHeader = _a.onSelectHeader, dataSource = _a.dataSource, children = _a.children, activeKey = _a.activeKey, defaultActiveKey = _a.defaultActiveKey, _c = _a.isSildeNav, isSildeNav = _c === void 0 ? true : _c, restProps = __rest(_a, ["type", "header", "headers", "onSelectHeader", "dataSource", "children", "activeKey", "defaultActiveKey", "isSildeNav"]);
    var ExactMenuComponent = type === 'secondary' ? SecondaryMenu : PrimaryMenu;
    var _d = useOpenKeys(activeKey, defaultActiveKey, dataSource), openKeys_ = _d.openKeys_, defaultOpenKeys = _d.defaultOpenKeys;
    var _e = useState(openKeys_), openKeys = _e[0], setOpenKey = _e[1];
    return (React.createElement(ExactMenuComponent, __assign({ openKeys: openKeys, defaultOpenKeys: defaultOpenKeys, onOpen: function (v) { return setOpenKey(v); } }, restProps, { header: header && (React.createElement(Header, { isSildeNav: true, header: header, headers: headers, onSelectHeader: onSelectHeader })), selectedKeys: activeKey, defaultSelectedKeys: defaultActiveKey }),
        Array.isArray(dataSource) && dataSource.map(mapItemToJSX),
        children));
};
NavMenu.Item = Nav.Item;
NavMenu.SubMenu = Nav.SubNav;
export default NavMenu;
