"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StreamNotificationProvider = StreamNotificationProvider;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const lucide_react_native_1 = require("lucide-react-native");
const react_1 = require("react");
const react_native_1 = require("react-native");
const react_native_reanimated_1 = tslib_1.__importStar(require("react-native-reanimated"));
const __1 = require("../../");
const stream_notification_manager_1 = require("./stream-notification-manager");
function StreamNotificationProvider({ children = (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {}), position = "top", }) {
    const [notifications, setNotifications] = (0, react_1.useState)(stream_notification_manager_1.streamNotificationManager.getAll());
    (0, react_1.useEffect)(() => {
        return stream_notification_manager_1.streamNotificationManager.subscribe(setNotifications);
    }, []);
    return ((0, jsx_runtime_1.jsxs)(react_native_1.View, { style: styles.container, children: [children, notifications.map((notification, index) => ((0, jsx_runtime_1.jsx)(NotificationItem, { notification: notification, index: index, position: position }, notification.id)))] }));
}
function NotificationItem({ notification, index, position, }) {
    const { theme } = (0, __1.useTheme)();
    const translateY = (0, react_native_reanimated_1.useSharedValue)(position === "top" ? -100 : 100);
    const opacity = (0, react_native_reanimated_1.useSharedValue)(0);
    const [isExiting, setIsExiting] = (0, react_1.useState)(false);
    const NOTIFICATION_HEIGHT = 60;
    const NOTIFICATION_GAP = 8;
    const offset = 16 + index * (NOTIFICATION_HEIGHT + NOTIFICATION_GAP);
    (0, react_1.useEffect)(() => {
        translateY.value = (0, react_native_reanimated_1.withTiming)(position === "top" ? offset : -offset, {
            duration: 300,
            easing: react_native_reanimated_1.Easing.out(react_native_reanimated_1.Easing.cubic),
        });
        opacity.value = (0, react_native_reanimated_1.withTiming)(1, {
            duration: 200,
        });
    }, [offset, position]);
    (0, react_1.useEffect)(() => {
        if (notification.shouldDismiss && !isExiting) {
            setIsExiting(true);
            setTimeout(() => {
                stream_notification_manager_1.streamNotificationManager.hide(notification.id, notification.dismissReason || "auto");
            }, 200);
        }
    }, [
        notification.shouldDismiss,
        isExiting,
        notification.id,
        notification.dismissReason,
    ]);
    (0, react_1.useEffect)(() => {
        if (isExiting) {
            translateY.value = (0, react_native_reanimated_1.withTiming)(position === "top" ? -100 : 100, {
                duration: 200,
                easing: react_native_reanimated_1.Easing.in(react_native_reanimated_1.Easing.cubic),
            });
            opacity.value = (0, react_native_reanimated_1.withTiming)(0, {
                duration: 200,
            });
        }
    }, [isExiting, position]);
    const animatedStyle = (0, react_native_reanimated_1.useAnimatedStyle)(() => ({
        transform: [{ translateY: translateY.value }],
        opacity: opacity.value,
    }));
    const variantStyles = {
        default: {
            backgroundColor: theme.colors.card,
            borderColor: theme.colors.border,
        },
        info: {
            backgroundColor: theme.colors.info,
            borderColor: theme.colors.info,
        },
        warning: {
            backgroundColor: theme.colors.warning,
            borderColor: theme.colors.warning,
        },
    };
    const handleDismiss = (reason = "user") => {
        console.log("Dismissing notification:", notification.id);
        setIsExiting(true);
        setTimeout(() => {
            console.log("Requesting dismiss for notification:", notification.id);
            stream_notification_manager_1.streamNotificationManager.hide(notification.id, reason);
        }, 200);
        console.log(stream_notification_manager_1.streamNotificationManager.getAll());
    };
    const handleAction = () => {
        notification.onAction?.();
        stream_notification_manager_1.streamNotificationManager.hide(notification.id, "user");
    };
    const positionStyle = position === "top" ? { top: 0 } : { bottom: 0 };
    return ((0, jsx_runtime_1.jsx)(react_native_reanimated_1.default.View, { style: [
            styles.notification,
            positionStyle,
            notification.render
                ? {}
                : variantStyles[notification.variant || "default"],
            { margin: 0, padding: 0 },
            animatedStyle,
        ], children: notification.render ? (notification.render(isExiting, handleDismiss, notification.startTime)) : ((0, jsx_runtime_1.jsxs)(react_native_1.View, { style: styles.content, children: [(0, jsx_runtime_1.jsx)(__1.Text, { style: [styles.message, { color: theme.colors.foreground }], children: notification.message }), (0, jsx_runtime_1.jsxs)(react_native_1.View, { style: styles.actions, children: [notification.actionLabel && ((0, jsx_runtime_1.jsx)(react_native_1.Pressable, { onPress: handleAction, children: (0, jsx_runtime_1.jsx)(__1.Text, { style: [styles.actionButton, { color: theme.colors.primary }], children: notification.actionLabel }) })), (0, jsx_runtime_1.jsx)(react_native_1.Pressable, { onPress: () => handleDismiss("user"), style: styles.closeButton, children: (0, jsx_runtime_1.jsx)(lucide_react_native_1.X, { size: 16, color: theme.colors.mutedForeground }) })] })] })) }));
}
const styles = react_native_1.StyleSheet.create({
    container: {
        flex: 1,
        pointerEvents: "box-none",
    },
    notification: {
        position: "absolute",
        top: 0,
        left: 16,
        right: 16,
        zIndex: 9999,
        borderRadius: 8,
        borderWidth: 1,
        padding: 12,
        shadowColor: "#000",
        shadowOffset: { width: 0, height: 2 },
        shadowOpacity: 0.25,
        shadowRadius: 8,
        elevation: 5,
    },
    content: {
        flexDirection: "row",
        alignItems: "center",
        justifyContent: "space-between",
        gap: 12,
    },
    message: {
        flex: 1,
        fontSize: 14,
        fontWeight: "500",
    },
    actions: {
        flexDirection: "row",
        alignItems: "center",
        gap: 12,
    },
    actionButton: {
        fontSize: 14,
        fontWeight: "600",
    },
    closeButton: {
        padding: 4,
    },
});
//# sourceMappingURL=stream-notification.js.map