import { useCrudOperations } from "@/hooks/cruds/baseCrud";
import {
    BarcodeTemplateSchema,
    CreateBarcodeTemplateSchema,
    UpdateBarcodeTemplateSchema,
} from "@/lib/client";
import {
    createBarcodeTemplateMutation,
    deleteBarcodeTemplateMutation,
    updateBarcodeTemplateMutation,
} from "@/lib/client/@tanstack/react-query.gen";

type UseBarcodeTemplateOperationsProps = {
    queryKey: any[];
};

export type BarcodeTemplateCrud = {
    onCreate: (template: CreateBarcodeTemplateSchema) => void;
    onUpdate: (
        templateId: number,
        template: UpdateBarcodeTemplateSchema
    ) => void;
    onDelete: (template: BarcodeTemplateSchema) => void;
};

export const useBarcodeTemplatesCrud = ({
    queryKey,
}: UseBarcodeTemplateOperationsProps): BarcodeTemplateCrud => {
    return useCrudOperations<
        BarcodeTemplateSchema,
        UpdateBarcodeTemplateSchema,
        CreateBarcodeTemplateSchema
    >({
        key: "getBarcodeTemplates",
        queryKey,
        mutations: {
            create: createBarcodeTemplateMutation(),
            update: updateBarcodeTemplateMutation(),
            delete: deleteBarcodeTemplateMutation(),
        },
        getUpdateEntity: (old, update) => ({
            ...old,
            name: update.name ?? old.name,
            attributes: update.attributes ?? old.attributes,
            size: update.size ?? old.size,
            isDefault: update.isDefault ?? old.isDefault,
        }),
        getDeleteConfirmTitle: () => "Удаление шаблона штрихкода",
    });
};
