var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import { isNonNullable, isNullable } from '../../lib/utils.js';
import { isSafari } from '../../lib/client.js';
import { MAX_ALLOWED_CHARS, MAX_SAFE_DIGITS, MINUS_SIGN, SPACE, THIN_SPACE } from './constants.js';
var CurrencyHelper = /** @class */ (function () {
    function CurrencyHelper() {
    }
    CurrencyHelper.getOptions = function (options) {
        return __assign(__assign({}, _a.defaultOptions), options);
    };
    CurrencyHelper.getInfo = function (value, options) {
        var raw = _a.unformatString(value);
        var formatted = _a.formatString(value, options);
        var cursorMap = _a.getCursorMap(formatted, options);
        return { raw: raw, formatted: formatted, cursorMap: cursorMap };
    };
    CurrencyHelper.getCursorMap = function (formatted, formattingOptions) {
        var options = _a.getOptions(formattingOptions);
        var regexp = new RegExp(options.thousandsDelimiter);
        var cursorMap = [];
        var index = formatted.length;
        var cursor = formatted.length;
        var skip = 0;
        while (index >= 0) {
            cursorMap[index] = cursor;
            var ignoredSymbol = regexp.exec(formatted[index - 1]);
            if (ignoredSymbol) {
                ++skip;
            }
            else {
                cursor = cursor - skip - 1;
                skip = 0;
            }
            --index;
        }
        return cursorMap;
    };
    CurrencyHelper.format = function (value, options) {
        if (isNullable(value)) {
            return '';
        }
        return _a.formatString(this.toDecimalString(value), options);
    };
    CurrencyHelper.parse = function (value) {
        var cleaned = _a.unformatString(value);
        if (!cleaned) {
            return null;
        }
        var destructed = _a.destructString(cleaned);
        if (!destructed) {
            return null;
        }
        var result = destructed.sign + (destructed.integer || '0') + (destructed.delimiter || '.') + (destructed.fraction || '0');
        return parseFloat(result);
    };
    CurrencyHelper.unformatString = function (value) {
        return value
            .replace(/\s/g, '')
            .replace(',', '.')
            .replace(/[\u2212\u002D\uFE63\uFF0D\u2012\u2013\u2014\u2015]/g, '-');
    };
    CurrencyHelper.formatForClipboard = function (value) {
        return _a.unformatString(value).replace('.', ',');
    };
    CurrencyHelper.formatString = function (value, formattingOptions) {
        var unformattedValue = _a.unformatString(value);
        var destructed = _a.destructString(unformattedValue) || {
            sign: '',
            integer: '',
            delimiter: '',
            fraction: '',
        };
        var sign = destructed.sign, integer = destructed.integer, delimiter = destructed.delimiter;
        var fraction = destructed.fraction;
        var fractionDigits = fraction.length;
        var options = _a.getOptions(formattingOptions);
        if (options.hideTrailingZeros) {
            fraction = fraction.replace(/0+$/, '');
            fractionDigits = fraction.length;
        }
        else if (options.fractionDigits !== null) {
            fractionDigits = options.fractionDigits;
        }
        var parts = [];
        var blockSize = 3;
        var start = ((integer.length - 1) % blockSize) - blockSize + 1;
        for (var i = start; i < integer.length; i += blockSize) {
            parts.push(integer.substring(Math.max(i, 0), i + blockSize));
        }
        var result = parts.join(options.thousandsDelimiter);
        if (delimiter || fractionDigits) {
            result += ',';
            result += fraction.padEnd(Math.min(fractionDigits, MAX_SAFE_DIGITS), '0');
        }
        if (sign) {
            result = options.minusSign + result;
        }
        return result;
    };
    CurrencyHelper.isValidString = function (value, options) {
        var unformattedValue = _a.unformatString(value);
        var destructed = _a.destructString(unformattedValue);
        if (!destructed) {
            return false;
        }
        var sign = destructed.sign, integer = destructed.integer, delimiter = destructed.delimiter, fraction = destructed.fraction;
        if (options.unsigned && sign) {
            return false;
        }
        if (options.fractionDigits === 0 && delimiter) {
            return false;
        }
        var integerDigits = integer === '0' ? 0 : integer.length;
        var fractionDigits = fraction.length;
        if (isNonNullable(options.integerDigits) && integerDigits > options.integerDigits) {
            return false;
        }
        if (isNullable(options.integerDigits) && integerDigits > MAX_SAFE_DIGITS - (options.fractionDigits || 0)) {
            return false;
        }
        if (isNonNullable(options.fractionDigits) && fractionDigits > options.fractionDigits) {
            return false;
        }
        return integerDigits + fractionDigits <= MAX_SAFE_DIGITS;
    };
    CurrencyHelper.extractValid = function (value, options) {
        var unformattedValue = _a.unformatString(value);
        var match = /[-.\d]+/.exec(unformattedValue);
        if (!match) {
            return '';
        }
        var token = match[0].substr(0, MAX_ALLOWED_CHARS);
        for (var i = token.length; i >= 0; --i) {
            var result = token.substr(0, i);
            if (_a.isValidString(result, options)) {
                return result;
            }
        }
        return '';
    };
    CurrencyHelper.destructString = function (value) {
        var match = /^(-)?(\d*)?(\.)?(\d*)?(e)?([-+]?\d+)?$/.exec(value);
        if (!match) {
            return null;
        }
        var _b = match[1], sign = _b === void 0 ? '' : _b, _c = match[2], integer = _c === void 0 ? '' : _c, _d = match[3], delimiter = _d === void 0 ? '' : _d, _e = match[4], fraction = _e === void 0 ? '' : _e, _f = match[6], exponent = _f === void 0 ? '' : _f;
        return { sign: sign, integer: integer, delimiter: delimiter, fraction: fraction, exponent: exponent };
    };
    var _a;
    _a = CurrencyHelper;
    CurrencyHelper.defaultOptions = {
        fractionDigits: null,
        hideTrailingZeros: false,
        thousandsDelimiter: isSafari ? SPACE : THIN_SPACE,
        minusSign: MINUS_SIGN,
    };
    CurrencyHelper.toDecimalString = function (number) {
        if (!number.toString().includes('e')) {
            return number.toString();
        }
        var destructed = _a.destructString(number.toExponential());
        if (destructed === null) {
            return '';
        }
        var _b = destructed.sign, sign = _b === void 0 ? '' : _b, integer = destructed.integer, fraction = destructed.fraction, exponent = destructed.exponent;
        var intExponent = parseInt(exponent || '0');
        if (intExponent > 0) {
            return [sign, integer, fraction, '0'.repeat(intExponent - fraction.length)].join('');
        }
        return [sign, '0.', ' 0'.repeat(-intExponent - 1), integer, fraction].join('');
    };
    return CurrencyHelper;
}());
export { CurrencyHelper };
//# sourceMappingURL=CurrencyHelper.js.map