"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.Tabs = exports.TabsBase = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const core_1 = require("../core");
const p_1 = require("../p");
const utils_1 = require("../utils");
const tab_1 = __importDefault(require("./tab"));
const tabsNavBar_1 = __importDefault(require("./tabsNavBar"));
const useTabsState_1 = require("./useTabsState");
exports.TabsBase = core_1.styled.divBox `
  width: 100%;
`;
/**
 * Implements Tabs navigation to switch between views. Implements Tabs navigation to switch between views. **Do not use as a top menu in a page!**
 */
exports.Tabs = (0, core_1.vui)((props, ref) => {
    const { actions, activeTabId, showBorder = true, children, className, size, onTabClick } = props, rest = __rest(props, ["actions", "activeTabId", "showBorder", "children", "className", "size", "onTabClick"]);
    const styles = (0, core_1.useStyleConfig)('Tabs', props);
    const { tabs, setTabs, activeTabIndex, setActiveTabIndex, animationDirection, setAnimationDirection } = (0, useTabsState_1.useTabsState)('fadeIn', 0);
    (0, react_1.useEffect)(() => {
        const _tabs = [];
        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.title)
                    _tabs.push({
                        id: child.props.id,
                        index: index,
                        title: child.props.title,
                        icon: child.props.icon,
                        className: child.props.className,
                        children: child.props.children,
                        disabled: child.props.disabled,
                        onClick: child.props.onClick,
                        onDelete: child.props.onDelete,
                    });
            });
        }
        setTabs(_tabs);
        if (activeTabId !== undefined) {
            const tab = _tabs.find((t) => (t === null || t === void 0 ? void 0 : t.id) === activeTabId || (t === null || t === void 0 ? void 0 : t.index) === activeTabId);
            if ((tab === null || tab === void 0 ? void 0 : tab.index) !== undefined)
                setActiveTabIndex(tab.index);
        }
    }, [children, activeTabId]);
    const onNavItemClick = (tabIndex, e) => {
        var _a;
        const tab = tabs.find(t => t.index === tabIndex);
        if ((tab === null || tab === void 0 ? void 0 : tab.children) !== undefined && (tab === null || tab === void 0 ? void 0 : tab.index) !== undefined) {
            setAnimationDirection(tab.index < activeTabIndex ? 'fadeLeft' : 'fadeRight');
            setActiveTabIndex(tab.index);
            onTabClick === null || onTabClick === void 0 ? void 0 : onTabClick(tab.id || tab.index);
        }
        (_a = tab === null || tab === void 0 ? void 0 : tab.onClick) === null || _a === void 0 ? void 0 : _a.call(tab, e);
    };
    const activeTab = tabs === null || tabs === void 0 ? void 0 : tabs[activeTabIndex];
    return ((0, jsx_runtime_1.jsxs)(exports.TabsBase, Object.assign({ className: (0, utils_1.cs)('vui-tabs', className), ref: ref }, styles.container, rest, { children: [!!(tabs === null || tabs === void 0 ? void 0 : tabs.length) && ((0, jsx_runtime_1.jsx)(tabsNavBar_1.default, { actions: actions, activeNavItem: activeTabIndex, animationDirection: animationDirection, onNavItemClick: (tabIndex, e) => onNavItemClick(tabIndex, e), showBorder: showBorder, size: size, tabs: tabs })), (tabs === null || tabs === void 0 ? void 0 : tabs.length) ? ((0, jsx_runtime_1.jsx)(tab_1.default, { onClick: activeTab === null || activeTab === void 0 ? void 0 : activeTab.onClick, title: activeTab === null || activeTab === void 0 ? void 0 : activeTab.title, children: !(activeTab === null || activeTab === void 0 ? void 0 : activeTab.disabled) && (activeTab === null || activeTab === void 0 ? void 0 : activeTab.children) })) : ((0, jsx_runtime_1.jsx)(p_1.P, { p: 2, children: "Please specify Tabs." }))] })));
});
exports.Tabs.displayName = 'Tabs';
exports.default = exports.Tabs;
//# sourceMappingURL=tabs.js.map