import { watch } from 'vue';
import type { Ref } from 'vue';

interface UseFormDialogOptions<T> {
    open: Ref<boolean>;
    source: () => T;
    populate: (value: T) => void;
    reset: () => void;
}

export function useFormDialog<T>({
    open,
    source,
    populate,
    reset,
}: UseFormDialogOptions<T>) {
    watch(source, populate);

    watch(open, (isOpen) => {
        if (isOpen) {
            populate(source());
        } else {
            reset();
        }
    });
}
