import { useEffect, useState } from 'react';
import { getCachedParsedProfile } from '../utils/profileServiceCache';
/**
 * Hook to load and cache rich text profiles
 * Uses simplified service-level caching for parsed profiles
 */
export function useRichTextProfile(profilePath, serviceCall, options = {}) {
    const { onError } = options;
    const [state, setState] = useState({
        profile: null,
        loading: false,
        error: null
    });
    useEffect(() => {
        if (profilePath === undefined) {
            setState({
                profile: null,
                loading: false,
                error: null
            });
            return;
        }
        const loadProfile = async () => {
            setState(prev => ({ ...prev, loading: true, error: null }));
            try {
                const parsedProfile = await getCachedParsedProfile(profilePath, serviceCall);
                setState({
                    profile: parsedProfile,
                    loading: false,
                    error: null
                });
            }
            catch (error) {
                const errorObj = error instanceof Error ? error : new Error('Failed to load profile');
                setState({
                    profile: null,
                    loading: false,
                    error: errorObj
                });
                if (onError) {
                    onError(errorObj);
                }
                else {
                    console.error("RTE loadProfile - error:", errorObj);
                }
            }
        };
        loadProfile();
    }, [profilePath, serviceCall, onError]);
    return state;
}
/**
 * Hook that returns the fully parsed profile object
 * This is now the main hook to use for rich text profiles
 */
export function useRichTextProfileParsed(profilePath, serviceCall) {
    const { profile, loading, error } = useRichTextProfile(profilePath, serviceCall);
    return {
        profile,
        loading,
        error
    };
}
//# sourceMappingURL=useRichTextProfile.js.map