import { useMemo } from "react";
import { IconCheck, IconX } from "@tabler/icons-react";
import { DataTableColumn } from "mantine-datatable";
import { Center } from "@mantine/core";
import UpdateDeleteTableActions from "@/components/ui/BaseTable/components/UpdateDeleteTableActions";
import { BarcodeTemplateSchema } from "@/lib/client";

type Props = {
    onDelete: (template: BarcodeTemplateSchema) => void;
    onChange: (template: BarcodeTemplateSchema) => void;
};

export const useBarcodeTemplatesTableColumns = ({
    onDelete,
    onChange,
}: Props) => {
    return useMemo(
        () =>
            [
                {
                    accessor: "actions",
                    title: <Center>Действия</Center>,
                    width: "0%",
                    render: template => (
                        <UpdateDeleteTableActions
                            onDelete={() => onDelete(template)}
                            onChange={() => onChange(template)}
                        />
                    ),
                },
                {
                    accessor: "name",
                    title: "Название",
                },
                {
                    accessor: "attributes",
                    title: "Атрибуты",
                    render: template => (
                        <>
                            {template.attributes
                                .map(attr => attr.name)
                                .join(", ")}
                        </>
                    ),
                },
                {
                    accessor: "size.name",
                    title: "Размер",
                    render: template => `${template.size.name} (${template.size.width}x${template.size.height})`
                },
                {
                    accessor: "isDefault",
                    title: "По умолчанию",
                    render: template =>
                        template.isDefault ? <IconCheck /> : <IconX />,
                },
            ] as DataTableColumn<BarcodeTemplateSchema>[],
        []
    );
};
