/**
 * Secure storage utility for browser-side code
 * Uses Electron's secure storage via IPC
 */ class SecureStorage {
    static cache = new Map();
    /**
   * Store sensitive data securely
   * @param {string} key - The key to store the data under
   * @param {string} value - The sensitive value to store
   */ static async set(key, value) {
        if (window.electronAPI?.setSecureValue) {
            const result = await window.electronAPI.setSecureValue(key, value);
            if (result?.success) {
                this.cache.set(key, value);
            }
            return result;
        } else {
            // Fallback for non-electron environments
            console.warn("Secure storage not available, using session storage");
            sessionStorage.setItem(key, value);
            this.cache.set(key, value);
            return {
                success: true
            };
        }
    }
    /**
   * Retrieve sensitive data securely
   * @param {string} key - The key to retrieve data for
   * @returns {Promise<string|null>} The stored value or null if not found
   */ static async get(key) {
        // Check cache first
        if (this.cache.has(key)) {
            return this.cache.get(key);
        }
        if (window.electronAPI?.getSecureValue) {
            const value = await window.electronAPI.getSecureValue(key);
            if (value) {
                this.cache.set(key, value);
            }
            return value;
        } else {
            // Fallback
            const value = sessionStorage.getItem(key);
            if (value) {
                this.cache.set(key, value);
            }
            return value;
        }
    }
    /**
   * Remove sensitive data
   * @param {string} key - The key to remove
   */ static async remove(key) {
        this.cache.delete(key);
        if (window.electronAPI?.removeSecureValue) {
            return await window.electronAPI.removeSecureValue(key);
        } else {
            sessionStorage.removeItem(key);
            return {
                success: true
            };
        }
    }
    /**
   * Clear all cached values
   */ static clearCache() {
        this.cache.clear();
    }
}
// Helper functions for API key management
let migrating = false;
async function getApiKey() {
    // First try secure storage
    let apiKey = await SecureStorage.get("apiKey");
    // Migration: if not in secure storage but in localStorage, migrate it
    // Use flag to prevent circular calls
    if (!apiKey && !migrating && localStorage.getItem("apiKey")) {
        migrating = true;
        try {
            apiKey = localStorage.getItem("apiKey");
            await setApiKey(apiKey);
            localStorage.removeItem("apiKey"); // Remove from insecure storage
        } finally{
            migrating = false;
        }
    }
    return apiKey;
}
async function setApiKey(apiKey) {
    return SecureStorage.set("apiKey", apiKey);
}
async function removeApiKey() {
    localStorage.removeItem("apiKey"); // Also clean up localStorage
    return SecureStorage.remove("apiKey");
}
// Make functions available globally for browser usage
if (typeof window !== "undefined") {
    window.SecureStorage = SecureStorage;
    window.getApiKey = getApiKey;
    window.setApiKey = setApiKey;
    window.removeApiKey = removeApiKey;
}

//# sourceMappingURL=secureStorageBrowser.js.map