import { useProfitTabContext } from "../../../contexts/ProfitTabContext.tsx";
import { useProfitTableColumns } from "./columns.tsx";
import { useMantineReactTable } from "mantine-react-table";
import { MRT_Localization_RU } from "mantine-react-table/locales/ru/index.cjs";


export const useProfitTable = () => {
    const {
        form,
        isTableLoading: isLoading,
        profitTableData
    } = useProfitTabContext();

    const columns = useProfitTableColumns({
        groupTableBy: form.values.groupTableBy,
    });

    const defaultSorting = [{ id: "groupedValue", desc: true }];

    const table = useMantineReactTable({
        enablePagination: false,
        data: profitTableData,
        columns,
        enableTopToolbar: false,
        enableBottomToolbar: false,
        enableSorting: true,
        initialState: {
            sorting: defaultSorting,
        },
        localization: MRT_Localization_RU,
        enableRowVirtualization: true,
        mantineTableContainerProps: { style: { maxHeight: "86vh" } },
    });

    return {
        table,
        form,
        isLoading,
    };
};