export function PhraseTranslator() {
    var self = this;
    self.splitConditionStrings = function (phrase) {
        var skipround = false;
        var array = [];
        if (_h.isNullOrEmpty(phrase))
            return array;
        for (var i = 0; i < phrase.length; i++) {
            var target = phrase[i];
            if (!skipround) {
                var nextChar = null;
                if (target.indexOf("&") !== -1) {
                    nextChar = phrase[i + 1].indexOf("&") !== -1;
                    if (nextChar) {
                        array.push(i);
                        skipround = true;
                    }

                }
                if (target.indexOf("|") !== -1) {
                    nextChar = phrase[i + 1].indexOf("|") !== -1;
                    if (nextChar) {
                        array.push(i);
                        skipround = true;
                    }
                }
            } else {
                skipround = false;
            }
        }

        if (array.length === 0)
            return [phrase];

        var firstPoint = 0;
        var outArray = [];
        for (var j = 0; j < array.length; j++) {
            var indexTarget = array[j];
            outArray.push(phrase.substring(firstPoint, indexTarget));
           
            if (array.length === 1) 
                outArray.push(phrase.substring(indexTarget + 2));
            

            if (array.length > 1) 
                firstPoint = indexTarget + 2;
            

            if (j + 1 === array.length) 
                outArray.push(phrase.substring(firstPoint));
            
           
        }

        return outArray;
    }
    self.getModel = function (phrase) {
        var array = [];
        var arrayIndex = [];
        var conds = self.splitConditionStrings(phrase);
        conds = _h.cleanArray(conds);
        for (var i = 0; i < conds.length; i++) {
            var cond = self.getCondition(conds[i]);
            if (arrayIndex.indexOf(cond.id) === -1)
                array.push(cond);
            arrayIndex.push(cond.id);
        }
        
        array.sort(function (a, b) { return a.id > b.id });
        return array;
    }
    self.getCondition = function (str) { 
        var strCl = self.clearConditionString(str);
        var number = self.getNumber(str);
        var operator = self.getOperator(str);
        var rightSide = self.getRightSide(strCl, operator.enText);
        var leftSide = self.getLeftSide(strCl, operator.enText);
        return {
            id: number,
            operator: operator,
            condition: str,
            clr: strCl,
            leftSide: leftSide,
            rightSide: rightSide
        };
    }
    self.getOperator = function (str) {
        var op = null;
        for (var i = 0; i < dynamicConditionalOperators2.length; i++) {
            if (str.indexOf(dynamicConditionalOperators2[i].enText) !== -1)
                op = dynamicConditionalOperators2[i];
        }
        return op;
    }
    self.getNumber = function (str) {
        var first = str.search(/cs\d/i);
        var firstD = str.search(/\:/i); 
        var tex = str.substring(first, firstD);
        return Number(tex.replace("cs", ""));
    }
    self.clearConditionString = function (str) {
        str = str.trim();
        var firstBracket = str.indexOf("[");
        if (firstBracket !== undefined && firstBracket !== 0)
            str = str.substring(firstBracket);
        var lastBracket = str.lastIndexOf("]");
        if (lastBracket !== undefined && lastBracket + 1 !== str.length)
            str = str.substring(0, lastBracket);

        str = str.trim() 
            .replace(/cs\d:/i, "");
        return str.substring(1, str.length - 1);
    }
    self.getLeftSide = function (str, operator) {
        var splits = str.split(operator);
        var val = splits[0]
            .substring(str.indexOf("'"), str.lastIndexOf("'"))
            .replace(/[\[]/g, "")
            .replace(/[\]]/g, "")
            .replace(/[\']/g, "");
        return Number(val);
    }
    self.getRightSide = function(str, operator) {
        var split = str.split(operator)[1];
        var type = null;
        var val = "";
        if (split !== "")
            val = split
                .substring(split.indexOf("'") + 1, split.lastIndexOf("'"))
                .replace(/[\']/g, "");
        //val = self.clearBracets(val);

        if (split === "")
            type = 13;
        else if (split.indexOf("listtext") !== -1)
            type = 9;
        else if (split.indexOf("listdate") !== -1)
            type = 10;
        else if (split.indexOf("listenum") !== -1)
            type = 12;
        else if (split.indexOf("listint") !== -1)
            type = 14;
        else if (split.indexOf("int") !== -1)
            type = 0;
        else if (split.indexOf("text") !== -1)
            type = 1;
        else if (split.indexOf("decimal") !== -1)
            type = 2;
        else if (split.indexOf("money") !== -1)
            type = 3;
        else if (split.indexOf("date") !== -1)
            type = 4;
        else if (split.indexOf("enum") !== -1)
            type = 7;
        else if (split.indexOf("sourcetable") !== -1)
            type = 17;
        else if (split.indexOf("calculationtable") !== -1)
            type = 18;
        else if (split.indexOf("table") !== -1)
            type = 8;
        else if (split.indexOf("likepattern") !== -1)
            type = 20;
        else if (split.indexOf("pattern") !== -1)
            type = 15;
        else if (split.indexOf("value") !== -1)
            type = 16;
        else if (split.indexOf("formula") !== -1)
            type = 21;

        return { type: type, value: val };
    }
    self.clearBracets = function (str) {

        if (str.startsWith("["))
            str =  str.substring(1);

        if (str.endsWith("]"))
            str = str.substring(0, str.length -1);

        return str;
    }
}