/*
 @author Zakai Hamilton
 @component KabTerms
 */

package.require("kab.terms", "browser");

package.kab.terms = function KabTerms(me) {
    me.terms = __json__;
    me.init = function() {
        me.ui.theme.useStylesheet("kab.terms");
    };
    me.parse = function (wordsString, options) {
        return me.core.string.parseWords(wordsString, function(words) {
            for (var wordIndex = 0; wordIndex < words.length; wordIndex++) {
                for (var term in me.terms.terms) {
                    if(term.startsWith("!")) {
                        continue;
                    }
                    var item = me.terms.terms[term];
                    var termWords = term.split(" ");
                    var numTermWords = termWords.length;
                    var collectedWords = me.toCase(item, words[wordIndex]);
                    for (var termWordIndex = 1; termWordIndex < numTermWords; termWordIndex++) {
                        if (wordIndex + termWordIndex >= words.length) {
                            break;
                        }
                        var word = words[wordIndex + termWordIndex];
                        var matchingTerm = me.terms.terms[word];
                        if(matchingTerm && matchingTerm.ignore) {
                            numTermWords++;
                            continue;
                        }
                        collectedWords += " " + word.toLowerCase();
                    }
                    if (me.toCase(item, term) === collectedWords) {
                        var expansion = item.expansion;
                        if (expansion) {
                            expansion = [].concat(expansion);
                            expansion = expansion.map(function(item) {
                                return me.parse(item, options);
                            });
                            if (expansion.length > 1) {
                                expansion = expansion.slice(0, -1).join(", ") + " and " + expansion.slice(-1).toString();
                            } else {
                                expansion = expansion.slice(-1).toString();
                            }
                            words.splice(wordIndex, numTermWords);
                            me.modify(words, wordIndex, item, term, " (", expansion, ")", options, true);
                        }
                        var translation = item.translation;
                        if (translation) {
                            translation = me.parse(translation, me.duplicateOptions(options, {"addStyles":false}));
                            words.splice(wordIndex, numTermWords);
                            me.modify(words, wordIndex, item, term, " [", translation, "]", options);
                        }
                        if(options.addStyles && !translation && !expansion) {
                            words.splice(wordIndex, numTermWords);
                            me.modify(words, wordIndex, item, term, "", term, "", me.duplicateOptions(options, {"keepSource":false}));
                        }
                        break;
                    }
                }
            }
        });
    };
    me.toCase = function(item, string) {
        if(item.case !== "sensitive") {
            string = string.toLowerCase();
        }
        return string;
    }
    me.duplicateOptions = function(options, overrides) {
        var duplicate = {};
        for(var key in options) {
            duplicate[key] = options[key];
        }
        if(overrides) {
            for(var key in overrides) {
                duplicate[key] = overrides[key];
            }
        }
        return duplicate;
    };
    me.modify = function (words, wordIndex, item, term, prefix, replacement, suffix, options, expansion) {
        if (!options.doTranslation) {
            replacement = term;
        }
        else if (options.keepSource) {
            replacement = term + prefix + replacement + suffix;
        }
        if (options.addStyles && (item.style || replacement !== term)) {
            replacement = me.applyStyles(term, item.style, replacement, options, expansion);
        }
        words.splice(wordIndex, 0, replacement);
    };
    me.applyStyles = function(term, styles, text, options, expansion) {
        var html = "";
        for(var style in styles) {
            if(style === "heading") {
                html += "<span class=\"kab-term-title\">" + styles[style] + "</span>";
            }
        }
        if(styles && styles.phase) {
            html += "<span class=\"kab-term-phase-" + styles.phase;
            html += "\" ";
            if(!options.keepSource && !expansion && options.doTranslation) {
                html += " kab-term-tooltip=\"" + term + "\"";
            }
            html += ">" + text + "</span>";
        }
        else if(!options.keepSource && !expansion) {
            html += "<span class=\"kab-term-phase-none";
            html += "\" ";
            html += " kab-term-tooltip=\"" + term + "\"";
            html += ">" + text + "</span>";
        }
        else {
            html += text;
        }
        return html;
    };
};
@@ -179,16 +179,17 @@
 function
+ 
 () %7B%0A   
@@ -351,16 +351,17 @@
 function
+ 
 (words) 
@@ -378,123 +378,357 @@
     
-for (var wordIndex = 0; wordIndex %3C words.length; wordIndex++) %7B%0A                for (var term in me.terms.terms) %7B
+var terms = Object.keys(me.terms.terms).sort(function (source, target) %7B%0A                return target.length - source.length;%0A            %7D);%0A            for (var wordIndex = 0; wordIndex %3C words.length; wordIndex++) %7B%0A                for (var termIndex = 0; termIndex %3C terms.length; termIndex++) %7B%0A                    var term = terms%5BtermIndex%5D;
 %0A   
@@ -746,16 +746,17 @@
       if
+ 
 (term.st
@@ -1458,16 +1458,17 @@
       if
+ 
 (matchin
@@ -1982,24 +1982,25 @@
 map(function
+ 
 (item) %7B%0A   
@@ -2821,16 +2821,17 @@
 Styles%22:
+ 
 false%7D))
@@ -3053,16 +3053,17 @@
       if
+ 
 (options
@@ -3298,16 +3298,17 @@
 Source%22:
+ 
 false%7D))
@@ -3463,16 +3463,17 @@
 function
+ 
 (item, s
@@ -3491,16 +3491,17 @@
       if
+ 
 (item.ca
@@ -3638,16 +3638,17 @@
 function
+ 
 (options
@@ -3693,32 +3693,33 @@
  %7B%7D;%0A        for
+ 
 (var key in opti
@@ -3788,16 +3788,17 @@
       if
+ 
 (overrid
@@ -3814,24 +3814,25 @@
          for
+ 
 (var key in 
@@ -4132,32 +4132,24 @@
 m;%0A        %7D
-%0A       
  else if (op
@@ -4503,16 +4503,17 @@
 function
+ 
 (term, s
@@ -4581,16 +4581,17 @@
      for
+ 
 (var sty
@@ -4616,24 +4616,25 @@
           if
+ 
 (style === %22
@@ -4766,16 +4766,17 @@
       if
+ 
 (styles 
@@ -4903,16 +4903,17 @@
       if
+ 
 (!option
@@ -5100,24 +5100,16 @@
    %7D
-%0A       
  else if
 (!op
@@ -5104,16 +5104,17 @@
  else if
+ 
 (!option
@@ -5338,24 +5338,16 @@
        %7D
-%0A       
  else %7B%0A

sorting terms by longest terms first