{"size": 1766, "ext": "js", "max_stars_repo_path": "lib/storage.js", "max_stars_repo_name": "SamsungInternet/xravatar", "max_stars_repo_stars_event_min_datetime": "2021-07-18T22:25:54.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-18T22:25:54.000Z", "max_issues_repo_path": "lib/storage.js", "max_issues_repo_name": "SamsungInternet/xravatar", "max_issues_repo_issues_event_min_datetime": "2019-12-02T18:22:11.000Z", "max_issues_repo_issues_event_max_datetime": "2022-01-27T16:08:40.000Z", "max_forks_repo_path": "lib/storage.js", "max_forks_repo_name": "SamsungInternet/xravatar", "max_forks_repo_forks_event_min_datetime": "2021-07-18T22:25:55.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-18T22:25:55.000Z"}
{"max_stars_count": 1, "max_issues_count": 17, "max_forks_count": 1, "avg_line_length": 26.7575757576, "max_line_length": 100, "alphanum_fraction": 0.7100792752}
// global localforage
import '../node_modules/localforage/dist/localforage.min.js';
import {
    createEmptyState
} from './schema.js'

const avatarKey = 'xravatar-avatar';
const savedDomainsKey = 'xravatar-saved-domains'
const localStateKey = 'xravatar-local-state';

let stateCache;
async function getState() {
    const state = stateCache || new Map(Object.entries(await getAvatar() || createEmptyState()));
    return state;
}

let localStateCache;
async function getLocalState() {
    if (localStateCache) return localStateCache;
    const localState = await window.localforage
    .getItem(localStateKey)
    .catch(() => undefined);
    const state = new Map(Object.entries(localState || {}));
    return state;
}

async function getAvatar() {
    return await window.localforage.getItem(avatarKey).catch(() => undefined);
}

async function storeAvatar(stateAsObject) {
    return await window.localforage.setItem(avatarKey, stateAsObject);
}

async function canDoLocalStorage() {
    return window.localforage
    .getItem(avatarKey)
    .then(() => true)
    .catch(() => false);
}

async function getSavedDomains() {
    return (await (window.localforage.getItem(savedDomainsKey).catch(() => false))) || [];
}

async function getAvatarJSON() {
    return JSON.stringify(await getAvatar());
}

async function getAvatarCompressed() {
    const avatarString = JSON.stringify(await getAvatar());
    const compressed = await new Promise(resolve => window.LZMA.compress(avatarString, 6, resolve));
    const base64 = btoa(String.fromCharCode(...compressed.map(i => i + 128)));
    return base64;
}

export {
    getAvatar,
    getAvatarCompressed,
    getSavedDomains,
    canDoLocalStorage,
    getAvatarJSON,
    storeAvatar,
    getLocalState,
    getState
}