// Basic encoding for API keys to prevent shoulder-surfing
// NOT secure encryption - relies on device security
const STORAGE_KEY = 'llm_provider_config';

export const SecureStorage = {
    saveKey: (apiKey: string): void => {
        if (typeof window === 'undefined') return;
        try {
            // Basic Base64 encoding
            const encoded = btoa(apiKey);
            localStorage.setItem(STORAGE_KEY, encoded);
        } catch (e) {
            console.error('Failed to save API key', e);
        }
    },

    getKey: (): string | null => {
        if (typeof window === 'undefined') return null;
        try {
            const encoded = localStorage.getItem(STORAGE_KEY);
            if (!encoded) return null;
            // Basic Base64 decoding
            return atob(encoded);
        } catch (e) {
            console.error('Failed to retrieve API key', e);
            return null;
        }
    },

    clearKey: (): void => {
        if (typeof window === 'undefined') return;
        localStorage.removeItem(STORAGE_KEY);
    },
};
