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

type Props = {
    data: CardProductServiceSchema[];
    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 hideGuestColumns = ["service.cost"];
    return useMemo<MRT_ColumnDef<CardProductServiceSchema>[]>(
        () => [
            {
                accessorKey: "service.name",
                header: "Услуга",
            },
            {
                accessorKey: "price",
                header: "Цена",
                size: 5,
                Cell: ({ cell }) => cell.getValue() + " ₽",
                Footer: () => <>Итог: {totalPrice.toLocaleString("ru")}₽</>,
            },
        ],
        [totalPrice]
    ).filter(
        columnDef =>
            !(
                hideGuestColumns.includes(columnDef.accessorKey || "") &&
                authState.isGuest
            )
    );
};

export default useProductServicesTableColumns;
