import { t as toast } from './index-D_roZz8G.js';

const activeToasts = new Set();
function toastKey(type, message, buttonLabel) {
    return `${type}::${message}::${buttonLabel ?? ""}`;
}
function getContentDir() {
    if (typeof document === "undefined")
        return "ltr";
    return (document.documentElement.getAttribute("dir") ??
        document.body.getAttribute("dir") ??
        "ltr");
}
function resolveMessage(message) {
    if (typeof message === "string")
        return message;
    if (message && typeof message === "object") {
        if (message.customMessage)
            return message.customMessage;
        if (message.notice)
            return message.notice;
        if (message.noticeCode)
            return message.noticeCode;
    }
    return String(message);
}
function resolveErrorMessage(errorObject) {
    if (typeof errorObject === "string")
        return errorObject;
    if (errorObject && typeof errorObject === "object") {
        // Axios-style error
        const axiosLike = errorObject;
        if (axiosLike.response?.data) {
            const data = axiosLike.response.data;
            if (data.customMessage)
                return String(data.customMessage);
            if (data.error)
                return String(data.error);
            if (Array.isArray(data.errors))
                return data.errors.join("\n");
            if (data.noticeCode)
                return String(data.noticeCode);
        }
        if (axiosLike.isAxiosError && axiosLike.message)
            return axiosLike.message;
        // Native Error
        if (errorObject instanceof Error)
            return errorObject.message;
        // ToastrMessage object
        return resolveMessage(errorObject);
    }
    return "Something went wrong.";
}
function showToast(type, message, config) {
    const { buttonLabel, onClick, showIcon, ...restConfig } = config ?? {};
    const key = toastKey(type, message, buttonLabel);
    if (activeToasts.has(key))
        return undefined;
    activeToasts.add(key);
    const options = {
        onDismiss: () => activeToasts.delete(key),
        onAutoClose: () => activeToasts.delete(key),
        position: getContentDir() === "rtl" ? "bottom-right" : "bottom-left",
        ...restConfig,
    };
    if (buttonLabel) {
        options.action = {
            label: buttonLabel,
            onClick: onClick ?? (() => { }),
        };
    }
    // When showIcon is explicitly false, hide the type icon
    if (showIcon === false) {
        options.icon = null;
    }
    return toast[type](message, options);
}
function parseConfig(args) {
    if (args.length === 0)
        return {};
    const first = args[0];
    if (typeof first === "object" && first !== null && !Array.isArray(first)) {
        return first;
    }
    // Legacy positional args: buttonLabel, onClick, customConfig, showIcon
    const [buttonLabel, onClick, customConfig, showIcon] = args;
    return {
        ...(buttonLabel ? { buttonLabel } : {}),
        ...(onClick ? { onClick } : {}),
        ...(showIcon !== undefined ? { showIcon } : {}),
        ...(customConfig ?? {}),
    };
}

const Toastr = {
    success(message, ...args) {
        return showToast("success", resolveMessage(message), parseConfig(args));
    },
    info(message, ...args) {
        return showToast("info", resolveMessage(message), parseConfig(args));
    },
    show(message, ...args) {
        return showToast("info", resolveMessage(message), parseConfig(args));
    },
    warning(message, ...args) {
        return showToast("warning", resolveMessage(message), parseConfig(args));
    },
    error(errorObject, ...args) {
        const msg = resolveErrorMessage(errorObject);
        return showToast("error", msg, parseConfig(args));
    },
};

export { Toastr as T };
//# sourceMappingURL=Toastr-DYqpSaMd.js.map
