'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var styles = require('./styles.js');
var index = require('../Button/index.js');
var index$1 = require('../../utils/index.js');
var MainHeader = require('./components/MainHeader.js');
var TableHeader = require('./components/TableHeader.js');
var TableBody = require('./components/TableBody.js');
var PaginatedFooter = require('./components/PaginatedFooter.js');
var TableFoot = require('./components/TableFoot.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

function DataTable({ variant = 'wide', header, actionsBar, columns, rows, footer, selectableRows, additionalRows, 'data-testid': dataTestId, 'data-trackid': dataTrackId, }) {
    const appendTestId = index$1.getId(dataTestId);
    const appendTrackId = index$1.getId(dataTrackId);
    const pagination = footer && footer.variant === 'pagination' && footer.pagination;
    const [currentPage, setCurrentPage] = React.useState((pagination && pagination.currentPage) || 1);
    React.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['default'].createElement(styles.DataTableWrapper, { "data-testid": appendTestId(), "data-trackid": appendTrackId() },
        header && (React__default['default'].createElement(MainHeader['default'], { 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['default'].createElement(styles.ActionBar, { "data-testid": appendTestId('action-bar'), "data-trackid": appendTrackId('action-bar') },
            actionsBar.text && selectableRows && (React__default['default'].createElement(styles.SelectedItemsInfo, null,
                selectableRows.selectedRows.length,
                " ",
                actionsBar.text)),
            React__default['default'].createElement(styles.ActionButtons, null, actionsBar.actionButtons.map((actionButton, index$1) => {
                return (React__default['default'].createElement(index.Button, { key: `action_button_${index$1}`, variant: "inverse-ghost", size: "small-expressive", iconPosition: "trailing", icon: actionButton.icon, disabled: !!actionButton.disabled, onClick: () => actionButton.onClick(), role: "button" }, actionButton.text));
            })))),
        React__default['default'].createElement(styles.ResponsiveTableWrapper, null,
            React__default['default'].createElement(styles.Table, { role: "table" },
                React__default['default'].createElement(TableHeader['default'], { 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['default'].createElement(TableBody['default'], { 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['default'].createElement(TableFoot['default'], { "data-testid": appendTestId('table-foot'), "data-trackid": appendTrackId('table-foot'), additionalRows: additionalRows })))),
        footer && (React__default['default'].createElement(styles.Footer, { "data-testid": appendTestId('footer'), "data-trackid": appendTrackId('footer') },
            footer.variant === 'pagination' && (React__default['default'].createElement(PaginatedFooter['default'], { footer: footer, currentPage: currentPage, setCurrentPage: setCurrentPage, numberOfRows: rows.length, variant: variant, "data-testid": appendTestId('pagination'), "data-trackid": appendTrackId('pagination') })),
            footer.variant === 'custom' && footer.content))));
}

exports.DataTable = DataTable;
