"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalize = exports.parseFirst = exports.parseAll = void 0;
const currencies_js_1 = require("./currencies.js");
const allSymbols = currencies_js_1.currencies
    .reduce((syms, curr) => syms.concat(curr.code).concat(curr.symbols), [])
    .map(sym => sym.replace(/([$.])/g, '\\$1'))
    .join('|');
const minuses = '[−–‑—‒-]';
const delimiters = `[\\s.,\\'‘’‛′´\`]`;
const numGroup = `(\\s*\\d+(?:${delimiters}*\\d+)*)`;
const symGroup = `(${allSymbols})`;
const leadingSym = [minuses + '?', symGroup, minuses + '?', numGroup].join('\\s*');
const trailingSym = [minuses + '?', numGroup, symGroup].join('\\s*');
const startDelimiter = '(?:^|\\s+)';
const endDelimiter = `(?=$|\\s+|[,;:\\!\\?\\.\\'"\\+\\-])`;
const mainGroups = `(?:\\s*${[leadingSym, trailingSym].join('|')})`;
const priceRegex = new RegExp(`${startDelimiter}${mainGroups}${endDelimiter}`, 'ig');
function parseAll(text, options = {}) {
    text = normalize(text);
    if (!text) {
        return [];
    }
    priceRegex.lastIndex = 0;
    const results = [];
    performNextMatch();
    return results;
    function performNextMatch() {
        const m = priceRegex.exec(text);
        if (m) {
            const result = matchResultToPrice(m, options);
            if (result) {
                results.push(result);
            }
            performNextMatch();
        }
    }
}
exports.parseAll = parseAll;
function parseFirst(text, options = {}) {
    text = normalize(text);
    if (!text) {
        return null;
    }
    priceRegex.lastIndex = 0;
    const m = priceRegex.exec(text);
    return m ? matchResultToPrice(m, options) : null;
}
exports.parseFirst = parseFirst;
function matchResultToPrice(match, options = {}) {
    const { parseNegative = false } = options;
    const isNegative = new RegExp(minuses).test(match[0]);
    const symbol = (match[1] || match[4] || '').toLowerCase();
    const currency = currencies_js_1.currencies.find(cur => cur.code === symbol || cur.symbols.indexOf(symbol) > -1);
    if (!currency) {
        return null;
    }
    const exponent = currency.exponent;
    const sign = parseNegative && isNegative ? -1 : 1;
    const floatValue = sign * parseNumber(match[2] || match[3], exponent);
    const value = Math.round(floatValue * Math.pow(10, exponent));
    return {
        value,
        currencyCode: currency.code,
        floatValue,
        symbol,
        currency,
    };
}
function parseNumber(str, exponent) {
    const chunks = str.split(new RegExp(delimiters + '+'));
    const num = parseInt(chunks.join(''), 10);
    const lastChunk = chunks.slice(-1)[0];
    const hasDecimalPoint = chunks.length > 1 && lastChunk.length <= exponent;
    const actualExponent = lastChunk.length;
    return hasDecimalPoint ? num / Math.pow(10, actualExponent) : num;
}
function normalize(str) {
    str = String(str || '');
    str = str.replace(/[\u200e-\u200f]/g, '');
    return str;
}
exports.normalize = normalize;
//# sourceMappingURL=price.js.map