/*
 * SDK Pullenti Lingvo, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software.
 * This class is generated using the converter Unisharping (www.unisharping.ru) from Pullenti C#.NET project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

const StringBuilder = require("./../unisharp/StringBuilder");

/**
 * Результат анализа
 * 
 */
class AnalysisResult {
    
    constructor() {
        this.m_Sofa = null;
        this.m_Entities = new Array();
        this.firstToken = null;
        this.ontology = null;
        this.baseLanguage = null;
        this.tokensCount = 0;
        this.m_Log = new Array();
        this.exceptions = new Array();
        this.isTimeoutBreaked = false;
        this.tag = null;
    }
    
    get sofa() {
        return this.m_Sofa;
    }
    set sofa(value) {
        this.m_Sofa = value;
        return value;
    }
    
    get entities() {
        return this.m_Entities;
    }
    
    get log() {
        return this.m_Log;
    }
    
    addException(ex) {
        let str = ex.toString();
        for (const e of this.exceptions) {
            if (e.toString() === str) 
                return;
        }
        this.exceptions.push(ex);
        this.m_Log.push("ERROR: " + ex.toString());
    }
    
    toString() {
        let res = new StringBuilder();
        res.append("Общая длина ").append(this.sofa.text.length).append(" знаков (").append(this.tokensCount).append(" токенов)");
        if (this.baseLanguage !== null) 
            res.append(", базовый язык ").append(this.baseLanguage.toString());
        res.append(", найдено ").append(this.entities.length).append(" сущностей");
        if (this.isTimeoutBreaked) 
            res.append(", прервано по таймауту");
        return res.toString();
    }
    
    serialize(stream) {
        
    }
    
    /**
     * Найти в цепочке токенов токен по его позиции
     * @param pos между BeginChar и EndChar
     * @param tFrom с какого токена начинать поиск (null - FirstToken)
     * @return токен или null
     */
    findTokenByPos(pos, tFrom = null) {
        let t = (tFrom != null ? tFrom : this.firstToken);
        for (; t !== null; t = t.next) {
            if (t.beginChar <= pos && pos <= t.endChar) 
                return t;
            else if (t.beginChar > pos) 
                break;
        }
        if (tFrom === null) 
            return null;
        for (t = this.firstToken; t !== tFrom && t !== null; t = t.next) {
            if (t.beginChar <= pos && pos <= t.endChar) 
                return t;
            else if (t.beginChar > pos) 
                break;
        }
        return null;
    }
}


module.exports = AnalysisResult