/*!
 * Custom elements for Nanopore-Digital Web applications
 */
import SqrlErr from "./err";
export function errWithBlocksOrFilters(name, blocks, // false means don't check
filters, native) {
    if (blocks && blocks.length > 0) {
        throw SqrlErr((native ? 'Native' : '') + "Helper '" + name + "' doesn't accept blocks");
    }
    if (filters && filters.length > 0) {
        throw SqrlErr((native ? 'Native' : '') + "Helper '" + name + "' doesn't accept filters");
    }
}
/* ASYNC LOOP FNs */
export function asyncArrLoop(arr, index, fn, res, cb) {
    fn(arr[index], index).then(function (val) {
        res += val;
        if (index === arr.length - 1) {
            cb(res);
        }
        else {
            asyncArrLoop(arr, index + 1, fn, res, cb);
        }
    });
}
export function asyncObjLoop(obj, keys, index, fn, res, cb) {
    fn(keys[index], obj[keys[index]]).then(function (val) {
        res += val;
        if (index === keys.length - 1) {
            cb(res);
        }
        else {
            asyncObjLoop(obj, keys, index + 1, fn, res, cb);
        }
    });
}
const escMap = {
    '&': '&amp;',
    '<': '&lt;',
    '>': '&gt;',
    '"': '&quot;',
    "'": '&#39;',
};
export function replaceChar(s) {
    return escMap[s];
}
export function XMLEscape(str) {
    // To deal with XSS. Based on Escape implementations of Mustache.JS and Marko, then customized.
    const newStr = String(str);
    if (/[&<>"']/.test(newStr)) {
        return newStr.replace(/[&<>"']/g, replaceChar);
    }
    else {
        return newStr;
    }
}
