"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PHXTabs = PHXTabs;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importStar(require("react"));
const types_1 = require("../types");
const Badge_1 = require("../Badge");
const Card_1 = require("../Card");
const Select_1 = require("../Select");
function PHXTabs({ aggregate, children, className, dataTabs, dataTabsQuery, defaultTab, filterValue, getOnlyTitleOfTabs, getQuery, getQuerySearch, pagination, paginationData, titleComponent, tabComponent, type = 'default', onClickTab, isNotSpacing = false, }) {
    var _a, _b;
    const defaultActive = dataTabs
        ? getOnlyTitleOfTabs
            ? dataTabs[0].id
            : dataTabs[0].name
        : dataTabsQuery
            ? dataTabsQuery[0].name
            : null;
    const [current, setCurrent] = (0, react_1.useState)(defaultTab ? defaultTab : defaultActive);
    const currTab = dataTabs === null || dataTabs === void 0 ? void 0 : dataTabs.find((tab) => (tab.id || tab.name) === current);
    const handleChangeTabsQuery = (eventValue) => {
        var _a, _b;
        setCurrent(eventValue);
        if (dataTabsQuery && paginationData) {
            paginationData(0);
            getQuery((_a = dataTabsQuery === null || dataTabsQuery === void 0 ? void 0 : dataTabsQuery.find((tab) => tab.name === eventValue)) === null || _a === void 0 ? void 0 : _a.query);
            getQuerySearch((_b = dataTabsQuery === null || dataTabsQuery === void 0 ? void 0 : dataTabsQuery.find((tab) => tab.name === eventValue)) === null || _b === void 0 ? void 0 : _b.querySearch);
        }
    };
    (0, react_1.useEffect)(() => {
        var _a, _b;
        if (dataTabsQuery && filterValue) {
            getQuery((_a = dataTabsQuery === null || dataTabsQuery === void 0 ? void 0 : dataTabsQuery.find((tab) => tab.name === current)) === null || _a === void 0 ? void 0 : _a.query);
            getQuerySearch((_b = dataTabsQuery === null || dataTabsQuery === void 0 ? void 0 : dataTabsQuery.find((tab) => tab.name === current)) === null || _b === void 0 ? void 0 : _b.querySearch);
        }
    }, [filterValue, pagination]);
    const componentByTab = (react_1.default.createElement(react_1.default.Fragment, null, ((_a = dataTabs === null || dataTabs === void 0 ? void 0 : dataTabs.find((tab) => (tab.id || tab.name) === current)) === null || _a === void 0 ? void 0 : _a.component) && (react_1.default.createElement("div", { className: 'pt-4' }, (_b = dataTabs === null || dataTabs === void 0 ? void 0 : dataTabs.find((tab) => (tab.id || tab.name) === current)) === null || _b === void 0 ? void 0 : _b.component))));
    const Tabs = () => dataTabsQuery ? (react_1.default.createElement("div", { className: (0, types_1.classNames)(isNotSpacing ? '' : 'mb-4') },
        react_1.default.createElement("div", { className: 'sm:hidden' },
            react_1.default.createElement("label", { className: 'sr-only', htmlFor: 'tabs' }, "Select a tab"),
            react_1.default.createElement("select", { className: 'block w-full rounded-md border-gray-300 py-2 pl-3 pr-10 text-base focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm', value: current, id: 'tabs', name: 'tabs', onChange: (e) => handleChangeTabsQuery(e.target.value) }, dataTabsQuery === null || dataTabsQuery === void 0 ? void 0 : dataTabsQuery.map((tab) => (react_1.default.createElement("option", { key: tab.name, value: tab.name }, tab.name === current && aggregate ? `${tab.name} (${aggregate})` : tab.name))))),
        react_1.default.createElement("div", { className: 'hidden sm:block' },
            react_1.default.createElement("nav", { "aria-label": 'Tabs', className: 'flex space-x-1' }, dataTabsQuery === null || dataTabsQuery === void 0 ? void 0 : dataTabsQuery.map((tab) => (react_1.default.createElement("button", { key: tab.name, className: (0, types_1.classNames)(tab.name === current ? 'bg-gray-200 text-gray-800' : 'text-gray-600 hover:bg-gray-200', 'rounded-lg px-3 py-1.5 text-xs font-normal leading-4'), onClick: () => handleChangeTabsQuery(tab.name), type: 'button' }, tab.name === current && aggregate ? (react_1.default.createElement("span", null,
                tab.name,
                "\u00A0",
                react_1.default.createElement(Badge_1.PHXBadge, { text: `${aggregate}`, type: 'informational' }))) : (tab.name)))))),
        children)) : (react_1.default.createElement("div", { className: (0, types_1.classNames)(isNotSpacing ? '' : 'mb-4') },
        react_1.default.createElement("div", { className: 'sm:hidden' },
            react_1.default.createElement("label", { className: 'sr-only', htmlFor: 'tabs' }, "Select a tab"),
            react_1.default.createElement(Select_1.PHXSelect
            // className='block w-full rounded-md border-gray-300 py-2 pl-3 pr-10 text-base focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm'
            , { 
                // className='block w-full rounded-md border-gray-300 py-2 pl-3 pr-10 text-base focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm'
                value: (currTab === null || currTab === void 0 ? void 0 : currTab.id) || (currTab === null || currTab === void 0 ? void 0 : currTab.name), id: 'tabs', name: 'tabs', onChange: (e) => {
                    const val = isNaN(Number(e.target.value)) ? e.target.value : Number(e.target.value);
                    setCurrent(val);
                    getOnlyTitleOfTabs === null || getOnlyTitleOfTabs === void 0 ? void 0 : getOnlyTitleOfTabs(val);
                    const tab = dataTabs === null || dataTabs === void 0 ? void 0 : dataTabs.find((item) => (item.id || item.name) === val);
                    onClickTab === null || onClickTab === void 0 ? void 0 : onClickTab(tab);
                } }, dataTabs === null || dataTabs === void 0 ? void 0 : dataTabs.map((tab) => (react_1.default.createElement("option", { key: tab.name, disabled: tab.disabled, value: tab.id || tab.name }, tab.name)))),
            componentByTab),
        react_1.default.createElement("div", { className: 'hidden sm:block' },
            react_1.default.createElement("nav", { "aria-label": 'Tabs', className: 'flex space-x-1' }, dataTabs === null || dataTabs === void 0 ? void 0 : dataTabs.map((tab) => (react_1.default.createElement("button", { key: tab.name, className: tab.disabled
                    ? 'text-xs px-3 py-1.5 font-normal leading-4 text-gray-400'
                    : (0, types_1.classNames)((tab.id || tab.name) === current
                        ? 'bg-gray-200 text-gray-800'
                        : 'text-gray-600 hover:bg-gray-200', 'rounded-lg px-3 py-1.5 text-xs font-normal leading-4'), onClick: () => {
                    setCurrent(tab.id || tab.name);
                    getOnlyTitleOfTabs === null || getOnlyTitleOfTabs === void 0 ? void 0 : getOnlyTitleOfTabs(tab.id);
                    onClickTab === null || onClickTab === void 0 ? void 0 : onClickTab(tab);
                }, type: 'button', disabled: tab.disabled }, tab.name)))),
            componentByTab)));
    const TabsComponent = () => {
        var _a, _b;
        return dataTabsQuery ? (react_1.default.createElement("div", { className: 'mb-4' },
            react_1.default.createElement("div", { className: 'sm:hidden' },
                react_1.default.createElement("label", { className: 'sr-only', htmlFor: 'tabs' }, "Select a tab"),
                react_1.default.createElement("select", { className: 'block w-full rounded-md border-gray-300 py-2 pl-3 pr-10 text-base focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm', defaultValue: (_a = dataTabsQuery === null || dataTabsQuery === void 0 ? void 0 : dataTabsQuery.find((tab) => tab.name === current)) === null || _a === void 0 ? void 0 : _a.name, id: 'tabs', name: 'tabs', onChange: (e) => handleChangeTabsQuery(e.target.value) }, dataTabsQuery === null || dataTabsQuery === void 0 ? void 0 : dataTabsQuery.map((tab) => (react_1.default.createElement("option", { key: tab.name, value: tab.name }, tab.name === current && aggregate ? react_1.default.createElement("span", null, tab.name + ' ' + '(' + aggregate + ')') : tab.name))))),
            react_1.default.createElement("div", { className: 'hidden sm:block' },
                react_1.default.createElement("nav", { "aria-label": 'Tabs', className: 'flex space-x-1' }, dataTabsQuery === null || dataTabsQuery === void 0 ? void 0 : dataTabsQuery.map((tab) => (react_1.default.createElement("button", { key: tab.name, className: (0, types_1.classNames)(tab.name === current ? 'bg-gray-200 text-gray-800' : 'text-gray-600 hover:bg-gray-200', 'rounded-lg px-3 py-1.5 text-xs font-normal leading-4'), onClick: () => handleChangeTabsQuery(tab.name), type: 'button' }, tab.name === current && aggregate ? (react_1.default.createElement("span", null,
                    tab.name,
                    "\u00A0",
                    react_1.default.createElement(Badge_1.PHXBadge, { text: `${aggregate}`, type: 'informational' }))) : (tab.name)))))),
            children)) : (react_1.default.createElement("div", { className: 'mb-4', onClick: () => {
                var _a;
                const tabName = (_a = dataTabs === null || dataTabs === void 0 ? void 0 : dataTabs.find((tab) => { var _a; return ((_a = tab.id) !== null && _a !== void 0 ? _a : tab.name) === current; })) === null || _a === void 0 ? void 0 : _a.name;
                if (onClickTab && tabName) {
                    onClickTab(tabName);
                }
            } },
            react_1.default.createElement("div", { className: 'flex items-center space-x-2' },
                react_1.default.createElement("select", { className: 'rounded-md hidden border-gray-300 py-2 pl-3 pr-10 text-base focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm', defaultValue: (_b = dataTabs === null || dataTabs === void 0 ? void 0 : dataTabs.find((tab) => { var _a; return ((_a = tab.id) !== null && _a !== void 0 ? _a : tab.name) === current; })) === null || _b === void 0 ? void 0 : _b.name, id: 'tabs', name: 'tabs', onChange: (e) => {
                        setCurrent(e.target.value);
                        getOnlyTitleOfTabs === null || getOnlyTitleOfTabs === void 0 ? void 0 : getOnlyTitleOfTabs(e.target.value);
                    } }, dataTabs === null || dataTabs === void 0 ? void 0 : dataTabs.map((tab) => {
                    var _a;
                    return (react_1.default.createElement("option", { key: tab.name, value: (_a = tab.id) !== null && _a !== void 0 ? _a : tab.name }, tab.name));
                })),
                tabComponent),
            react_1.default.createElement("div", { className: 'hidden sm:block' },
                react_1.default.createElement("nav", { "aria-label": 'Tabs', className: 'flex space-x-1' }, dataTabs === null || dataTabs === void 0 ? void 0 : dataTabs.map((tab) => (react_1.default.createElement("button", { key: tab.name, className: (0, types_1.classNames)((tab.id || tab.name) === current ? 'bg-gray-200 text-gray-800' : 'text-gray-600 hover:bg-gray-200', 'rounded-lg px-3 py-1.5 text-xs font-normal leading-4'), onClick: () => {
                        setCurrent(tab.id || tab.name);
                        getOnlyTitleOfTabs === null || getOnlyTitleOfTabs === void 0 ? void 0 : getOnlyTitleOfTabs(tab.id);
                    }, type: 'button' }, tab.name))))),
            componentByTab));
    };
    const TabBarUndeline = () => {
        var _a;
        return (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement("div", { className: 'sm:hidden' },
                react_1.default.createElement("select", { className: 'block w-full rounded-md border-gray-300 focus:border-indigo-500 focus:ring-indigo-500', defaultValue: (_a = dataTabs === null || dataTabs === void 0 ? void 0 : dataTabs.find((tab) => (tab.id || tab.name) === current)) === null || _a === void 0 ? void 0 : _a.name, id: 'tabs', name: 'tabs' }, dataTabs === null || dataTabs === void 0 ? void 0 : dataTabs.map((tab) => (react_1.default.createElement("option", { key: tab.name, value: tab.id || tab.name }, tab.name))))),
            react_1.default.createElement("div", { className: 'hidden sm:block' },
                react_1.default.createElement("nav", { "aria-label": 'Tabs', className: 'isolate flex divide-x divide-gray-200 rounded-lg shadow' }, dataTabs === null || dataTabs === void 0 ? void 0 : dataTabs.map((tab, index) => (react_1.default.createElement("button", { key: tab.name, className: (0, types_1.classNames)(tab.name ? 'text-gray-900' : 'text-gray-500 hover:text-gray-700', index === 0 ? 'rounded-l-lg' : '', index === dataTabs.length - 1 ? 'rounded-r-lg' : '', 'group relative min-w-0 flex-1 overflow-hidden bg-white px-4 py-4 text-center text-sm font-medium hover:bg-gray-50 focus:z-10'), onClick: () => {
                        setCurrent(tab.id || tab.name);
                        getOnlyTitleOfTabs === null || getOnlyTitleOfTabs === void 0 ? void 0 : getOnlyTitleOfTabs(tab.id);
                        onClickTab && onClickTab(tab);
                    }, type: 'button' },
                    react_1.default.createElement("span", null, tab.name),
                    react_1.default.createElement("span", { "aria-hidden": 'true', className: (0, types_1.classNames)((tab.id || tab.name) === current ? 'bg-indigo-500' : 'bg-transparent', 'absolute inset-x-0 bottom-0 h-0.5') }))))),
                componentByTab)));
    };
    return (react_1.default.createElement("div", { className: className }, type === 'inside-of-card' ? (react_1.default.createElement(Card_1.PHXCard, null,
        react_1.default.createElement(Tabs, null))) : type === 'bar-underline' ? (react_1.default.createElement(TabBarUndeline, null)) : type === 'inside-component' ? (react_1.default.createElement(Card_1.PHXCard, { title: titleComponent, primaryHeader: true },
        react_1.default.createElement(TabsComponent, null))) : (react_1.default.createElement(Tabs, null))));
}
//# sourceMappingURL=Tabs.js.map