/*!
 * Custom elements for Nanopore-Digital Web applications
 */
import * as squirrel from "./squirrelly/browser";
squirrel.filters.define('date_long', (str, options = {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
}) => {
    if (!str)
        return;
    const date = new Date(parseInt(str) * 1000);
    str = date.toLocaleDateString('en-GB', options);
    return str;
});
squirrel.filters.define('date_short', (str, format = 'd/m/y') => {
    if (!str)
        return;
    const date = new Date(parseInt(str) * 1000);
    str = format.replace(/(d)/, date.getDate().toString());
    str = str.replace(/(m)/, (date.getMonth() + 1).toString());
    str = str.replace(/(y)/, date.getFullYear().toString());
    return str;
});
squirrel.filters.define('public_name', (str) => {
    if (!str)
        return;
    str = str
        .split('_')
        .map((val) => {
        return val.length < 3 ? val.toUpperCase() : val;
    })
        .join(' ');
    return str.charAt(0).toUpperCase() + str.slice(1);
});
squirrel.filters.define('trim_to', (str, length = 100) => {
    if (!str)
        return;
    if (str.length <= length)
        return str;
    return str.substr(0, length) + '...';
});
squirrel.filters.define('classname', (str) => {
    if (!str)
        return;
    return str.replace(/[\W]+/g, '');
});
squirrel.filters.define('abs_url', (str, index = null, result) => {
    if (!index)
        return str;
    if (!str)
        return;
    let domain = undefined;
    if (index.domains && result && result.origin) {
        const found = index.domains.find((dm) => dm.origin === result.origin);
        domain = found ? found.domain : '';
    }
    else if (index.domain)
        domain = index.domain;
    if (str.match(/^http/) || !domain)
        return str;
    if (!str.startsWith('/'))
        str = '/' + str;
    return location.protocol + '//' + domain + str;
});
squirrel.filters.define('replace', (str, search, replace) => str.replace(search, replace));
squirrel.filters.define('remove_spaces', (str) => str.replace(/ /g, '_'));
squirrel.filters.define('add_spaces', (str) => (str = str.replace(/[_\-]/g, ' ')));
squirrel.filters.define('capitalise', (str) => (str = str.charAt(0).toUpperCase() + str.slice(1)));
squirrel.filters.define('lowercase', (str) => (str = str.toLowerCase()));
squirrel.filters.define('tag_tidy', (str) => {
    const arr = str.split('>');
    return arr.map((s) => s.trim())[arr.length - 1];
});
export const Squirrel = squirrel;
