'use client';
import { jsx as _jsx } from "react/jsx-runtime";
import React, { createContext, useContext, useState, useCallback, useEffect, useMemo } from 'react';
import { switchView } from '@/lib/docs-navigation';
const ModeContext = /*#__PURE__*/ createContext(null);
/**
 * Parse view mode from URL hash
 */ function parseViewFromHash() {
    if (typeof window === 'undefined') return 'docs';
    const hash = window.location.hash.slice(1);
    if (!hash) return 'docs';
    // Check for view suffix at the end
    if (hash.endsWith('/playground')) return 'playground';
    if (hash.endsWith('/notes')) return 'notes';
    return 'docs';
}
/**
 * Convert ViewMode to PlaygroundMode
 */ function viewToPlaygroundMode(view) {
    switch(view){
        case 'playground':
            return 'api_client';
        case 'notes':
            return 'notes';
        default:
            return 'docs';
    }
}
/**
 * Convert PlaygroundMode to ViewMode
 */ function playgroundModeToView(mode) {
    switch(mode){
        case 'api_client':
            return 'playground';
        case 'notes':
            return 'notes';
        default:
            return 'docs';
    }
}
export function ModeProvider({ children, defaultMode = 'docs' }) {
    // Track current hash to derive mode
    const [hash, setHash] = useState(()=>typeof window !== 'undefined' ? window.location.hash : '');
    // Derive mode from URL hash
    const mode = useMemo(()=>{
        const view = parseViewFromHash();
        return viewToPlaygroundMode(view);
    }, [
        hash
    ]);
    // Notes-specific state (not URL-based yet)
    const [activeFilePath, setActiveFilePathState] = useState(null);
    const [streamingContent, setStreamingContent] = useState(null);
    const [notesRefreshTrigger, setNotesRefreshTrigger] = useState(0);
    // Listen for hash changes to update mode
    useEffect(()=>{
        const handleHashChange = ()=>{
            setHash(window.location.hash);
        };
        window.addEventListener('hashchange', handleHashChange);
        window.addEventListener('popstate', handleHashChange);
        return ()=>{
            window.removeEventListener('hashchange', handleHashChange);
            window.removeEventListener('popstate', handleHashChange);
        };
    }, []);
    // Wrapper for setActiveFilePath
    const setActiveFilePath = useCallback((path)=>{
        // Handle the ?t= timestamp suffix for forcing reloads
        const cleanPath = path?.split('?')[0] || null;
        setActiveFilePathState(cleanPath);
    }, []);
    const switchToNotes = useCallback((openFile)=>{
        if (openFile) {
            setActiveFilePathState(openFile);
        }
        switchView('notes');
    }, []);
    const switchToApiClient = useCallback(()=>{
        switchView('playground');
    }, []);
    const switchToDocs = useCallback(()=>{
        switchView('docs');
    }, []);
    const appendStreamingContent = useCallback((delta)=>{
        setStreamingContent((prev)=>{
            if (!prev) return prev;
            return {
                ...prev,
                content: prev.content + delta
            };
        });
    }, []);
    const triggerNotesRefresh = useCallback(()=>{
        setNotesRefreshTrigger((prev)=>prev + 1);
    }, []);
    const value = {
        mode,
        activeFilePath,
        setActiveFilePath,
        streamingContent,
        setStreamingContent,
        appendStreamingContent,
        switchToDocs,
        switchToNotes,
        switchToApiClient,
        notesRefreshTrigger,
        triggerNotesRefresh
    };
    return /*#__PURE__*/ _jsx(ModeContext.Provider, {
        value: value,
        children: children
    });
}
export function useModeContext() {
    const context = useContext(ModeContext);
    if (!context) {
        throw new Error('useModeContext must be used within a ModeProvider');
    }
    return context;
}
// Hook for checking if we're in notes mode
export function useIsNotesMode() {
    const { mode } = useModeContext();
    return mode === 'notes';
}
