import { createDraftStore } from '@/lib/draft-store';

export type PurchaseDraftData = {
    name: string;
    unit: string;
    supplierId: string;
    discount: number;
    shippingCost: number;
    notes: string;
    variants: Array<{
        variant: string;
        price: number;
        stock: number;
        photo?: string;
    }>;
    mode?: 'new' | 'existing';
    selectedMaterialName?: string;
    quantities?: Record<string, number>;
    photo?: string;
};

export const purchaseDraftStore =
    createDraftStore<PurchaseDraftData>('purchase-draft');

export function savePurchaseDraft(
    type: 'create' | 'edit',
    data: PurchaseDraftData,
    userId?: number,
): boolean {
    return purchaseDraftStore.save(type, data, userId);
}

export function loadPurchaseDraft(
    type: 'create' | 'edit',
    userId?: number,
): PurchaseDraftData | null {
    return purchaseDraftStore.load(type, userId);
}

export function clearPurchaseDraft(
    type: 'create' | 'edit',
    userId?: number,
): void {
    purchaseDraftStore.clear(type, userId);
}
