import {notifications} from "@mantine/notifications";

export function showErrorMessage(msg: string, title: string = "Error", autoClose: boolean | number = 10000) {
    console.log("ErrorMessage:", title, msg)
    notifications.show({
        title: title,
        message: msg,
        color: 'red',
        autoClose: autoClose
    })
}

export function showWarningMessage(msg: string, title: string = "Warning", autoClose: boolean | number = 10000) {
    console.log("WarningMessage:", title, msg)
    notifications.show({
        title: title,
        message: msg,
        color: 'yellow',
        autoClose: autoClose,
    })
}

export function showInfoMessage(msg: string, title: string = "Info", autoClose: boolean | number = 10000) {
    notifications.show({
        title: title,
        message: msg,
        color: 'blue',
        autoClose: autoClose,
    })
}

export function getEnumKeyByValue<T extends Record<string, string>>(
    enumObj: T,
    value: string
): keyof T | undefined {
    return (Object.keys(enumObj) as Array<keyof T>)
        .find(k => enumObj[k] === value);
}


export function convertNumber(value: number, multiplier: number, decimalPlaces: number) {
    if (!Number.isFinite(value) || !Number.isFinite(multiplier)) {
        return "NaN"
    }

    if (!Number.isInteger(decimalPlaces) || decimalPlaces < 0) {
        return "NaN"
    }

    const result = value * multiplier;

    return result.toFixed(decimalPlaces);
}


export const sleep = (ms: number): Promise<void> => {
    return new Promise(resolve => setTimeout(resolve, ms));
};
