/*
 * Copyright 2020 Netsyms Technologies.
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/**
 * Save something to persistent storage.
 * @param {string} key
 * @param {string} value non-string values are converted to strings.
 * @param {bool} nochangeupdate If true, the lastchange setting won't be updated.
 * @returns {undefined}
 */
function setStorage(key, value, nochangeupdate) {
    if (typeof nochangeupdate == 'undefined') {
        nochangeupdate = false;
    }
    localStorage.setItem(key, value);
    if (!nochangeupdate && !SETTINGS.synckeyblacklist.includes(key)) {
        localStorage.setItem("lastchange", Date.now() / 1000);
    }
}

/**
 * Get an item from persistent storage.
 * @param {type} key
 * @returns {DOMString}
 */
function getStorage(key) {
    return localStorage.getItem(key);
}

/**
 * Check if an item is in the persistent storage.
 * @param {string} key
 * @returns {Boolean}
 */
function inStorage(key) {
    return localStorage.getItem(key) != null;
}

/**
 * Get all item from persistent storage.
 * @returns {Array} [{key: "", value: ""},...]
 */
function getAllStorage() {
    var all = [];
    for (var key in localStorage) {
        if (localStorage.hasOwnProperty(key)) {
            all.push({
                key: key,
                value: getStorage(key)
            });
        }
    }
    return all;
}