/**
 * Simple in-memory storage fallback for environments where localStorage is unavailable.
 * Stores data for the duration of the session only.
 */
class MemoryStorage {
    constructor() {
        this.data = new Map();
    }
    getItem(key) {
        var _a;
        return (_a = this.data.get(key)) !== null && _a !== void 0 ? _a : null;
    }
    setItem(key, value) {
        this.data.set(key, value);
    }
    removeItem(key) {
        this.data.delete(key);
    }
    clear() {
        this.data.clear();
    }
    key(index) {
        var _a;
        const keys = Array.from(this.data.keys());
        return (_a = keys[index]) !== null && _a !== void 0 ? _a : null;
    }
    get length() {
        return this.data.size;
    }
}
/**
 * Get or create the appropriate storage backend.
 * Uses localStorage if available, falls back to in-memory storage.
 */
function getStorageBackend() {
    try {
        // Check if localStorage is available and not just a falsy value
        if (typeof window !== "undefined" && window.localStorage) {
            return window.localStorage;
        }
        return new MemoryStorage();
    }
    catch (_a) {
        // localStorage is unavailable (private mode, disabled, etc.)
        return new MemoryStorage();
    }
}
/**
 * This class is named "Browser" and not "Local" on purpose in case
 * we decide to use something else than local storage.
 */
export class BrowserStorage {
    constructor(group) {
        this.group = group;
        this.storage = getStorageBackend();
    }
    /**
     * Sets the group under which to save a value.
     *
     * E.g. as a group of "global" and a key of "thing", the location
     * in storage would be "global.thing"
     *
     * @param group Unique key to group under
     */
    setGroup(group) {
        this.group = group;
    }
    /**
     * @returns Currently set group name
     */
    getGroup() {
        return this.group;
    }
    /**
     * Returns a value stored in storage
     *
     * @param key The key under the group "group.key"
     * @returns String value saved under "group.key"
     */
    getItem(key) {
        return this.storage.getItem(this.group ? `${this.group}.${key}` : key);
    }
    /**
     * Saves a new value in storage
     *
     * @param key The key under the group "group.key"
     * @param value String value to save
     */
    setItem(key, value) {
        this.storage.setItem(this.group ? `${this.group}.${key}` : key, value);
    }
    /**
     * Removes a stored value
     *
     * @param key The key under the group "group.key"
     */
    removeItem(key) {
        this.storage.removeItem(this.group ? `${this.group}.${key}` : key);
    }
}
export default new BrowserStorage();
//# sourceMappingURL=browserStorage.js.map