"use strict";(function(){var e=0;var r=0;"use strict";var t=require("fs");var n=require("path");var i=require("./jsUtil");var o="@a",u="@l",a=/@a/g,s=/@l/g,l=/^\s*[\w@]*\s?/g,f="db/priorityRule/",c="metadata.json";function d(){if(false===this instanceof d){return new d}var e=this;e.cmdForests=null;e.ruleCache=null;e.confPath=null;e._init()}d.prototype._init=function(){var e=this;e.cmdForests={};e.ruleCache={};var r;var t=i.getRootPath(),o=n.resolve(t,"db/priorityRule"),u=n.join(o,c);e.confPath=o;e.confFilePath=u};d.prototype.getCmdsByName=function(e){var r=this;return r.cmdForests[e]};d.prototype.putCmdsByName=function(e,r){var t=this;t.cmdForests[e]=r};d.prototype.parseCmds=function(e){var r=this;if(e[e.length-1]!="\n"){return i.parseConfig(e+"\r\n",true)}return i.parseConfig(e,true)};d.prototype.cmdsToStr=function(e){var r=this;var t="",n=true;i.foreachCmds(e,function(e){if(e.cmd[0]!=" "&&!n){t+="!\r\n"}n=false;t+=e.cmd+"\r\n"});return t};d.prototype.createPriorityRuleIns=function(e){var t=this;var n=new h;if(!n.init(e)){if(r){console.warn("ruleIns init failed")}return null}return n};d.prototype.sortCmdsByPriorityRuleName=function(e,t,o,u){var a=this;var s;var l=i.getRulePath(a.confFilePath,t,o,u);if(l==null){return false}l=n.join(a.confPath,l);if(a.ruleCache[t]==undefined||a.ruleCache[t][o]==undefined||a.ruleCache[t][o][u]==undefined){s=a.createPriorityRuleIns(l);if(s==null){return false}a.ruleCache[t]=a.ruleCache[t]==undefined?{}:a.ruleCache[t];a.ruleCache[t][o]=a.ruleCache[t][o]==undefined?{}:a.ruleCache[t][o];a.ruleCache[t][o][u]}else{s=a.ruleCahce[t][o][u]}s.addPriorityToCmds(e);if(r){console.info(e)}i.sortCmds(e,function(e,r){var t=e.priority==undefined?0:e.priority;var n=r.priority==undefined?0:r.priority;if(t==n){var i=e.line==undefined?0:e.line;var o=r.line==undefined?0:r.line;return i-o}return t-n});return true};d.prototype.mergeCmds=function(e){var r=this;var t=[];i.mergeCmdsToCmds(t,e);return t};function h(){var e=this;e.ruleHash={}}h.prototype.init=function(e){var n=this;var o;try{o=t.readFileSync(e,"utf-8")}catch(u){if(r){console.warn(u)}return false}var a;if(o[o.length-1]!="\n"){a=i.parseConfig(o+"\r\n",true)}else{a=i.parseConfig(o,true)}n.ruleHash={};v(a,n.ruleHash);if(r){console.info("rule hash:");console.info(n.ruleHash)}return true};h.prototype.addPriorityToCmds=function(e){var t=this;m(e,t.ruleHash);if(r){console.info("cmds with priority:");console.info(e)}};module.exports=d;function m(e,t){var n,i,u,a,s,l,f;for(i=0;i<e.length;++i){f=e[i].newCmd==undefined?e[i].cmd:e[i].newCmd;n=p(f);a=t[n];if(a==undefined){a=t[o]}l=false;if(a!=undefined){for(u=0;u<a.length;++u){s=a[u].ruleReg;if(f.match(s)!=null){if(r){console.info("%s match %s success",f,s)}l=true;e[i].priority=a[u].priority;m(e[i].subcmds,a[u].ruleHash);break}}}if(!l){e[i].priority=999999999}}}function p(e){var r=e.match(l);if(r==null){return o}r=r[0].trim();return r}function v(e,r){var t,n,i,o,u,l,f;for(t=0;t<e.length;++t){u=e[t];i=p(u.cmd);if(r[i]==undefined){r[i]=[]}f=r[i];o=u.cmd.replace(a,"\\S*?");o=o.replace(s,".*");l=false;for(n=0;n<r[i].length;++n){if(f[n].ruleReg==o){v(u.subcmds,f[n].ruleHash);l=true;break}}if(!l){f.push({ruleReg:o,priority:u.line,ruleHash:{}});v(u.subcmds,f[f.length-1].ruleHash)}}}(function(){if(e){var r=new d;var n=t.readFileSync("d:\\wechat.txt","utf-8");var i=r.parseCmds(n);i=r.mergeCmds(i);r.sortCmdsByPriorityRuleName(i,"EG350","10.X","wechat");r.sortCmdsByPriorityRuleName(i,"EG350","10.X","wechat");var o=r.cmdsToStr(i);console.log(o)}})()})();