import Swal from 'sweetalert2';

export const alertError = async (message: string) => {
    return Swal.fire({
        icon: 'error',
        title: 'Oops...',
        text: message,
    });
};

export const alertSuccess = async (message: string) => {
    return Swal.fire({
        icon: 'success',
        title: 'Success',
        text: message,
    });
};

export const alertWarning = async (message: string) => {
    return Swal.fire({
        icon: 'warning',
        title: 'Warning',
        text: message,
    });
};

export const alertInfo = async (message: string) => {
    return await Swal.fire({
        icon: 'info',
        title: 'Info',
        text: message,
    });
};

export const alertConfirm = async (message: string) => {
    return await Swal.fire({
        icon: 'question',
        title: 'Confirmation',
        text: message,
        showCancelButton: true,
        confirmButtonText: 'Yes',
        cancelButtonText: 'No',
    });
};

export const passwordPrompt = async ({ message, callback }: { message: string; callback: () => boolean | Promise<boolean> }) => {
    return await Swal.fire({
        input: 'password',
        title: message,
        inputLabel: 'Masukan Password',
        showCancelButton: true,
        inputPlaceholder: 'Masukan password',
        inputAttributes: {
            maxlength: '10',
            autocapitalize: 'off',
            autocorrect: 'off',
        },
        inputValidator: (value) => {
            if (!value) {
                return 'Password tidak boleh kosong!';
            }
        },
        preConfirm: async (password) => {
            console.log(password);
            Swal.showLoading();
            await callback();
        },
        allowOutsideClick: () => !Swal.isLoading(),
    }).then((result) => {
        if (result.isConfirmed) {
            return { isConfirmed: true, password: result.value };
        } else {
            return { isConfirmed: false, password: '' };
        }
    });
};
