"use strict";var e=require("acorn");class r extends Error{constructor(r){super(r),this.code="SYNTAX_ERROR"}}function n(r){return Object.prototype.toString.call(r).slice(8,-1).toLowerCase()}function t(r){const e=n(r);return["string","number","boolean","null"].includes(e)}function i(r){return"number"===n(r)}function s(r){return"boolean"===n(r)}function o(r,e,n){if(e&&n){const t=e.start,o=e.end;r+=`\n${n.permission}`;const i=new Array(o).fill(" ",0,t);i.fill("^",t,o),r+=`\n${i.join("")}`}return r}const a={LogicalExpression:(e,n,t={})=>{let{left:i,operator:a,right:u}=e;if(!["&&","||"].includes(a))throw new r(o(`不支持的操作符：${a} `,e,n));if(i=c(i,n),u=c(u,n),!s(i))throw new r(o(`运算符[${a}]左侧值不合法`,e,n));if(!s(u))throw new r(o(`运算符[${a}]右侧值不合法`,e,n));switch(a){case"&&":return i&&u;case"||":return i=c(i,n),u=c(u,n),i||u}const l={"&&":"and","||":"or"}[a];if(!l)throw new r(o(`不支持的操作符：${a} `,e,n));return i=c(i,n),u=c(u,n),i[l](u)},UnaryExpression:function(e,n,t={}){const{operator:i,argument:s}=e;if("!"!==i)throw new r(o(`不支持的运算符：${i}`,e,n));return!c(s,n)},BinaryExpression:(e,n,s={})=>{let{left:a,operator:u,right:l}=e;const f=["+","-","*","/"].indexOf(u)>-1,w=[">","<","==",">=","<=","!="].includes(u),p="in"===u;if(!f&&!w&&!p)throw new r(o(`不支持的运算符：${u} `,e,n));if(a=c(a,n),l=c(l,n),void 0===a||void 0===l)return!1;if(w){if(!t(a))throw new r(o(`运算符[${u}]左侧值不合法`,e,n));if(!t(l))throw new r(o(`运算符[${u}]右侧值不合法`,e,n))}if(f){if(!i(a))throw new r(o(`运算符[${u}]左侧值不合法`,e,n));if(!i(l))throw new r(o(`运算符[${u}]右侧值不合法`,e,n))}if(p){if(!t(a))throw new r(o(`运算符[${u}]左侧值不合法`,e,n));if(h=l,!Array.isArray(h)||!h.every((r=>t(r))))throw new r(o(`运算符[${u}]右侧值不合法`,e,n))}var h;let b=!1;switch(u){case">":b=a>l;break;case"<":b=a<l;break;case"==":b=a===l;break;case">=":b=a>=l;break;case"<=":b=a<=l;break;case"!=":b=a!==l;break;case"in":b=l.includes(a);break;case"+":b=a+l;break;case"-":b=a-l;break;case"*":b=a*l;break;case"/":b=a/l;break;default:throw new r(o(`不支持的运算符：${u} `,e,n))}return f?b:!0===b},MemberExpression:(e,t,i={})=>{let{object:s,property:a}=e;if("Identifier"!==a.type&&"Literal"!==a.type)throw new r(o("对象键值不合法",e,t));s=c(s,t,{isObject:!0}),a=c(a,t,{isProp:!0});const u=n(s);return"object"!==u&&"array"!==u||null==s?null:s[a]},ArrayExpression:(e,n,t={})=>{if(e.elements.some((r=>"Literal"!==r.type)))throw new r(o("数组内元素仅支持字面量",e,n));return e.elements.map((r=>c(r,n)))},Identifier:(e,n,t={})=>{const i=e.name;if("undefined"===i)throw new r(o("表达式内不允许使用undefined",e,n));return t.isProp?i:(s=n.values,c=i,Object.prototype.hasOwnProperty.call(s,c)?n.values[i]:i);var s,c},Literal:(e,n,i={})=>{const s=e.value;if(!t(s))throw new r(o("不支持的数据类型",e,n));return s}};function c(e,n,t={}){if(!e.type||!a[e.type])throw new r(o("错误的规则表达式",e,n));return a[e.type](e,n,t)}module.exports=class{constructor(r,e){this.permission=r,this.values=e}verify(){return!0===function(n,t){const o=e.Parser.parse(n,{ecmaVersion:2020}).body;if(!o||1!==o.length)throw new r("权限表达式必须是单一语句");return c(o[0].expression,function(r){const{permission:e,values:n}=r;return{permission:e,values:n}}({permission:n,values:t}))}(this.permission,this.values)}};