import React__default, { useState, useEffect } from 'react';
import { DataTableWrapper, ActionBar, SelectedItemsInfo, ActionButtons, ResponsiveTableWrapper, Table, Footer } from './styles.js';
import { Button } from '../Button/index.js';
import { getId } from '../../utils/index.js';
import MainHeader from './components/MainHeader.js';
import TableHeader from './components/TableHeader.js';
import TableBody from './components/TableBody.js';
import PaginatedFooter from './components/PaginatedFooter.js';
import TableFoot from './components/TableFoot.js';

function DataTable({ variant = 'wide', header, actionsBar, columns, rows, footer, selectableRows, additionalRows, 'data-testid': dataTestId, 'data-trackid': dataTrackId, }) {
    const appendTestId = getId(dataTestId);
    const appendTrackId = getId(dataTrackId);
    const pagination = footer && footer.variant === 'pagination' && footer.pagination;
    const [currentPage, setCurrentPage] = useState((pagination && pagination.currentPage) || 1);
    useEffect(() => {
        pagination &&
            pagination.controlledPagination &&
            setCurrentPage(pagination.currentPage || 1);
    }, [rows]);
    const tableBodyRows = pagination && pagination.controlledPagination
        ? rows.slice(currentPage * pagination.rowsPerPage - pagination.rowsPerPage, currentPage * pagination.rowsPerPage)
        : rows;
    return (React__default.createElement(DataTableWrapper, { "data-testid": appendTestId(), "data-trackid": appendTrackId() },
        header && (React__default.createElement(MainHeader, { variant: variant, headerVariant: header.variant || 'subtle', title: header.title, secondaryText: header.secondaryText, "data-testid": appendTestId('main-header'), "data-trackid": appendTrackId('main-header') })),
        actionsBar && (React__default.createElement(ActionBar, { "data-testid": appendTestId('action-bar'), "data-trackid": appendTrackId('action-bar') },
            actionsBar.text && selectableRows && (React__default.createElement(SelectedItemsInfo, null,
                selectableRows.selectedRows.length,
                " ",
                actionsBar.text)),
            React__default.createElement(ActionButtons, null, actionsBar.actionButtons.map((actionButton, index) => {
                return (React__default.createElement(Button, { key: `action_button_${index}`, variant: "inverse-ghost", size: "small-expressive", iconPosition: "trailing", icon: actionButton.icon, disabled: !!actionButton.disabled, onClick: () => actionButton.onClick(), role: "button" }, actionButton.text));
            })))),
        React__default.createElement(ResponsiveTableWrapper, null,
            React__default.createElement(Table, { role: "table" },
                React__default.createElement(TableHeader, { columns: columns, rows: rows, selectAllRows: selectableRows === null || selectableRows === void 0 ? void 0 : selectableRows.onSelectAll, selectedRows: selectableRows === null || selectableRows === void 0 ? void 0 : selectableRows.selectedRows, rowIdentifier: selectableRows === null || selectableRows === void 0 ? void 0 : selectableRows.rowId, "data-testid": appendTestId('table-header'), "data-trackid": appendTrackId('table-header') }),
                React__default.createElement(TableBody, { filteredRows: tableBodyRows, onRowClick: selectableRows === null || selectableRows === void 0 ? void 0 : selectableRows.onRowClick, selectedRows: selectableRows === null || selectableRows === void 0 ? void 0 : selectableRows.selectedRows, disabledRows: selectableRows === null || selectableRows === void 0 ? void 0 : selectableRows.disabledRows, columns: columns, rowIdentifier: selectableRows === null || selectableRows === void 0 ? void 0 : selectableRows.rowId, "data-testid": appendTestId('table-body'), "data-trackid": appendTrackId('table-body') }),
                additionalRows && additionalRows.length > 0 && (React__default.createElement(TableFoot, { "data-testid": appendTestId('table-foot'), "data-trackid": appendTrackId('table-foot'), additionalRows: additionalRows })))),
        footer && (React__default.createElement(Footer, { "data-testid": appendTestId('footer'), "data-trackid": appendTrackId('footer') },
            footer.variant === 'pagination' && (React__default.createElement(PaginatedFooter, { footer: footer, currentPage: currentPage, setCurrentPage: setCurrentPage, numberOfRows: rows.length, variant: variant, "data-testid": appendTestId('pagination'), "data-trackid": appendTrackId('pagination') })),
            footer.variant === 'custom' && footer.content))));
}

export { DataTable };
