import {useMemo} from "react";
import {MRT_ColumnDef} from "mantine-react-table";
import {DealProductServiceSchema} from "../../../../../../client";
import {useSelector} from "react-redux";
import {RootState} from "../../../../../../redux/store.ts";

type Props = {
    data: DealProductServiceSchema[];
    quantity: number;
}
const useProductServicesTableColumns = (props: Props) => {
    const {data, quantity} = props;
    const authState = useSelector((state: RootState) => state.auth);
    const totalPrice = useMemo(() => data.reduce((acc, row) => acc + (row.price * quantity), 0), [data, quantity]);
    const totalCost = useMemo(() => data.reduce((acc, row) => acc + ((row.service.cost || 0) * quantity), 0), [data, quantity]);
    const hideGuestColumns = [
        "service.cost"
    ]
    return useMemo<MRT_ColumnDef<DealProductServiceSchema>[]>(() => [
        {
            accessorKey: "service.name",
            header: "Услуга",
        },
        {
            enableHiding: true,
            accessorKey: "service.cost",
            header: "Себестоимость",
            Footer: () => <>Итоговая себестоимость: {totalCost.toLocaleString("ru")}₽</>,
        },
        {
            accessorKey: "price",
            header: "Цена",
            Footer: () => <>Итог: {totalPrice.toLocaleString("ru")}₽</>,
        }
    ], [totalPrice]).filter(columnDef => !(hideGuestColumns.includes(columnDef.accessorKey || "") && authState.isGuest));
}

export default useProductServicesTableColumns;