import { parseFromValuesOrFunc } from "@/core/utils/utils";
import { TableRow } from "@mui/material";
import DataTable_TableHeadCell from "./TableHeadCell";

const DataTable_TableHeadRow = ({
    columnVirtualizer,
    headerGroup,
    table,
    ...rest
}) => {
    const {
        options: {
            enableStickyHeader,
            layoutMode,
            mrtTheme: { baseBackgroundColor },
            muiTableHeadRowProps,
        },
    } = table;

    const { virtualColumns, virtualPaddingLeft, virtualPaddingRight } =
        columnVirtualizer ?? {};

    const tableRowProps = {
        ...parseFromValuesOrFunc(muiTableHeadRowProps, {
            headerGroup,
            table,
        }),
        ...rest,
    };

    return (
        <TableRow
            {...tableRowProps}
            sx={(theme) => ({
                backgroundColor: baseBackgroundColor,
                display: layoutMode?.startsWith('grid') ? 'flex' : undefined,
                position:
                    enableStickyHeader && layoutMode === 'semantic'
                        ? 'sticky'
                        : 'relative',
                top: 0,
                ...(parseFromValuesOrFunc(tableRowProps?.sx, theme)),
            })}
        >
            {virtualPaddingLeft ? (
                <th style={{ display: 'flex', width: virtualPaddingLeft }} />
            ) : null}
            {(virtualColumns ?? headerGroup.headers).map(
                (headerOrVirtualHeader, staticColumnIndex) => {
                    let header = headerOrVirtualHeader;

                    if (columnVirtualizer) {
                        staticColumnIndex = (headerOrVirtualHeader)
                            .index;
                        header = headerGroup.headers[staticColumnIndex];
                    }

                    return header ? (
                        <DataTable_TableHeadCell
                            columnVirtualizer={columnVirtualizer}
                            header={header}
                            key={header.id}
                            staticColumnIndex={staticColumnIndex}
                            table={table}
                        />
                    ) : null;
                },
            )}
            {virtualPaddingRight ? (
                <th style={{ display: 'flex', width: virtualPaddingRight }} />
            ) : null}
        </TableRow>
    );

}
export default DataTable_TableHeadRow