import { _ as __assign } from '../tslib.es6-20a2a300.js';
import { jsx, jsxs } from 'react/jsx-runtime';
import React__default from 'react';
import TableHeadSort from './TableHeadSort.js';
import { formatCellDataRowsIntoArray } from './TableContainer.js';
import { m as makeStyles } from '../makeStyles-c9a99c7a.js';
import { a as createStyles } from '../createStyles-16423e9e.js';
import { b as MuiTableContainer, M as MuiTable, a as MuiTableBody } from '../TableContainer-e3324a72.js';
import { M as MuiTableRow, a as MuiTableCell } from '../TableRow-b30398d4.js';
import './TableSortLabel.js';
import './TableSortLabelIcon.js';
import './Icon.js';
import '../icons/ArrowRight.js';
import '../icons/ArrowRightCircle.js';
import '../icons/Check.js';
import '../icons/CheckCircle.js';
import '../icons/ChevronRight.js';
import '../icons/CrossCircle.js';
import '../icons/DollarSign.js';
import '../icons/InfoCircle.js';
import '../icons/Link.js';
import '../icons/TriangleUp.js';
import '../icons/TriangleDown.js';
import '../icons/TriangleLeft.js';
import '../icons/Copy.js';
import '../icons/SortUp.js';
import '../icons/Cross.js';
import '../icons/Search.js';
import '../icons/Range.js';
import '../theme/colors.js';
import '../defaultTheme-49a26576.js';
import '../Box-581dc061.js';
import '../styled-339d0187.js';
import '../clsx.m-6243b874.js';
import '../hoist-non-react-statics.cjs-b718ab87.js';
import '../createSvgIcon-6fb7d1b7.js';
import '../withStyles-99aeb25b.js';
import '../capitalize-5d3e9aac.js';
import '../ButtonBase-11d3cdb5.js';
import 'react-dom';
import '../useForkRef-4ff8cc89.js';
import '../useEventCallback-8817ed1c.js';
import '../deprecatedPropType-0232df1e.js';
import '../useIsFocusVisible-c048447c.js';
import '../TransitionGroupContext-2ba64e99.js';
import '../refType-5cc24a92.js';
import '../elementTypeAcceptingRef-7677eeee.js';

/**
 * Sorting Utilities
 */
function getCellDataKey(data) {
    return Object.keys(data)[0];
}
function getCellComparatorKey(data, orderBy) {
    var _a;
    return ((_a = data === null || data === void 0 ? void 0 : data[orderBy]) === null || _a === void 0 ? void 0 : _a.key) ? data[orderBy].key : null;
}
function descendingComparator(a, b, orderBy) {
    var aComparatorKey = getCellComparatorKey(a, orderBy);
    var bComparatorKey = getCellComparatorKey(b, orderBy);
    if (!aComparatorKey || !bComparatorKey || aComparatorKey === bComparatorKey)
        return 0;
    return bComparatorKey < aComparatorKey ? -1 : 1;
}
function getComparator(order, orderBy) {
    return order === "desc"
        ? function (a, b) { return descendingComparator(a, b, orderBy); }
        : function (a, b) { return -descendingComparator(a, b, orderBy); };
}
function stableSort(array, comparator) {
    var stabilizedThis = array.map(function (el, index) { return [el, index]; });
    stabilizedThis.sort(function (a, b) {
        var order = comparator(a[0], b[0]);
        return order !== 0 ? order : a[1] - b[1];
    });
    return stabilizedThis.map(function (el) { return el[0]; });
}
/**
 * Utilities
 */
function createCellSortData(dataSets, cellDataKeys) {
    var cellData = {};
    cellDataKeys.forEach(function (key, index) { return (cellData[key] = dataSets[index]); });
    return cellData;
}
/**
 *
 * Styles
 *
 */
var useStyles = makeStyles(function (theme) {
    return createStyles({
        root: {
            width: "100%"
        }
    });
});
var TableContainerSort = function (_a) {
    var headCells = _a.headCells, bodyCellsRows = _a.bodyCellsRows;
    var classes = useStyles();
    var _b = React__default.useState("asc"), order = _b[0], setOrder = _b[1];
    // set initial orderBy as the key of first cell in first row
    var _c = React__default.useState(getCellDataKey(bodyCellsRows[0])), orderBy = _c[0], setOrderBy = _c[1];
    var handleRequestSort = function (event, property) {
        var isAsc = orderBy === property && order === "asc";
        setOrder(isAsc ? "desc" : "asc");
        setOrderBy(property);
    };
    return (jsx(MuiTableContainer, __assign({ className: classes.root }, { children: jsxs(MuiTable, __assign({ "aria-labelledby": "tableTitle", "aria-label": "enhanced table" }, { children: [jsx(TableHeadSort, { headCells: headCells, order: order, orderBy: orderBy.toString(), onRequestSort: handleRequestSort }, void 0), jsx(MuiTableBody, { children: stableSort(bodyCellsRows, getComparator(order, orderBy)).map(function (rowCells, index) { return (jsx(MuiTableRow, __assign({ tabIndex: -1 }, { children: formatCellDataRowsIntoArray(rowCells).map(function (cell, cellIndex) { return (jsx(MuiTableCell, __assign({}, cell.props, { children: cell.content }), cellIndex)); }) }), index)); }) }, void 0)] }), void 0) }), void 0));
};

export { createCellSortData, TableContainerSort as default };
