import { create } from "zustand";

export enum ALERT_MODES {
    SUCCESS = "success",
    ERROR = "error",
    WARNING = "warning",
    INFO = "info",
}

interface IAlertStore {
    visible: boolean;
    message: string;
    mode: ALERT_MODES;
    hiddingTimeout: number;
    setVisible: (state: boolean) => void;
    showSuccess: (message: string) => void;
    showError: (message: string) => void;
    showWarning: (message: string) => void;
    showInfo: (message: string) => void;
    clear: () => void;
}

export type AlertState = IAlertStore;

const useAlert = create<AlertState>()((set) => ({
    visible: false,
    message: "",
    mode: ALERT_MODES.SUCCESS,
    hiddingTimeout: 6000,
    setVisible: (state: boolean) => set(() => ({ visible: state })),
    showSuccess: (message: string) =>
        set(() => ({
            message: message,
            visible: true,
            mode: ALERT_MODES.SUCCESS,
            hiddingTimeout: 1000,
        })),
    showError: (message: string) =>
        set(() => ({
            message: message,
            visible: true,
            mode: ALERT_MODES.ERROR,
            hiddingTimeout: 6000,
        })),
    showWarning: (message: string) =>
        set(() => ({
            message: message,
            visible: true,
            mode: ALERT_MODES.WARNING,
            hiddingTimeout: 6000,
        })),
    showInfo: (message: string) =>
        set(() => ({
            message: message,
            visible: true,
            mode: ALERT_MODES.INFO,
            hiddingTimeout: 6000,
        })),
    clear: () =>
        set(() => ({
            visible: false,
            message: "",
            mode: ALERT_MODES.SUCCESS,
            hiddingTimeout: 6000,
        })),
}));

export { useAlert };
