import { useCallback } from 'react';

import type { DialogButton } from './index';

interface UseActionProps {
    id?: string;
    closeDialog: (id: string) => void;
    maskClosable?: boolean;
}

export function useAction({ id, closeDialog, maskClosable = true }: UseActionProps) {
    const handleOverlayClick = useCallback(() => {
        if (id && maskClosable) {
            closeDialog(id);
        }
    }, [id, closeDialog, maskClosable]);

    const handleButtonClick = useCallback(
        (button: DialogButton, event: React.MouseEvent<HTMLButtonElement>, dialogId: string) => {
            button.onClick?.(event, dialogId);
        },
        []
    );

    return {
        handleOverlayClick,
        handleButtonClick,
    };
}
