"use client";

import { createContext, useContext, useState, useCallback, type ReactNode } from "react";

type EntryMutationContextValue = {
    mutationVersion: number;
    notifyEntryMutation: () => void;
};

const EntryMutationContext = createContext<EntryMutationContextValue | null>(null);

export function EntryMutationProvider({ children }: { children: ReactNode }) {
    const [mutationVersion, setMutationVersion] = useState(0);

    const notifyEntryMutation = useCallback(() => {
        setMutationVersion(prev => prev + 1);
    }, []);

    return (
        <EntryMutationContext.Provider value={{ mutationVersion, notifyEntryMutation }}>
            {children}
        </EntryMutationContext.Provider>
    );
}

export function useEntryMutation() {
    const context = useContext(EntryMutationContext);
    if (!context) throw new Error("useEntryMutation must be used within EntryMutationProvider");
    return context;
}
