/*
 * 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 Utils = require("./../../unisharp/Utils");
const Hashtable = require("./../../unisharp/Hashtable");

const MetaToken = require("./../MetaToken");
const ProcessorService = require("./../ProcessorService");
const ChemicalFormulaReferent = require("./ChemicalFormulaReferent");
const ReferentToken = require("./../ReferentToken");
const Token = require("./../Token");
const Referent = require("./../Referent");
const ResourceHelper3 = require("./../core/internal/ResourceHelper3");
const MetaChemical = require("./MetaChemical");
const ChemicalToken = require("./internal/ChemicalToken");
const Analyzer = require("./../Analyzer");

/**
 * Анализатор химических формул (специфический анализатор)
 */
class ChemicalAnalyzer extends Analyzer {
    
    get name() {
        return ChemicalAnalyzer.ANALYZER_NAME;
    }
    
    get caption() {
        return "Химические формулы";
    }
    
    get description() {
        return "Химические формулы и их текстовые аналоги";
    }
    
    clone() {
        return new ChemicalAnalyzer();
    }
    
    get isSpecific() {
        return true;
    }
    
    get typeSystem() {
        return [MetaChemical.GLOBAL_META];
    }
    
    get images() {
        let res = new Hashtable();
        res.put(MetaChemical.IMAGE_ID.toString(), ResourceHelper3.getBytes("chemical.png"));
        return res;
    }
    
    createReferent(type) {
        if (type === ChemicalFormulaReferent.OBJ_TYPENAME) 
            return new ChemicalFormulaReferent();
        return null;
    }
    
    get progressWeight() {
        return 1;
    }
    
    process(kit) {
        let ad = kit.getAnalyzerData(this);
        let probs = new Array();
        for (let t = kit.firstToken; t !== null; t = t.next) {
            let li = ChemicalToken.tryParseList(t, 0);
            if (li === null || li.length === 0) 
                continue;
            t = li[li.length - 1].endToken;
            let cf = ChemicalToken.createReferent(li);
            if (cf === null) {
                probs.push(li);
                continue;
            }
            cf = Utils.as(ad.registerReferent(cf), ChemicalFormulaReferent);
            let rt = new ReferentToken(cf, li[0].beginToken, t);
            kit.embedToken(rt);
            t = rt;
        }
        for (const pr of probs) {
            let cf = ChemicalToken.createReferent(pr);
            if (cf === null) 
                continue;
            cf = Utils.as(ad.registerReferent(cf), ChemicalFormulaReferent);
            let rt = new ReferentToken(cf, pr[0].beginToken, pr[pr.length - 1].endToken);
            kit.embedToken(rt);
        }
    }
    
    static initialize() {
        MetaChemical.initialize();
        ChemicalToken.initialize();
        ProcessorService.registerAnalyzer(new ChemicalAnalyzer());
    }
    
    static static_constructor() {
        ChemicalAnalyzer.ANALYZER_NAME = "CHEMICAL";
    }
}


ChemicalAnalyzer.static_constructor();

module.exports = ChemicalAnalyzer