class FallbackStorage {
    constructor() {
        this.items = {};
    }
    clear() {
        this.items = {};
    }
    getItem(key) {
        return this.items[key] || null;
    }
    setItem(key, data) {
        this.items[key] = data;
    }
    removeItem(key) {
        delete this.items[key];
    }
}
/**
 * Per the HTML spec browsers are allowed to throw exceptions for (local/session)Storage so it needs to be wrapped.
 * See: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage#Exceptions
 */
class StorageWrapper {
    constructor(storage) {
        this.storage = storage;
    }
    static hasStorage(key) {
        try {
            const uid = `anjHasStorage${+new Date()}`;
            const storage = window[key];
            storage.setItem(uid, uid);
            const test = storage.getItem(uid);
            storage.removeItem(uid);
            return test === uid;
        }
        catch (_ex) {
            return false;
        }
    }
    clear() {
        return this.storage.clear();
    }
    getItem(key) {
        return JSON.parse(this.storage.getItem(key));
    }
    removeItem(key) {
        return this.storage.removeItem(key);
    }
    setItem(key, data) {
        return this.storage.setItem(key, JSON.stringify(data));
    }
}
class SessionStorage extends StorageWrapper {
    constructor() {
        if (SessionStorage.hasStorage('sessionStorage')) {
            super(window.sessionStorage);
        }
        else {
            super(new FallbackStorage());
        }
    }
}
class LocalStorage extends StorageWrapper {
    constructor() {
        if (LocalStorage.hasStorage('localStorage')) {
            super(window.localStorage);
        }
        else {
            super(new FallbackStorage());
        }
    }
}

export { FallbackStorage as F, LocalStorage as L, StorageWrapper as S, SessionStorage as a };
