import { modals } from "@mantine/modals";
import { useBarcodeTemplatesCrud } from "@/app/barcode-templates/hooks/useBarcodeTemplatesCrud";
import useBarcodeTemplatesList from "@/app/barcode-templates/hooks/useBarcodeTemplatesList";
import { BarcodeTemplateSchema } from "@/lib/client";

const useBarcodeTemplateActions = () => {
    const { queryKey } = useBarcodeTemplatesList();
    const barcodeTemplatesCrud = useBarcodeTemplatesCrud({ queryKey });

    const onChange = (template: BarcodeTemplateSchema) => {
        modals.openContextModal({
            modal: "barcodeTemplateEditorModal",
            title: "Редактирование шаблона",
            withCloseButton: false,
            innerProps: {
                onChange: updated =>
                    barcodeTemplatesCrud.onUpdate(template.id, updated),
                entity: template,
                isEditing: true,
            },
        });
    };

    const onCreate = () => {
        modals.openContextModal({
            modal: "barcodeTemplateEditorModal",
            title: "Создание шаблона",
            withCloseButton: false,
            innerProps: {
                onCreate: barcodeTemplatesCrud.onCreate,
                isEditing: false,
            },
        });
    };

    return {
        onChange,
        onCreate,
    };
};

export default useBarcodeTemplateActions;
