"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntitiesParser = void 0;
var EntitiesParser = /** @class */ (function () {
    function EntitiesParser(entities) {
        this.entities = entities;
        this.ENTITY_DEFINITION_PATTERN = /\[(.*?)\]\((.*?)\)/gm;
    }
    EntitiesParser.prototype.parse = function (sentence) {
        var definedEntities = [];
        var text = sentence;
        var m;
        var charactersRemoved = 0; //amount of characters removed because of entity definition removal: [shirt](product) -> shirt.
        while ((m = this.ENTITY_DEFINITION_PATTERN.exec(sentence))) {
            var entityDefinition = m[0];
            var entityText = m[1];
            var entityLabel = m[2];
            if (!this.entities.includes(entityLabel)) {
                throw new Error("Undefined entity '" + entityLabel + "'");
            }
            text = text
                .slice(0, m.index - charactersRemoved)
                .concat(entityText, text.slice(m.index + entityDefinition.length - charactersRemoved));
            definedEntities.push({
                label: entityLabel,
                start: m.index - charactersRemoved,
                end: m.index + entityText.length - charactersRemoved,
            });
            charactersRemoved += entityDefinition.length - entityText.length;
        }
        return { text: text, entities: definedEntities };
    };
    return EntitiesParser;
}());
exports.EntitiesParser = EntitiesParser;
//# sourceMappingURL=entities-parser.js.map