import { FC, useEffect } from "react";
import useDealsTableColumns from "./columns.tsx";
import { DealSummary } from "../../../../../../../client";
import { BaseTable } from "../../../../../../../components/BaseTable/BaseTable.tsx";
import { usePrefillDealContext } from "../../../../../contexts/PrefillDealContext.tsx";

type Props = {
    items: DealSummary[];
};

const DealsTable: FC<Props> = ({ items }) => {
    const { selectPrefillDeal } = usePrefillDealContext();
    const columns = useDealsTableColumns();
    const defaultSorting = [{ id: "createdAt", desc: false }];

    useEffect(() => {
        if (items.length < 1) return;
        selectPrefillDeal(items[0].id);
    }, []);

    return (
        <BaseTable
            data={items}
            columns={columns}

            restProps={
                {
                    enableSorting: true,
                    enableColumnActions: false,
                    enablePagination: true,
                    enableBottomToolbar: true,
                    paginationDisplayMode: "pages",
                    initialState: {
                        sorting: defaultSorting,
                    },
                    mantinePaginationProps: {
                        showRowsPerPage: false,
                    },
                    enableStickyHeader: true,
                    enableStickyFooter: true,
                    mantineTableContainerProps: { style: { maxHeight: "81vh", height: "81vh" } },
                }
            }
        />
    );
};

export default DealsTable;
