"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.TabPanel = exports.TabPanels = exports.Tab = exports.TabList = exports.Tabs = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importStar(require("react"));
const Tabs = ({ activeTab, onChange, children, className, variant = 'default', align = 'left', vertical, lazyLoad, borderless, }) => {
    const [activeTabIndex, setActiveTabIndex] = (0, react_1.useState)(0);
    (0, react_1.useEffect)(() => {
        if (activeTab !== undefined) {
            setActiveTabIndex(activeTab);
        }
    }, [activeTab]);
    const handleTabClick = (index) => {
        if (onChange) {
            onChange(index);
        }
        else {
            setActiveTabIndex(index);
        }
    };
    const tabList = react_1.default.Children.toArray(children).find((child) => child.type === TabList);
    const tabPanels = react_1.default.Children.toArray(children).find((child) => child.type === TabPanels);
    const tabsClasses = [
        'bf-tabs',
        `bf-tabs--${variant}`,
        `bf-tabs--${align}`,
        vertical ? 'bf-tabs--vertical' : '',
        borderless ? 'bf-tabs--borderless' : '',
        className || '',
    ].filter(Boolean).join(' ');
    return ((0, jsx_runtime_1.jsxs)("div", { className: tabsClasses, children: [tabList && react_1.default.cloneElement(tabList, {
                activeTabIndex,
                onTabClick: handleTabClick,
                vertical,
            }), tabPanels && react_1.default.cloneElement(tabPanels, {
                activeTabIndex,
                lazyLoad,
            })] }));
};
exports.Tabs = Tabs;
const TabList = ({ children, activeTabIndex = 0, onTabClick, className, vertical, }) => {
    const tabListClasses = [
        'bf-tab-list',
        vertical ? 'bf-tab-list--vertical' : '',
        className || '',
    ].filter(Boolean).join(' ');
    return ((0, jsx_runtime_1.jsx)("div", { className: tabListClasses, role: "tablist", children: react_1.default.Children.map(children, (child, index) => {
            if (!react_1.default.isValidElement(child))
                return null;
            return react_1.default.cloneElement(child, {
                isActive: index === activeTabIndex,
                onClick: () => onTabClick && onTabClick(index),
                index,
                role: 'tab',
            });
        }) }));
};
exports.TabList = TabList;
const Tab = ({ children, isActive, onClick, index, disabled, icon, className, }) => {
    const tabClasses = [
        'bf-tab',
        isActive ? 'bf-tab--active' : '',
        disabled ? 'bf-tab--disabled' : '',
        className || '',
    ].filter(Boolean).join(' ');
    const handleClick = () => {
        if (!disabled && onClick && typeof index === 'number') {
            onClick(index);
        }
    };
    return ((0, jsx_runtime_1.jsxs)("button", { className: tabClasses, role: "tab", "aria-selected": !!isActive, "aria-controls": `bf-tab-panel-${index}`, id: `bf-tab-${index}`, tabIndex: isActive ? 0 : -1, onClick: handleClick, disabled: disabled, children: [icon && (0, jsx_runtime_1.jsx)("span", { className: "bf-tab__icon", children: icon }), (0, jsx_runtime_1.jsx)("span", { className: "bf-tab__text", children: children })] }));
};
exports.Tab = Tab;
const TabPanels = ({ children, activeTabIndex = 0, lazyLoad, className, }) => {
    const tabPanelsClasses = [
        'bf-tab-panels',
        className || '',
    ].filter(Boolean).join(' ');
    return ((0, jsx_runtime_1.jsx)("div", { className: tabPanelsClasses, children: react_1.default.Children.map(children, (child, index) => {
            if (!react_1.default.isValidElement(child))
                return null;
            if (lazyLoad && index !== activeTabIndex) {
                return null;
            }
            return react_1.default.cloneElement(child, {
                isActive: index === activeTabIndex,
                index,
                role: 'tabpanel',
            });
        }) }));
};
exports.TabPanels = TabPanels;
const TabPanel = ({ children, isActive, index, className, }) => {
    const tabPanelClasses = [
        'bf-tab-panel',
        isActive ? 'bf-tab-panel--active' : '',
        className || '',
    ].filter(Boolean).join(' ');
    return ((0, jsx_runtime_1.jsx)("div", { className: tabPanelClasses, role: "tabpanel", id: `bf-tab-panel-${index}`, "aria-labelledby": `bf-tab-${index}`, tabIndex: 0, hidden: !isActive, children: children }));
};
exports.TabPanel = TabPanel;
exports.default = Tabs;
//# sourceMappingURL=Tabs.js.map