/**
 * Toast Notifications System
 *
 * Provides non-intrusive feedback for user actions:
 * - Success, error, warning, info toasts
 * - Auto-dismiss with configurable timeout
 * - Stack multiple notifications
 * - Animations and visual feedback
 */
import React, { useState, useEffect, useCallback } from 'react';
import { Box, Text } from 'ink';
import { useTheme } from '../context/theme-context.js';
/**
 * Single Toast Component
 */
function ToastItem({ toast, onDismiss, config }) {
    const [remainingTime, setRemainingTime] = useState(toast.duration || 0);
    const [isExiting, setIsExiting] = useState(false);
    useEffect(() => {
        if (!toast.duration || toast.duration === 0)
            return;
        // Update remaining time every 100ms
        const interval = setInterval(() => {
            setRemainingTime((prev) => {
                const newTime = Math.max(0, prev - 100);
                if (newTime === 0) {
                    // Start exit animation
                    setIsExiting(true);
                    // Dismiss after animation (200ms)
                    setTimeout(() => onDismiss(toast.id), 200);
                }
                return newTime;
            });
        }, 100);
        return () => clearInterval(interval);
    }, [toast.duration, toast.id, onDismiss]);
    // Calculate progress bar for remaining time
    const progress = toast.duration && toast.duration > 0
        ? (remainingTime / toast.duration) * 100
        : 100;
    const progressBarWidth = 30;
    const filledWidth = Math.round((progress / 100) * progressBarWidth);
    const progressBar = '─'.repeat(Math.max(0, filledWidth));
    return (React.createElement(Box, { flexDirection: "column", borderStyle: "round", borderColor: config.borderColor, paddingX: 1, marginBottom: 1 },
        React.createElement(Box, null,
            React.createElement(Text, { color: config.color },
                config.icon,
                " "),
            React.createElement(Text, null, toast.message)),
        toast.duration && toast.duration > 0 && !isExiting && (React.createElement(Box, { marginTop: 0 },
            React.createElement(Text, { color: config.color, dimColor: true }, progressBar),
            React.createElement(Text, { dimColor: true }, ' '.repeat(Math.max(0, progressBarWidth - filledWidth))),
            React.createElement(Text, { dimColor: true },
                " ",
                (remainingTime / 1000).toFixed(1),
                "s"))),
        isExiting && (React.createElement(Box, null,
            React.createElement(Text, { dimColor: true }, "Dismissing...")))));
}
/**
 * Toast Notifications Manager Component
 */
export function ToastNotifications({ maxToasts = 3, defaultDuration: _defaultDuration = 3000, position = 'bottom', }) {
    const { colors } = useTheme();
    const [toasts, setToasts] = useState([]);
    // Toast configurations by type
    const toastConfigs = {
        success: {
            icon: '✓',
            color: colors.success,
            borderColor: colors.success,
        },
        error: {
            icon: '✗',
            color: colors.error,
            borderColor: colors.error,
        },
        warning: {
            icon: '⚠',
            color: colors.warning,
            borderColor: colors.warning,
        },
        info: {
            icon: 'ℹ',
            color: colors.info,
            borderColor: colors.info,
        },
    };
    // Dismiss toast
    const dismissToast = useCallback((id) => {
        setToasts((prev) => prev.filter((t) => t.id !== id));
    }, []);
    // Limit number of visible toasts
    const visibleToasts = toasts.slice(0, maxToasts);
    if (visibleToasts.length === 0)
        return null;
    return (React.createElement(Box, { flexDirection: "column", marginTop: position === 'top' ? 0 : 1, marginBottom: position === 'bottom' ? 0 : 1 },
        visibleToasts.map((toast) => (React.createElement(ToastItem, { key: toast.id, toast: toast, onDismiss: dismissToast, config: toastConfigs[toast.type] }))),
        toasts.length > maxToasts && (React.createElement(Box, null,
            React.createElement(Text, { dimColor: true },
                "+",
                toasts.length - maxToasts,
                " more notification",
                toasts.length - maxToasts !== 1 ? 's' : '')))));
}
/**
 * Toast Manager Hook
 *
 * Provides imperative API to show toasts
 */
export function useToastManager() {
    const [toasts, setToasts] = useState([]);
    const showToast = useCallback((type, message, duration) => {
        const id = `toast-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
        const toast = {
            id,
            type,
            message,
            duration: duration ?? 3000,
            createdAt: Date.now(),
        };
        setToasts((prev) => [...prev, toast]);
        return id;
    }, []);
    const dismissToast = useCallback((id) => {
        setToasts((prev) => prev.filter((t) => t.id !== id));
    }, []);
    const clearAllToasts = useCallback(() => {
        setToasts([]);
    }, []);
    // Convenience methods
    const success = useCallback((message, duration) => {
        return showToast('success', message, duration);
    }, [showToast]);
    const error = useCallback((message, duration) => {
        return showToast('error', message, duration);
    }, [showToast]);
    const warning = useCallback((message, duration) => {
        return showToast('warning', message, duration);
    }, [showToast]);
    const info = useCallback((message, duration) => {
        return showToast('info', message, duration);
    }, [showToast]);
    return {
        toasts,
        showToast,
        dismissToast,
        clearAllToasts,
        success,
        error,
        warning,
        info,
    };
}
/**
 * Toast Manager Context
 *
 * Provides toast functionality throughout the app
 */
import { createContext, useContext } from 'react';
const ToastContext = createContext(null);
/**
 * Toast Provider Component
 */
export function ToastProvider({ children }) {
    const toastManager = useToastManager();
    return (React.createElement(ToastContext.Provider, { value: {
            showToast: toastManager.showToast,
            dismissToast: toastManager.dismissToast,
            clearAllToasts: toastManager.clearAllToasts,
            success: toastManager.success,
            error: toastManager.error,
            warning: toastManager.warning,
            info: toastManager.info,
        } },
        children,
        React.createElement(ToastNotifications, null)));
}
/**
 * Hook to access toast functionality
 */
export function useToast() {
    const context = useContext(ToastContext);
    if (!context) {
        throw new Error('useToast must be used within ToastProvider');
    }
    return context;
}
export default ToastNotifications;
//# sourceMappingURL=ToastNotifications.js.map