import{createFactory as t,$eq as s,$ne as i,$lt as e,$lte as n,$gt as r,$gte as o,$in as h,$nin as c,$all as u,$size as a,$regex as l,$options as f,$elemMatch as d,$exists as p,eq as g,ne as y,lt as w,lte as b,gt as $,gte as m,within as v,nin as x,all as M,size as E,regex as j,elemMatch as _,exists as F}from"@ucast/mongo2js";function A(t){return Array.isArray(t)?t:[t]}const O="__caslSubjectType__";function C(t,s){if(s)if(s.hasOwnProperty(O)){if(t!==s[O])throw new Error(`Trying to cast object to subject type ${t} but previously it was casted to ${s[O]}`)}else Object.defineProperty(s,O,{value:t});return s}function R(t){if(!t)return"all";if("string"==typeof t)return t;if(t.hasOwnProperty(O))return t[O];const s="function"==typeof t?t:t.constructor;return s.modelName||s.name}const z=t=>t;function T(t){return"production"!==process.env.NODE_ENV&&function(t){if(t.manage)throw new Error('Cannot add alias for "manage" action because it is reserved');Object.keys(t).forEach(s=>{if(s===t[s]||Array.isArray(t[s])&&(-1!==t[s].indexOf(s)||-1!==t[s].indexOf("manage")))throw new Error(`Attempt to alias action to itself: ${s} -> ${t[s]}`)})}(t),s=>function(t,s){let i=A(s),e=0;for(;e({rules:[],merged:!1}),U=()=>new Map,k=()=>({emits:!1,last:null,destroy:null});class B extends class{constructor(t=[],s={}){this.o=!1,this.h=new Map,this.u={conditionsMatcher:s.conditionsMatcher,fieldMatcher:s.fieldMatcher,resolveAction:s.resolveAction||z},this.detectSubjectType=s.detectSubjectType||R,this.l=t,this.p=this.g(t)}get rules(){return this.l}update(t){const s={rules:t,ability:this,target:this};return this.$("update",s),this.l=t,this.p=this.g(t),this.$("updated",s),this}g(t){const s=new Map;for(let i=t.length-1;i>=0;i--){const e=t.length-i-1,n=new D(t[i],this.u,e),r=A(n.action),o=A(n.subject);this.m(n);for(let t=0;tt.matchesField(i)):e}on(t,s){const i=Y(this.h,t,k),e=new I(s,i.last);i.last=e;const n=()=>{if(i.emits)return i.destroy=i.destroy||[],void i.destroy.push(n);e.next||e.prev||i.last!==e?e.destroy():i.last=null};return n}$(t,s){const i=this.h.get(t);if(i)try{i.emits=!0;let t=i.last;for(;null!==t;)t.value(s),t=t.prev}finally{i.emits=!1,i.destroy&&i.destroy.forEach(t=>t())}}}{can(...t){const s=this.relevantRuleFor(...t);return!!s&&!s.inverted}relevantRuleFor(...t){const s=this.rulesFor(...t),i=t[1];for(let t=0;tt(Object.assign({},G,s),Object.assign({},H,i),e),K=t(G,H),N=/[-/\\^$+?.()|[\]{}]/g,P=/\.?\*+\.?/g,Q=/\*+/,V=/\./g;function W(t,s,i){const e="*"===i[0]||"."===t[0]&&"."===t[t.length-1]?"+":"*",n=-1===t.indexOf("**")?"[^.]":".",r=t.replace(V,"\\$&").replace(Q,n+e);return s+t.length===i.length?`(?:${r})?`:r}function X(t,s,i){return"."!==t||"*"!==i[s-1]&&"*"!==i[s+1]?"\\"+t:t}const Z=t=>{let s;return i=>(void 0===s&&(s=-1===t.join("").indexOf("*")?null:function(t){const s=t.map(t=>t.replace(N,X).replace(P,W)),i=s.length>1?`(?:${s.join("|")})`:s[0];return new RegExp(`^${i}$`)}(t)),null===s||-1!==i.indexOf("*")?-1!==t.indexOf(i):s.test(i))};class tt extends B{constructor(t,s){super(t,Object.assign({conditionsMatcher:K,fieldMatcher:Z},s))}}class st{constructor(t){this.v=t}because(t){return this.v.reason=t,this}}class it{constructor(t){this.rules=[],this.M=t;this.can=this.can.bind(this),this.cannot=this.cannot.bind(this),this.build=this.build.bind(this)}can(t,s,i,e){const n={action:t};return s&&(n.subject=s,Array.isArray(i)||"string"==typeof i?n.fields=i:void 0!==i&&(n.conditions=i),void 0!==e&&(n.conditions=e)),this.rules.push(n),new st(n)}cannot(t,s,i,e){const n=this.can(t,s,i,e);return n.v.inverted=!0,n}build(t){return new this.M(this.rules,t)}}function et(t,s){const i=new it(tt),e=t(i.can,i.cannot);return e&&"function"==typeof e.then?e.then(()=>i.build(s)):i.build(s)}const nt=t=>`Cannot execute "${t.action}" on "${t.subjectType}"`,rt=function(t){this.message=t};rt.prototype=Object.create(Error.prototype);class ot extends rt{static setDefaultMessage(t){this.j="string"==typeof t?()=>t:t}static from(t){return new this(t)}constructor(t){super(""),this.field=void 0,this.ability=t,"function"==typeof Error.captureStackTrace&&(this.name="ForbiddenError",Error.captureStackTrace(this,this.constructor))}setMessage(t){return this.message=t,this}throwUnlessCan(...t){const s=this.ability.relevantRuleFor(...t);if(s&&!s.inverted)return;this.action=t[0],this.subject=t[1],this.subjectType=this.ability.detectSubjectType(t[1]),this.field=t[2];const i=s?s.reason:"";throw this.message=this.message||i||this.constructor.j(this),this}}ot.j=nt;export{tt as Ability,it as AbilityBuilder,ot as ForbiddenError,B as PureAbility,J as buildMongoQueryMatcher,T as createAliasResolver,et as defineAbility,R as detectSubjectType,Z as fieldPatternMatcher,nt as getDefaultErrorMessage,K as mongoQueryMatcher,C as subject,A as wrapArray}; //# sourceMappingURL=index.mjs.map