(function(u,c){typeof exports=="object"&&typeof module<"u"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(u=typeof globalThis<"u"?globalThis:u||self,c(u.RuleForge={}))})(this,function(u){"use strict";var v=Object.defineProperty;var T=(u,c,f)=>c in u?v(u,c,{enumerable:!0,configurable:!0,writable:!0,value:f}):u[c]=f;var n=(u,c,f)=>(T(u,typeof c!="symbol"?c+"":c,f),f);var c=(i=>(i.PASS="PASS",i.ERROR="ERROR",i.FAIL="FAIL",i))(c||{}),f=(i=>(i.EQUAL="EQUAL",i.NOTEQUAL="NOTEQUAL",i.CONTAINS="CONTAINS",i.NOTCONTAINS="NOTCONTAINS",i.GREATERTHAN="GREATERTHAN",i.LESSTHAN="LESSTHAN",i.GREATEROREQUAL="GREATEROREQUAL",i.LESSOREQUAL="LESSOREQUAL",i))(f||{});function N(i,e,t){switch(e){case"EQUAL":return typeof i=="number"?i==parseInt(t):i.toString()==t;case"NOTEQUAL":return typeof i=="number"?i==parseInt(t):i.toString()!=t;case"CONTAINS":return typeof i!="object"||i.forEach(s=>{if(typeof s=="number"&&s===parseInt(t)||s.toString()==t)return!0}),!1;case"NOTCONTAINS":return typeof i!="object"||i.forEach(s=>{if(typeof s=="number"&&s===parseInt(t)||s.toString()==t)return!1}),!0;case"GREATERTHAN":return typeof i!="number"?!1:i>parseInt(t);case"LESSTHAN":return typeof i!="number"?!1:i=parseInt(t);case"LESSOREQUAL":return typeof i!="number"?!1:i<=parseInt(t);default:return!1}}class R{constructor(e,t){n(this,"_factName");n(this,"_factData");this._factName=e,this._factData=t}get factName(){return this._factName}get factData(){return this._factData}hasSubject(e){return this._factData.hasOwnProperty(e)}hasAttribute(e,t){return this._factData.hasOwnProperty(e)?this._factData[e].hasOwnProperty(t):!1}hasValue(e,t,s,r){if(!this._factData.hasOwnProperty(e)||!this._factData[e].hasOwnProperty(t))return!1;const o=this._factData[e][t];return N(o,s,r)}}class m{constructor(e,t,s="",r=""){n(this,"_relationshipName");n(this,"_relationshipDescription");n(this,"_relation");n(this,"_subject");this._relationshipName=e,this._relationshipDescription=t,this._relation=s,this._subject=r}get relationshipName(){return this._relationshipName}get relationshipDescription(){return this._relationshipDescription}get relation(){return this._relation}get subject(){return this._subject}setSubject(e){this._subject=e}setRelation(e){this._relation=e}}class g{constructor(){n(this,"_relationshipMap");n(this,"_memoryElementMap");n(this,"_relatedList",[]);this._relationshipMap=new Map,this._memoryElementMap=new Map}get relationships(){return this._relationshipMap}get memoryElements(){return this._memoryElementMap}get related(){return this._relatedList}hasRelationship(e){return this._relationshipMap.has(e)}getRelationships(e){const t=this._relationshipMap.get(e);return t===void 0?[]:t}addRelationship(e){this._relationshipMap.get(e.relation)===void 0&&this._relationshipMap.set(e.relation,[e])}removeRelationship(e){this._relationshipMap.delete(e.relation)}removeRelation(e){this._relationshipMap.delete(e)}hasMemoryElement(e){return this._memoryElementMap.has(e)}getMemoryElement(e){const t=this._memoryElementMap.get(e);return t===void 0?[]:t}getMemoryElementsByFact(e){const t=[];for(const[s,r]of Object.entries(e.factData)){const o=this._memoryElementMap.get(s);o!==void 0&&t.push(...o)}return t}addMemoryElement(e,t){const s=this._memoryElementMap.get(e);s===void 0?this._memoryElementMap.set(e,[t]):s.push(t)}removeMemoryElement(e){this._memoryElementMap.delete(e)}clearRelated(){this._relatedList=[]}addToRelated(e){this._relatedList.push(e)}}class p{constructor(e){n(this,"act",e=>{});this.act=e}}class E{constructor(e,t,s){n(this,"knowledgeBase");n(this,"actFunction",e=>{});n(this,"fact");this.knowledgeBase=e,this.actFunction=t,this.fact=s}act(e){this.knowledgeBase.addMemoryElement(Object.keys(this.fact.factData)[0],this.fact),this.actFunction()}}class A{constructor(e,t,s){n(this,"knowledgeBase");n(this,"actFunction",e=>{});n(this,"relationship");this.knowledgeBase=e,this.actFunction=t,this.relationship=s}act(e){if(e===void 0)throw new Error("No object with relationship data passed to action");if(typeof e!="object")throw new Error("No object with relationship data passed to action");const t=e.factData;if(!t.hasOwnProperty("subject"))throw new Error("Object passed to relationship action is missing a subject");for(let s=0,r=this.knowledgeBase.related.length;sh.hasValue(t,s,r,o)?l:d}get name(){return this._name}}class _{constructor(e,t,s,r){n(this,"_name");n(this,"_action");n(this,"_conditionMap");n(this,"_knowledgeBase");if(this._name=e,this._action=t,this._conditionMap=new Map,r!==void 0)for(let o=0,l=r.length;o{}));n(this,"_factMap",new Map);n(this,"lastRuleset","");n(this,"lastRule","");n(this,"lastResult",{rulesetName:"",ruleName:"",factName:"",outcome:""});n(this,"lastResults",[]);n(this,"knowledgeBase",new g);e!==void 0&&(this.knowledgeBase=e)}NewRuleset(e){const t=new M(e);return this._rulesetMap.set(e,t),this.lastRuleset=e,this}RemoveRuleset(e){return this._rulesetMap.delete(e),this.lastRuleset===e&&(this.lastRuleset=""),this}rulesetError(){return"Ruleset could not be found for this RuleForge (was it removed?)"}rulesetNameError(e){return`Ruleset '${e}' could not be found for this RuleForge (was it removed?)`}ruleError(e){return`Rule could not be found in ruleset '${e}' for this RuleForge`}ruleCheck(){if(this.lastRuleset=="")throw new Error(this.rulesetError());if(this.lastRule==="")throw new Error(this.ruleError(this.lastRuleset))}AddRule(e,t=this.baseAction){if(this.lastRuleset=="")throw new Error(this.rulesetError());const s=this._rulesetMap.get(this.lastRuleset);if(s===void 0)throw new Error(this.rulesetNameError(this.lastRuleset));return s.addRule(new _(e,t,this.knowledgeBase)),this.lastRule=e,this}AddRuleToRuleset(e,t,s=this.baseAction){const r=this._rulesetMap.get(e);if(r===void 0)throw new Error(this.rulesetNameError(e));return r.addRule(new _(t,s,this.knowledgeBase)),this}RemoveRuleFromRuleset(e,t){const s=this._rulesetMap.get(e);if(s===void 0)throw new Error(this.rulesetNameError(e));return s.removeRule(t),this}AddAction(e){this.ruleCheck();const t=this._rulesetMap.get(this.lastRuleset);if(t===void 0)throw new Error(this.rulesetNameError(this.lastRuleset));const s=t.getRule(this.lastRule);return s.action=new p(e),this}AddKnowledgeAction(e,t,s,r,o){this.ruleCheck();const l=this._rulesetMap.get(this.lastRuleset);if(l===void 0)throw new Error(this.rulesetNameError(this.lastRuleset));const d=l.getRule(this.lastRule),h={};h[s]={},h[s][r]=o;const a=new R(t,h);return d.action=new E(this.knowledgeBase,e,a),this}AddRelationshipAction(e,t,s,r="",o=""){this.ruleCheck();const l=this._rulesetMap.get(this.lastRuleset);if(l===void 0)throw new Error(this.rulesetNameError(this.lastRuleset));const d=l.getRule(this.lastRule),h=new m(t,s,o,r);return d.action=new A(this.knowledgeBase,e,h),this}AddActionToRule(e,t,s){const r=this._rulesetMap.get(e);if(r===void 0)throw new Error(this.rulesetNameError(e));return r.getRule(t).action=new p(s),this}AddKnowledgeActionToRule(e,t,s,r,o,l,d){const h=this._rulesetMap.get(e);if(h===void 0)throw new Error(this.rulesetNameError(e));const a={};a[o]={},a[o][l]=d;const S=new R(r,a);return h.getRule(t).action=new E(this.knowledgeBase,s,S),this}AddRelationshipActionToRule(e,t,s,r,o,l,d){const h=this._rulesetMap.get(e);if(h===void 0)throw new Error(this.rulesetNameError(e));const a=new m(r,o,d,l);return h.getRule(t).action=new A(this.knowledgeBase,s,a),this}ClearActionFromRule(e,t){const s=this._rulesetMap.get(e);if(s===void 0)throw new Error(this.rulesetNameError(e));return s.getRule(t).action=this.baseAction,this}AddCondition(e,t){this.ruleCheck();const s=this._rulesetMap.get(this.lastRuleset);if(s===void 0)throw new Error(this.rulesetNameError(this.lastRuleset));return s.getRule(this.lastRule).addCondition(new w(e,t)),this}AddConditionToRule(e,t,s,r){const o=this._rulesetMap.get(e);if(o===void 0)throw new Error(this.rulesetNameError(e));return o.getRule(t).addCondition(new w(s,r)),this}RemoveConditionFromRule(e,t,s){const r=this._rulesetMap.get(e);if(r===void 0)throw new Error(this.rulesetNameError(e));return r.getRule(t).removeCondition(s),this}RunRules(e,t=!0,s=!1){this.ruleCheck();const r=this._rulesetMap.get(this.lastRuleset);if(r===void 0)throw new Error(this.rulesetNameError(this.lastRuleset));return this.lastResult=r.runRules(e,t,s),this}GetLastResult(){if(this.ruleCheck(),this.lastResult.outcome==="")throw new Error("The rules have not been run for this RuleForge (did you call RunRules yet?)");return this.lastResult}GetResults(){this.ruleCheck();const e=this._rulesetMap.get(this.lastRuleset);if(e===void 0)throw new Error(this.rulesetNameError(this.lastRuleset));return this.lastResults.concat(e.results),e.results}GetResultsOnRuleset(e){const t=this._rulesetMap.get(e);if(t===void 0)throw new Error(this.rulesetNameError(e));return t.results}FireAllPasses(e){this.ruleCheck();const t=this._rulesetMap.get(this.lastRuleset);if(t===void 0)throw new Error(this.rulesetNameError(this.lastRuleset));return t.fireAllPasses(e),this}AddFact(e,t){return this._factMap.set(e,t),this}GetFact(e){return this._factMap.get(e)}DeleteFact(e){return this._factMap.delete(e),this}BulkRunRules(e=!0,t=!1){this.ruleCheck(),this.lastResults=[];const s=this._rulesetMap.get(this.lastRuleset);if(s===void 0)throw new Error(this.rulesetNameError(this.lastRuleset));return this._factMap.forEach(r=>{const o=s.runRules(r,e,t);this.lastResult=o,this.GetResults()}),this}BulkRunRulesOnRuleset(e,t=!0,s=!1){const r=this._rulesetMap.get(e);if(r===void 0)throw new Error(this.rulesetNameError(e));return this._factMap.forEach(o=>{const l=r.runRules(o,t,s);this.lastResult=l,this.GetResults()}),this}GetBulkResults(){return this.ruleCheck(),this.lastResults}}u.Action=p,u.COMPARE=f,u.Condition=w,u.Fact=R,u.KnowledgeBase=g,u.OUTCOME=c,u.Rule=_,u.RuleForge=k,u.Ruleset=M,u.StandardCondition=y,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});