const PREFIX = "parhelia.";
export const localStorageService = {
    getString(key) {
        try {
            if (typeof window === "undefined") {
                return null;
            }
            return window.localStorage.getItem(`${PREFIX}${key}`);
        }
        catch {
            return null;
        }
    },
    setString(key, value) {
        try {
            if (typeof window !== "undefined") {
                window.localStorage.setItem(`${PREFIX}${key}`, value);
            }
        }
        catch {
            // Silently fail
        }
    },
    removeItem(key) {
        try {
            if (typeof window !== "undefined") {
                window.localStorage.removeItem(`${PREFIX}${key}`);
            }
        }
        catch {
            // Silently fail
        }
    },
    getItem(key) {
        try {
            if (typeof window === "undefined") {
                return null;
            }
            const raw = window.localStorage.getItem(`${PREFIX}${key}`);
            if (raw === null) {
                return null;
            }
            return JSON.parse(raw);
        }
        catch {
            return null;
        }
    },
    setItem(key, value) {
        try {
            if (typeof window !== "undefined") {
                window.localStorage.setItem(`${PREFIX}${key}`, JSON.stringify(value));
            }
        }
        catch {
            // Silently fail
        }
    },
    getOrSetItem(key, defaultValue) {
        try {
            if (typeof window === "undefined") {
                return defaultValue;
            }
            const existing = localStorageService.getItem(key);
            if (existing !== null) {
                return existing;
            }
            // Value doesn't exist, set and return default
            localStorageService.setItem(key, defaultValue);
            return defaultValue;
        }
        catch {
            return defaultValue;
        }
    },
    removeAllMatchingKeys(pattern = "") {
        try {
            if (typeof window === "undefined") {
                return;
            }
            const keysToRemove = [];
            const prefixPattern = pattern == "" ? PREFIX : `${PREFIX}${pattern}`;
            for (let i = 0; i < window.localStorage.length; i++) {
                const key = window.localStorage.key(i);
                if (key) {
                    if (key.startsWith(prefixPattern)) {
                        keysToRemove.push(key);
                    }
                }
            }
            keysToRemove.forEach((key) => {
                window.localStorage.removeItem(key);
            });
        }
        catch {
            // Silently fail
        }
    },
};
//# sourceMappingURL=localStorageService.js.map