var _TextGlyphMapper = /** @class */ (function () {
    function _TextGlyphMapper() {
        this._isHex = false;
    }
    Object.defineProperty(_TextGlyphMapper.prototype, "text", {
        get: function () {
            return this._text;
        },
        set: function (value) {
            this._text = value;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(_TextGlyphMapper.prototype, "glyph", {
        get: function () {
            return this._glyph;
        },
        set: function (value) {
            this._glyph = value;
        },
        enumerable: true,
        configurable: true
    });
    _TextGlyphMapper.prototype._applyEscapeSequence = function (text) {
        var escapedText = text;
        if (text.indexOf('(') !== -1) {
            escapedText = escapedText.replace(/\(/g, '\\(');
        }
        if (text.indexOf(')') !== -1) {
            escapedText = escapedText.replace(/\)/g, '\\)');
        }
        if (text.indexOf("'") !== -1) { // eslint-disable-line
            escapedText = escapedText.replace(/'/g, "\\'"); // eslint-disable-line
        }
        if (text.indexOf('\\') !== -1) {
            escapedText = escapedText.replace(/\\/g, '\\\\');
        }
        if (text.indexOf('\b') !== -1) {
            escapedText = escapedText.replace(/\b/g, '\\b');
        }
        if (text.indexOf('\f') !== -1) {
            escapedText = escapedText.replace(/\f/g, '\\f');
        }
        if (text.indexOf('\n') !== -1) {
            escapedText = escapedText.replace(/\n/g, '\\n');
        }
        if (text.indexOf('\r') !== -1) {
            escapedText = escapedText.replace(/\r/g, '\\r');
        }
        return escapedText;
    };
    _TextGlyphMapper.prototype._getText = function () {
        var _this = this;
        if (typeof (this.glyph) === 'undefined') {
            return this.text;
        }
        else {
            var finalText_1 = '';
            var currentText_1 = '';
            var subString = this.text.length >= 2;
            var start = this.text.startsWith('(');
            var end = this.text.endsWith(')');
            if (subString && start && !end) {
                currentText_1 = this.text.substring(1, this.text.length);
            }
            else if (subString && !start && end) {
                currentText_1 = this.text.substring(0, this.text.length - 1);
            }
            else if (subString) {
                currentText_1 = this.text.substring(1, this.text.length - 1);
            }
            var replaceText = '';
            var otherText = '';
            var glyphs = [];
            var glyphDictionary_1 = new Map();
            var dictionary = new Map();
            var rTcount = 0;
            var roTcount = 0;
            var isHex_1 = false;
            for (var i = 0; i < this.glyph.length; i++) {
                if (this.glyph[Number.parseInt(i.toString(), 10)]._isReplace) {
                    if (otherText !== '') {
                        dictionary.set('O-' + roTcount++, otherText);
                        otherText = '';
                        isHex_1 = this.glyph[Number.parseInt(i.toString(), 10)]._isHex;
                    }
                    glyphs.push(this.glyph[Number.parseInt(i.toString(), 10)]);
                    replaceText += this.glyph[Number.parseInt(i.toString(), 10)].text;
                }
                else {
                    if (replaceText !== '') {
                        var key = 'R-' + rTcount++;
                        glyphDictionary_1.set(key, glyphs);
                        dictionary.set(key, replaceText);
                        glyphs = [];
                        replaceText = '';
                    }
                    isHex_1 = this.glyph[Number.parseInt(i.toString(), 10)]._isHex;
                    var text = this.glyph[Number.parseInt(i.toString(), 10)].text;
                    if (!isHex_1) {
                        otherText += this._applyEscapeSequence(text);
                    }
                    else {
                        otherText += text;
                    }
                }
            }
            if (replaceText !== '') {
                var key = 'R-' + rTcount++;
                glyphDictionary_1.set(key, glyphs);
                dictionary.set(key, replaceText);
                glyphs = [];
                replaceText = '';
            }
            if (otherText !== '') {
                var key = 'O-' + roTcount++;
                dictionary.set(key, otherText);
                otherText = '';
            }
            dictionary.forEach(function (value, key) {
                if (key.indexOf('O-') !== -1) {
                    if (isHex_1) {
                        finalText_1 += '<' + value + '>';
                    }
                    else {
                        finalText_1 += '(' + value + ')';
                    }
                }
                else {
                    finalText_1 += ' -' + _this._getReplacedCharacter(glyphDictionary_1.get(key)) + ' ';
                    currentText_1 = _this._replacedText(currentText_1, glyphDictionary_1.get(key));
                }
            });
            return finalText_1;
        }
    };
    _TextGlyphMapper.prototype._replacedText = function (text, glyphs) {
        if (!text) {
            return text;
        }
        var currentText = text;
        var count = 0;
        for (var _i = 0, glyphs_1 = glyphs; _i < glyphs_1.length; _i++) {
            var gly = glyphs_1[_i];
            var unicode = gly.text;
            if (unicode) {
                count += unicode.length;
            }
        }
        currentText = currentText.substring(count);
        return currentText;
    };
    _TextGlyphMapper.prototype._getReplacedCharacter = function (glyphs) {
        var totalWidth = 0;
        for (var i = 0; i < glyphs.length; i++) {
            var width = glyphs[Number.parseInt(i.toString(), 10)]._width;
            if (width > 0) {
                if (glyphs[Number.parseInt(i.toString(), 10)].text !== ' ') {
                    totalWidth += width;
                }
                else {
                    var actualFontSize = (0.001 * width * glyphs[Number.parseInt(i.toString(), 10)].fontSize);
                    totalWidth += (width + ((width / actualFontSize) * glyphs[Number.parseInt(i.toString(), 10)]._charSpacing) +
                        ((width / actualFontSize) * glyphs[Number.parseInt(i.toString(), 10)]._wordSpacing));
                }
            }
        }
        return totalWidth;
    };
    return _TextGlyphMapper;
}());
export { _TextGlyphMapper };
