/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/fparser@4.2.0/dist/fparser.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
var L=Object.defineProperty,M=(t,r,e)=>r in t?L(t,r,{enumerable:!0,configurable:!0,writable:!0,value:e}):t[r]=e,r=(t,r,e)=>(M(t,"symbol"!=typeof r?r+"":r,e),e);function m(t,r,e){let i=t,s=null;for(let t of r){if(!["object","string"].includes(typeof i))throw new Error(`Cannot evaluate ${t}, property not found (from path ${e})`);if("object"==typeof i&&!(t in i))throw new Error(`Cannot evaluate ${t}, property not found (from path ${e})`);if("string"==typeof i&&!i.hasOwnProperty(t))throw new Error(`Cannot evaluate ${t}, property not found (from path ${e})`);s=i,i=i[t]}return"function"==typeof i&&s&&(i=i.bind(s)),i}class ${static throwIfNotNumber(t){if("string"==typeof t)throw new Error("Strings are not allowed in math operations")}}class E{static throwIfNotNumber(t){if("string"==typeof t)throw new Error("Strings are not allowed in math operations")}}class l{static createOperatorExpression(t,r,e){const i="string"==typeof t?t:String(t.value);if("^"===i)return new x(r,e);if(["*","/"].includes(i))return new g(i,r,e);if(["+","-"].includes(i))return new N(i,r,e);if(["<",">","<=",">=","=","!="].includes(i))return new f(i,r,e);throw new Error(`Unknown operator: ${i}`)}toString(){return""}}class b extends l{constructor(t){if(super(),r(this,"innerExpression"),this.innerExpression=t,!(this.innerExpression instanceof l))throw new Error("No inner expression given for bracket expression")}evaluate(t={}){return this.innerExpression.evaluate(t)}toString(){return`(${this.innerExpression.toString()})`}}class w extends l{constructor(t,e="number"){switch(super(),r(this,"value"),r(this,"type"),this.value=Number(t),e){case"number":if(this.value=Number(t),isNaN(this.value))throw new Error("Cannot parse number: "+t);break;case"string":this.value=String(t);break;default:throw new Error("Invalid value type: "+e)}this.type=e}evaluate(){return this.value}toString(){switch(this.type){case"number":return String(this.value);case"string":return'"'+this.value+'"';default:throw new Error("Invalid type")}}}class N extends l{constructor(t,e,i){if(super(),r(this,"operator"),r(this,"left"),r(this,"right"),!["+","-"].includes(t))throw new Error(`Operator not allowed in Plus/Minus expression: ${t}`);this.operator=t,this.left=e,this.right=i}evaluate(t={}){const r=this.left.evaluate(t),e=this.right.evaluate(t);if(E.throwIfNotNumber(r),E.throwIfNotNumber(e),"+"===this.operator)return Number(r)+Number(e);if("-"===this.operator)return Number(r)-Number(e);throw new Error("Unknown operator for PlusMinus expression")}toString(){return`${this.left.toString()} ${this.operator} ${this.right.toString()}`}}r(N,"PLUS","+"),r(N,"MINUS","-");class g extends l{constructor(t,e,i){if(super(),r(this,"operator"),r(this,"left"),r(this,"right"),!["*","/"].includes(t))throw new Error(`Operator not allowed in Multiply/Division expression: ${t}`);this.operator=t,this.left=e,this.right=i}evaluate(t={}){const r=this.left.evaluate(t),e=this.right.evaluate(t);if(E.throwIfNotNumber(r),E.throwIfNotNumber(e),"*"===this.operator)return Number(r)*Number(e);if("/"===this.operator)return Number(r)/Number(e);throw new Error("Unknown operator for MultDiv expression")}toString(){return`${this.left.toString()} ${this.operator} ${this.right.toString()}`}}r(g,"MULT","*"),r(g,"DIV","/");class x extends l{constructor(t,e){super(),r(this,"base"),r(this,"exponent"),this.base=t,this.exponent=e}evaluate(t={}){const r=this.base.evaluate(t),e=this.exponent.evaluate(t);return E.throwIfNotNumber(r),E.throwIfNotNumber(e),Math.pow(Number(r),Number(e))}toString(){return`${this.base.toString()}^${this.exponent.toString()}`}}class f extends l{constructor(t,e,i){if(super(),r(this,"operator"),r(this,"left"),r(this,"right"),!["<",">","<=",">=","=","!="].includes(t))throw new Error(`Operator not allowed in Logical expression: ${t}`);this.operator=t,this.left=e,this.right=i}evaluate(t={}){const r=this.left.evaluate(t),e=this.right.evaluate(t);switch(this.operator){case"<":return r<e?1:0;case">":return r>e?1:0;case"<=":return r<=e?1:0;case">=":return r>=e?1:0;case"=":return r===e?1:0;case"!=":return r!==e?1:0}throw new Error("Unknown operator for Logical expression")}toString(){return`${this.left.toString()} ${this.operator} ${this.right.toString()}`}}r(f,"LT","<"),r(f,"GT",">"),r(f,"LTE","<="),r(f,"GTE",">="),r(f,"EQ","="),r(f,"NEQ","!=");class I extends l{constructor(t,e,i=null){super(),r(this,"fn"),r(this,"varPath"),r(this,"argumentExpressions"),r(this,"formulaObject"),r(this,"blacklisted"),this.fn=t??"",this.varPath=this.fn.split("."),this.argumentExpressions=e||[],this.formulaObject=i,this.blacklisted=void 0}evaluate(t={}){t=t||{};const r=this.argumentExpressions.map((r=>r.evaluate(t)));try{let e=m(t,this.varPath,this.fn);if(e instanceof Function)return e.apply(this,r)}catch{}let e;try{e=m(this.formulaObject??{},this.varPath,this.fn)}catch{}if(this.formulaObject&&e instanceof Function){if(this.isBlacklisted())throw new Error("Blacklisted function called: "+this.fn);return e.apply(this.formulaObject,r)}try{const t=m(Math,this.varPath,this.fn);if(t instanceof Function)return r.forEach((t=>{$.throwIfNotNumber(t)})),t.apply(this,r)}catch{}throw new Error("Function not found: "+this.fn)}toString(){return`${this.fn}(${this.argumentExpressions.map((t=>t.toString())).join(", ")})`}isBlacklisted(){return void 0===this.blacklisted&&(this.blacklisted=v.functionBlacklist.includes(this.formulaObject?this.formulaObject[this.fn]:null)),this.blacklisted}}class y extends l{constructor(t,e=null){super(),r(this,"fullPath"),r(this,"varPath"),r(this,"formulaObject"),this.formulaObject=e,this.fullPath=t,this.varPath=t.split(".")}evaluate(t={}){let r;try{r=m(t,this.varPath,this.fullPath)}catch{}if(void 0===r&&(r=m(this.formulaObject??{},this.varPath,this.fullPath)),"function"==typeof r)throw new Error(`Cannot use ${this.fullPath} as value: It is a function and not allowed as a variable value.`);return r}toString(){return`${this.varPath.join(".")}`}}var a=(t=>(t.NUMBER="NUMBER",t.VARIABLE="VARIABLE",t.OPERATOR="OPERATOR",t.LOGICAL_OPERATOR="LOGICAL_OPERATOR",t.FUNCTION="FUNCTION",t.LEFT_PAREN="LEFT_PAREN",t.RIGHT_PAREN="RIGHT_PAREN",t.COMMA="COMMA",t.STRING="STRING",t.EOF="EOF",t))(a||{});const c=class t{constructor(){r(this,"input"),r(this,"position"),this.input="",this.position=0}tokenize(t){this.input=t,this.position=0;const r=[];for(;this.position<this.input.length&&(this.skipWhitespace(),!(this.position>=this.input.length));){const t=this.nextToken(r);t&&r.push(t)}return r.push({type:"EOF",value:"",raw:"",position:this.position,length:0}),r}nextToken(t){return this.readString()||this.readLogicalOperator()||this.readNumber(t)||this.readOperator()||this.readParenthesis()||this.readComma()||this.readIdentifier()||this.throwUnexpectedChar()}skipWhitespace(){const r=this.input.slice(this.position).match(t.PATTERNS.WHITESPACE);r&&(this.position+=r[0].length)}remaining(){return this.input.slice(this.position)}readNumber(r){const e=this.position,i=this.remaining().match(t.PATTERNS.NUMBER);if(!i)return null;const s=i[0];if(s.startsWith("-")){const t=r.length>0?r[r.length-1]:null;if(t&&"OPERATOR"!==t.type&&"LOGICAL_OPERATOR"!==t.type&&"COMMA"!==t.type&&"LEFT_PAREN"!==t.type)return null}return this.position+=s.length,{type:"NUMBER",value:parseFloat(s),raw:s,position:e,length:s.length}}readIdentifier(){const r=this.position,e=this.remaining();let i=e.match(t.PATTERNS.BRACKETED_IDENTIFIER);if(i){const t=i[0],e=i[1];if(""===e)throw new Error(`Empty bracketed variable at position ${r}`);if(!/^[a-zA-Z0-9_.]+$/.test(e)){const t=e.match(/[^a-zA-Z0-9_.]/),i=t?t[0]:e[0],s=r+1+e.indexOf(i);throw new Error(`Invalid character '${i}' in bracketed variable at position ${s}`)}this.position+=t.length;const s=this.position;this.skipWhitespace();const n=this.position<this.input.length&&"("===this.input[this.position];return this.position=s,{type:n?"FUNCTION":"VARIABLE",value:e,raw:t,position:r,length:t.length}}if(i=e.match(t.PATTERNS.IDENTIFIER),i){const t=i[0],e=t;this.position+=t.length;const s=this.position;this.skipWhitespace();const n=this.position<this.input.length&&"("===this.input[this.position];return this.position=s,{type:n?"FUNCTION":"VARIABLE",value:e,raw:t,position:r,length:t.length}}return null}readString(){const r=this.position,e=this.remaining();let i=e.match(t.PATTERNS.STRING_DOUBLE);if(i){const t=i[0],e=i[1].replace(/\\(.)/g,"$1");return this.position+=t.length,{type:"STRING",value:e,raw:t,position:r,length:t.length}}if(i=e.match(t.PATTERNS.STRING_SINGLE),i){const t=i[0],e=i[1].replace(/\\(.)/g,"$1");return this.position+=t.length,{type:"STRING",value:e,raw:t,position:r,length:t.length}}if(e.startsWith('"')||e.startsWith("'"))throw new Error(`Unterminated string at position ${r}`);return null}readOperator(){const r=this.position,e=this.remaining().match(t.PATTERNS.OPERATOR);if(!e)return null;const i=e[0];return this.position+=i.length,{type:"OPERATOR",value:i,raw:i,position:r,length:i.length}}readLogicalOperator(){const r=this.position,e=this.remaining();if(e.startsWith("!")&&!e.startsWith("!="))throw new Error(`Invalid operator '!' at position ${r}. Did you mean '!='?`);const i=e.match(t.PATTERNS.LOGICAL_OPERATOR);if(!i)return null;const s=i[0];return this.position+=s.length,{type:"LOGICAL_OPERATOR",value:s,raw:s,position:r,length:s.length}}readParenthesis(){const r=this.position,e=this.remaining();let i=e.match(t.PATTERNS.LEFT_PAREN);if(i){const t=i[0];return this.position+=t.length,{type:"LEFT_PAREN",value:t,raw:t,position:r,length:t.length}}if(i=e.match(t.PATTERNS.RIGHT_PAREN),i){const t=i[0];return this.position+=t.length,{type:"RIGHT_PAREN",value:t,raw:t,position:r,length:t.length}}return null}readComma(){const r=this.position,e=this.remaining().match(t.PATTERNS.COMMA);if(!e)return null;const i=e[0];return this.position+=i.length,{type:"COMMA",value:i,raw:i,position:r,length:i.length}}throwUnexpectedChar(){const t=this.input[this.position]||"EOF";throw new Error(`Unexpected character '${t}' at position ${this.position}`)}};r(c,"PATTERNS",{WHITESPACE:/^\s+/,NUMBER:/^-?\d+(\.\d+)?([eE][+\-]?\d+)?/,IDENTIFIER:/^[a-zA-Z_][a-zA-Z0-9_.]*/,BRACKETED_IDENTIFIER:/^\[([^\]]*)\]/,STRING_DOUBLE:/^"((?:[^"\\]|\\.)*)"/,STRING_SINGLE:/^'((?:[^'\\]|\\.)*)'/,LOGICAL_OPERATOR:/^(<=|>=|!=|<|>|=)/,OPERATOR:/^[+\-*/^]/,LEFT_PAREN:/^\(/,RIGHT_PAREN:/^\)/,COMMA:/^,/});let R=c;const P={"=":1,"!=":1,"<":1,">":1,"<=":1,">=":1,"+":2,"-":2,"*":3,"/":3,"^":4};class T{constructor(t,e){r(this,"tokens"),r(this,"current"),r(this,"formulaObject"),this.tokens=t,this.current=0,this.formulaObject=e}parse(){const t=this.parseExpression(0);if(!this.isAtEnd()){const t=this.peek();throw new Error(`Unexpected token '${t.value}' at position ${t.position}: Expected end of expression`)}return t}parseExpression(t){let r=this.parsePrimary();for(;!this.isAtEnd();){const e=this.peek();if(e.type!==a.OPERATOR&&e.type!==a.LOGICAL_OPERATOR)break;const i=this.getPrecedence(e);if(i<t)break;const s="^"===e.value?i:i+1;this.consume();const n=this.parseExpression(s);r=l.createOperatorExpression(e,r,n)}return r}parsePrimary(){const t=this.peek();if(this.match(a.OPERATOR)&&"-"===t.value){this.consume();const t=this.parsePrimary();return new g("*",new w(-1),t)}if(this.match(a.OPERATOR)&&"+"===t.value)return this.consume(),this.parsePrimary();if(this.match(a.NUMBER))return this.consume(),new w(t.value);if(this.match(a.STRING))return this.consume(),new w(t.value,"string");if(this.match(a.LEFT_PAREN))return this.parseParenthesizedExpression();if(this.match(a.VARIABLE,a.FUNCTION))return this.parseVariableOrFunction();throw new Error(`Unexpected token '${t.value}' at position ${t.position}: Expected number, variable, function, or '('`)}parseParenthesizedExpression(){const t=this.consume(a.LEFT_PAREN),r=this.parseExpression(0);if(!this.match(a.RIGHT_PAREN)){const r=this.peek();throw new Error(`Missing closing parenthesis at position ${r.position}: Expected ')' to match '(' at position ${t.position}`)}return this.consume(a.RIGHT_PAREN),new b(r)}parseVariableOrFunction(){const t=this.consume(),r=t.value;return this.match(a.LEFT_PAREN)?this.parseFunctionCall(r,t.position):(this.formulaObject.registerVariable(r),new y(r,this.formulaObject))}parseFunctionCall(t,r){const e=this.consume(a.LEFT_PAREN),i=[];if(!this.match(a.RIGHT_PAREN))do{i.push(this.parseExpression(0))}while(this.matchAndConsume(a.COMMA));if(!this.match(a.RIGHT_PAREN)){const r=this.peek();throw new Error(`Missing closing parenthesis for function '${t}' at position ${r.position}: Expected ')' to match '(' at position ${e.position}`)}return this.consume(a.RIGHT_PAREN),new I(t,i,this.formulaObject)}peek(){return this.tokens[this.current]}consume(t){const r=this.peek();if(t&&r.type!==t)throw new Error(`Expected ${t} at position ${r.position}, got ${r.type} ('${r.value}')`);return this.current++,r}match(...t){return t.includes(this.peek().type)}matchAndConsume(t){return!!this.match(t)&&(this.consume(),!0)}isAtEnd(){return this.peek().type===a.EOF}getPrecedence(t){if(t.type===a.LOGICAL_OPERATOR){const r=t.value;return P[r]??0}if(t.type===a.OPERATOR){const r=t.value;return P[r]??0}return 0}}const A={PI:Math.PI,E:Math.E,LN2:Math.LN2,LN10:Math.LN10,LOG2E:Math.LOG2E,LOG10E:Math.LOG10E,SQRT1_2:Math.SQRT1_2,SQRT2:Math.SQRT2},h=class t{constructor(t,e={}){r(this,"formulaExpression"),r(this,"options"),r(this,"formulaStr"),r(this,"_variables"),r(this,"_memory"),this.formulaExpression=null,this.options={memoization:!1,...e},this.formulaStr="",this._variables=[],this._memory={},this.setFormula(t)}setFormula(t){return t&&(this.formulaExpression=null,this._variables=[],this._memory={},this.formulaStr=t,this.formulaExpression=this.parse(t)),this}enableMemoization(){this.options.memoization=!0}disableMemoization(){this.options.memoization=!1,this._memory={}}parse(t){const r=(new R).tokenize(t);return new T(r,this).parse()}registerVariable(t){this._variables.indexOf(t)<0&&this._variables.push(t)}getVariables(){return this._variables}evaluate(t){if(t instanceof Array)return t.map((t=>this.evaluate(t)));let r=this.getExpression();if(!(r instanceof l))throw new Error("No expression set: Did you init the object with a Formula?");if(this.options.memoization){let e=this.resultFromMemory(t);return null!==e||(e=r.evaluate({...A,...t}),this.storeInMemory(t,e)),e}return r.evaluate({...A,...t})}hashValues(t){return JSON.stringify(t)}resultFromMemory(t){let r=this.hashValues(t),e=this._memory[r];return void 0!==e?e:null}storeInMemory(t,r){this._memory[this.hashValues(t)]=r}getExpression(){return this.formulaExpression}getExpressionString(){return this.formulaExpression?this.formulaExpression.toString():""}static calc(r,e=null,i={}){return e=e??{},new t(r,i).evaluate(e)}ifElse(t,r,e){return t?r:e}first(...t){for(const r of t)if(r instanceof Array){let t=this.first(...r);if(t)return t}else if(r)return r;if(t.length>0){const r=t[t.length-1];return r instanceof Array?this.first(...r):r}throw new Error("first(): At least one argument is required")}};r(h,"Expression",l),r(h,"BracketExpression",b),r(h,"PowerExpression",x),r(h,"MultDivExpression",g),r(h,"PlusMinusExpression",N),r(h,"LogicalExpression",f),r(h,"ValueExpression",w),r(h,"VariableExpression",y),r(h,"FunctionExpression",I),r(h,"MATH_CONSTANTS",A),r(h,"ALLOWED_FUNCTIONS",["ifElse","first"]),r(h,"Tokenizer",R),r(h,"TokenType",a),r(h,"Parser",T),r(h,"functionBlacklist",Object.getOwnPropertyNames(h.prototype).filter((t=>h.prototype[t]instanceof Function&&!h.ALLOWED_FUNCTIONS.includes(t))).map((t=>h.prototype[t])));let v=h;export{v as default};
//# sourceMappingURL=/sm/a6625da8697423e9167799e81a1e1a05a47bf14cbd496624d634944a696ca0f8.map