/**
 * @license MIT
 * author: Cube Dev Team
 * @cube-dev/ui-kit v0.0.0-canary-032998c
 * Released under the MIT license.
 */

import { jsx as _jsx } from "react/jsx-runtime";
import { isValidElement, useMemo } from 'react';
import { isFragment } from 'react-is';
import { CheckIcon, DangerIcon, InfoCircleIcon, WarningIcon, } from '../../../icons/index.js';
import { useToastContext } from './ToastProvider.js';
// Default icons for each theme
const THEME_ICONS = {
    success: _jsx(CheckIcon, {}),
    danger: _jsx(DangerIcon, {}),
    warning: _jsx(WarningIcon, {}),
    note: _jsx(InfoCircleIcon, {}),
};
/**
 * Get the appropriate icon for a toast based on theme and loading state.
 * - When loading, returns the provided icon as-is (loading indicator is separate)
 * - When not loading, returns the provided icon or falls back to theme-based default
 */
export function getThemeIcon(theme, icon, isLoading) {
    // When loading, don't apply theme icons (loading indicator is shown separately)
    if (isLoading)
        return icon;
    // If icon is explicitly provided, use it
    if (icon !== undefined)
        return icon;
    // Apply theme-based default icon
    return theme ? THEME_ICONS[theme] : undefined;
}
function isToastData(data) {
    // ReactNode includes: ReactElement | string | number | boolean | null | undefined | Iterable<ReactNode>
    const isReactNode = isValidElement(data) ||
        isFragment(data) ||
        typeof data === 'string' ||
        typeof data === 'number' ||
        typeof data === 'boolean' ||
        data === null ||
        data === undefined ||
        Array.isArray(data);
    return !isReactNode;
}
function normalizeToastData(data, defaultTheme) {
    const theme = isToastData(data) ? data.theme ?? defaultTheme : defaultTheme;
    const isLoading = isToastData(data) ? data.isLoading : undefined;
    const providedIcon = isToastData(data) ? data.icon : undefined;
    const icon = getThemeIcon(theme, providedIcon, isLoading);
    if (isToastData(data)) {
        return {
            ...data,
            theme: data.theme ?? defaultTheme,
            icon,
        };
    }
    // String or ReactNode passed directly - use as title
    return {
        title: data,
        theme: defaultTheme,
        icon,
    };
}
/**
 * Hook to display toast notifications.
 *
 * @example
 * ```tsx
 * const toast = useToast();
 *
 * // String shorthand
 * toast.success('Copied to clipboard');
 *
 * // Object with options
 * toast.success({
 *   title: 'Success',
 *   description: 'File copied to clipboard',
 *   icon: <CustomIcon />,
 *   duration: 3000,
 * });
 *
 * // Methods: toast(), toast.success(), toast.danger(), toast.warning(), toast.note()
 * ```
 */
export function useToast() {
    const { addToast, removeToast } = useToastContext();
    const api = useMemo(() => {
        const toast = (data) => {
            return addToast(normalizeToastData(data));
        };
        toast.success = (data) => {
            return addToast(normalizeToastData(data, 'success'));
        };
        toast.danger = (data) => {
            return addToast(normalizeToastData(data, 'danger'));
        };
        toast.warning = (data) => {
            return addToast(normalizeToastData(data, 'warning'));
        };
        toast.note = (data) => {
            return addToast(normalizeToastData(data, 'note'));
        };
        toast.remove = (id) => {
            removeToast(id);
        };
        return toast;
    }, [addToast, removeToast]);
    return api;
}

        