"use strict";
var fs = require('fs');

const TextAnalysisManagerBase = require("./textAnalysisManagerBase");
const TokenRuleConditionServices = require("./tokenRuleConditionServices");

module.exports = class TextAnalysisManagerDev extends TextAnalysisManagerBase{
    constructor(options, language, ruleType, category, ruleId, json)
    {
        super(options,language, ruleType);
        var self = this;
        // dictionaries
        this.ReadDictionaries();
        // macros
        var streamRules = false;
        this.macros = {};

        // conflict Rules
        this.UnstreamConflictRules();

        var folder = this.rootFolder  + "macros/";
        if(fs.existsSync(folder))
        {
            var localFolder = this.localRootFolder + "macros/";
            fs.readdirSync(folder).forEach(file => {
                if (file.length > 5 && file.endsWith(".json")) {
                    var name = file.substring(0,file.length-5);

                    var localRuleFileName = localFolder + name;
                    var macros = require(localRuleFileName);
                    
                    for (var mi = 0; mi < macros.length; mi++)
                    {
                        var macro = macros[mi];
                        if (!macro.name)
                        {
                            debugger;
                            continue;
                        }
                        var condition = TokenRuleConditionServices.Instanciate(this, macro);
                        if (!condition)
                        {
                            debugger;
                            continue;
                        }
                        this.macros[macro.name] = condition;
                    }
                }
            });
        }

        // functions
        this.ReadFunctions();
        // annotator rules
        var folderName = "annotatorRules";
        this.LoadPostTaggerRules(folderName, this.annotatorRules);
        // annotator rules
        this.LoadPhraseRules();
        //
        if (!json)
        {
            this.LoadAllRules(ruleType, category, ruleId);
        }
        else{
            this.LoadRulesFromJson(json);
        }
    }
}
