/*!
 * Custom elements for Nanopore-Digital Web applications
 */
import { getAssetPath } from "@stencil/core";
let CACHED_MAP;
export const getIconMap = () => {
    if (!CACHED_MAP) {
        const win = window;
        win.Nanoicons = win.Nanoicons || {};
        CACHED_MAP = win.Nanoicons.map = win.Nanoicons.map || new Map();
    }
    return CACHED_MAP;
};
export const addIcons = (icons) => {
    const map = getIconMap();
    Object.keys(icons).forEach((name) => map.set(name, icons[name]));
};
export const getUrl = (i) => {
    let url = getSrc(i.src);
    if (url)
        return url;
    url = getName(i.name, i.icon);
    if (url)
        return getNamedUrl(url);
    if (i.icon) {
        url = getSrc(i.icon);
        if (url)
            return url;
    }
    return null;
};
const getNamedUrl = (iconName) => {
    const url = getIconMap().get(iconName);
    if (url)
        return url;
    if (iconName.startsWith('device/') ||
        iconName.startsWith('pictogram/') ||
        iconName.startsWith('flag/')) {
        return getAssetPath(`../nano-assets/icon/${iconName}.svg`);
    }
    return getAssetPath(`../nano-assets/fontawesome-pro/svgs/${iconName}.svg`);
};
export const getName = (iconName, icon) => {
    if (!iconName && icon && !isSrc(icon)) {
        iconName = icon;
    }
    if (isStr(iconName)) {
        iconName = toLower(iconName);
    }
    if (!isStr(iconName) || iconName.trim() === '') {
        return null;
    }
    // only allow alpha characters and dash
    const invalidChars = iconName.replace(/[a-z]|-|\/|\d/gi, '');
    if (invalidChars !== '') {
        console.warn('invalid characters in icon name ' + iconName);
        return null;
    }
    return iconName;
};
export const getSrc = (src) => {
    if (isStr(src)) {
        src = src.trim();
        if (isSrc(src))
            return src;
    }
    return null;
};
export const isSrc = (str) => str.length > 0 && /(\/|\.)/.test(str);
export const isStr = (val) => typeof val === 'string';
export const toLower = (val) => val.toLowerCase();
