var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));var _classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));var _createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass"));var _react=_interopRequireDefault(require("react"));var _graph=require("../helpers/graph");var _tree=require("../helpers/tree");var _ViewSystem=_interopRequireDefault(require("./ViewSystem"));var _css=_interopRequireDefault(require("css"));function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly)symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable;});keys.push.apply(keys,symbols);}return keys;}function _objectSpread(target){for(var i=1;i */gm;var MATCH_SPACES=/ +/gm;var MATCH_PLUS=/ *\+ */gm;var MATCH_MINUS=/ *\- */gm;var RulingSystem=function(){function RulingSystem(){(0,_classCallCheck2.default)(this,RulingSystem);this.rules=[];this.index={};}(0,_createClass2.default)(RulingSystem,[{key:"getDeclarations",value:function getDeclarations(rule){var res=rule.declarations.reduce(function(acc,curr){return _objectSpread({},acc,(0,_defineProperty2.default)({},curr.property,curr.value));},{});return res;}},{key:"getCSS",value:function getCSS(Node,View){var _this=this;var rules=this.match(Node,View);var css=rules.map(function(r){return _this.getDeclarations(r);});return css.reduce(function(acc,curr){return _objectSpread({},acc,{},curr);},{});}},{key:"match",value:function match(Node,View){var rules=[];if(!Node.content)return[];var array=[].concat((0,_toConsumableArray2.default)(this.index['*']),(0,_toConsumableArray2.default)(this.index[Node.content.value]||[]));for(var i in array){var rule=array[i];if(this.matchRule(rule,Node,View)){rules.push(rule);}}return rules;}},{key:"matchRule",value:function matchRule(rule,Node,View){var selectors=rule.selectors,declarations=rule.declarations;for(var i in selectors){var selector=selectors[i];if(this.matchSelector(selector,Node,View)){return true;}}return false;}},{key:"matchSelector",value:function matchSelector(selector,Node,View){if(Node.depth+1=0;i--){var ViewNode=Node.content;var _selector$i=selector[i],nodes=_selector$i.nodes,prenodes=_selector$i.prenodes,nextnodes=_selector$i.nextnodes;if(View.isRoot(Node)&&nodes.includes('ROOT'))return true;if(!Node.content)return false;if(!nodes.includes(ViewNode.value)&&!nodes.includes('*')){return false;}if(prenodes.length>0){var preNode=View.getBefore(Node);var preNodeValue=preNode&&preNode.content&&preNode.content.value;if(preNodeValue&&!prenodes.includes(preNodeValue)){return false;}}if(nextnodes.length>0){var afterNode=View.getAfter(Node);var afterNodeValue=afterNode&&afterNode.content&&afterNode.content.value;if(afterNodeValue&&!nextnodes.includes(afterNodeValue)){return false;}}Node=View.getParent(Node);}return true;}},{key:"parseSelector",value:function parseSelector(selector){var string=selector;var tree=string.split(MATCH_CHILD);for(var i in tree){var minus=tree[i].split(MATCH_MINUS);if(minus.length===1){minus[1]=minus[0];minus[0]="";}var plus=minus[1].split(MATCH_PLUS);if(plus.length===1){plus[1]="";}var nodes=plus[0].split(MATCH_SPACES).filter(Boolean);var prenodes=minus[0].split(MATCH_SPACES).filter(Boolean);var nextnodes=plus[1].split(MATCH_SPACES).filter(Boolean);tree[i]={nodes:nodes,prenodes:prenodes,nextnodes:nextnodes,initial:tree[i]};}return tree;}},{key:"addRule",value:function addRule(rule){var _this2=this;var selectors=rule.selectors,declarations=rule.declarations;var parsed=selectors.map(function(s){return _this2.parseSelector(s);});var res={selectors:parsed,declarations:declarations};this.rules.push(res);parsed.forEach(function(selectorGroup){selectorGroup.forEach(function(selector){selector.nodes.forEach(function(node){if(!_this2.index[node])_this2.index[node]=[];_this2.index[node].push(res);});});});}},{key:"addRuleFile",value:function addRuleFile(file){var ruleCSS=_css.default.parse(file);for(var i in ruleCSS.stylesheet.rules){this.addRule(ruleCSS.stylesheet.rules[i]);}}},{key:"export",value:function _export(){var graph=this.topology.export();return{topology:graph,home:this.homeNode};}},{key:"import",value:function _import(data){var topology=data.topology,home=data.home;try{this.topology.import(topology,RouteNode);this.setHome(home);}catch(e){console.log(e);throw new Error("RS Import Failed: ");}return this;}}]);return RulingSystem;}();exports.default=RulingSystem;