"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntitiesParser = void 0;
var EntitiesParser = /** @class */ (function () {
    function EntitiesParser() {
    }
    EntitiesParser.parse = function (samples, entities) {
        return samples.map(function (sample) { return EntitiesParser.parseSample(sample, entities); });
    };
    EntitiesParser.parseSample = function (sample, entities) {
        var text = sample.text;
        var accumulation = 0;
        var m;
        while ((m = EntitiesParser.ENTITY_DEFINITION_PATTERN.exec(text))) {
            if (entities.includes(m[2])) {
                sample.text = sample.text
                    .slice(0, m.index - accumulation)
                    .concat(m[1], sample.text.slice(m.index + m[0].length - accumulation));
                sample.entities.push({
                    text: m[1],
                    label: m[2],
                    start: m.index - accumulation,
                    end: m.index + m[1].length - accumulation,
                });
                accumulation += m[0].length - m[1].length;
            }
            else {
                throw new Error("Undefined entity: " + m[2]);
            }
        }
        return sample;
    };
    // eslint-disable-next-line @typescript-eslint/naming-convention
    EntitiesParser.ENTITY_DEFINITION_PATTERN = /\[(.*?)\]\((.*?)\)/gm;
    return EntitiesParser;
}());
exports.EntitiesParser = EntitiesParser;
//# sourceMappingURL=entities-parser.js.map