/*!
 * Custom elements for Nanopore-Digital Web applications
 */
import { parse, stringify } from "tyqs";
const STORAGE_NAME_SPACE = 'nano-component-store:';
const urlRead = (from = 'hash', win = window) => {
    if (!win || !win.location)
        return {};
    if (from === 'hash' && win.location.hash && win.location.hash.match(/&|=/))
        return parse(win.location.hash.replace(/^#/, ''));
    else if (from === 'query' &&
        win.location.search &&
        win.location.search.match(/&|=/))
        return parse(win.location.search.replace(/^\?/, ''));
    return {};
};
const urlSave = (object, to = 'hash', win = window, method = 'replace') => {
    if (!win || !win.history)
        return;
    const oldQuery = to === 'query' ? win.location.search : win.location.hash;
    const basePath = to === 'query'
        ? win.location.pathname
        : win.location.pathname + win.location.search;
    let queryStr = stringify(object);
    if (!queryStr || !queryStr.length) {
        if (!oldQuery || !oldQuery.length)
            return;
        if (method === 'replace')
            win.history.replaceState(null, null, basePath);
        else
            win.history.pushState(null, null, basePath);
        return;
    }
    if (to === 'hash' && win.location.hash === '#' + queryStr) {
        try {
            document.querySelector('#' + queryStr)?.focus();
            // eslint-disable-next-line no-empty
        }
        catch (_e) { }
        return;
    }
    queryStr = (to === 'hash' ? '#' : '?') + queryStr;
    if (queryStr === oldQuery)
        return;
    if (method === 'replace')
        win.history.replaceState(null, null, basePath + queryStr);
    else
        win.history.pushState(null, null, basePath + queryStr);
};
export const urlSet = (id, data, to = 'hash', win = window, method = 'replace') => {
    // sanitize the incoming key
    id = Object.keys(parse(id))[0];
    const object = {};
    // delete all empty incoming values
    for (const key in data) {
        if (!data[key] || (Array.isArray(data[key]) && !data[key].length)) {
            delete data[key];
        }
    }
    object[id] = data;
    let currentData = urlRead(to, win);
    // current data is empty and incoming data is empty
    // let's not do anything
    if (!currentData[id] && !object[id])
        return;
    // let's merge current and incoming data and write it to the url
    currentData = { ...currentData, ...object };
    urlSave(currentData, to, win, method);
};
export const urlGet = (key, to = 'hash', win = window) => {
    // sanitize the incoming key
    key = Object.keys(parse(key))[0];
    const currentData = urlRead(to, win);
    return currentData[key] || null;
};
export const sessionSet = (key, object, win = window) => {
    if (!object) {
        win.sessionStorage.removeItem(STORAGE_NAME_SPACE + key);
        return;
    }
    win.sessionStorage.setItem(STORAGE_NAME_SPACE + key, JSON.stringify(object));
};
export const sessionGet = (key, win = window) => {
    const configStr = win.sessionStorage.getItem(STORAGE_NAME_SPACE + key);
    return configStr !== null ? JSON.parse(configStr) : null;
};
export const storageSet = (key, object, win = window) => {
    if (!object) {
        win.localStorage.removeItem(STORAGE_NAME_SPACE + key);
        return;
    }
    win.localStorage.setItem(STORAGE_NAME_SPACE + key, JSON.stringify(object));
};
export const storageGet = (key, win = window) => {
    const configStr = win.localStorage.getItem(STORAGE_NAME_SPACE + key);
    return configStr !== null ? JSON.parse(configStr) : null;
};
