(function(r,n){typeof exports==="object"&&typeof module!=="undefined"?n(exports,require("@ucast/mongo2js")):typeof define==="function"&&define.amd?define(["exports","@ucast/mongo2js"],n):(r=typeof globalThis!=="undefined"?globalThis:r||self,n((r.casl=r.casl||{},r.casl.extra={}),r.ucast.mongo2js))})(this,function(r,n){"use strict";function t(r){return Array.isArray(r)?r:[r]}var e=new Set(["__proto__","constructor","prototype"]);function i(r,n,t){var i=r;var u=n;if(n.indexOf(".")!==-1){var o=n.split(".");u=o.pop();i=o.reduce(function(r,n){if(e.has(n))return r;r[n]=r[n]||{};return r[n]},r)}if(!e.has(u))i[u]=t}var u=function r(n){return Array.isArray(n)?n.join(","):n};function o(r,n){return r.map(function(r){var e=[u(r.action||r.actions),typeof n==="function"?t(r.subject).map(n).join(","):u(r.subject),r.conditions||0,r.inverted?1:0,r.fields?u(r.fields):0,r.reason||""];while(e.length>0&&!e[e.length-1])e.pop();return e})}function f(r,n){return r.map(function(r){var t=r[0],e=r[1],i=r[2],u=r[3],o=r[4],f=r[5];var a=e.split(",");var c={inverted:!!u,action:t.split(","),subject:typeof n==="function"?a.map(n):a};if(i)c.conditions=i;if(o)c.fields=o.split(",");if(f)c.reason=f;return c})}function a(r,n,t,e){var i=r.detectSubjectType(t);var u=r.possibleRulesFor(n,i);var o=new Set;var f=o.delete.bind(o);var a=o.add.bind(o);var c=u.length;while(c--){var s=u[c];if(s.matchesConditions(t)){var v=s.inverted?f:a;e.fieldsFrom(s).forEach(v)}}return Array.from(o)}var c=function(){function r(r,n,t){this.t=r;this.i=n;this.u=t}var n=r.prototype;n.ofType=function r(n){return a(this.t,this.i,n,{fieldsFrom:this.o(n)})};n.of=function r(n){return a(this.t,this.i,n,{fieldsFrom:this.o(this.t.detectSubjectType(n))})};n.o=function r(n){var t=this;return function(r){return r.fields||t.u(n)}};return r}();function s(r,n,t){return r.rulesFor(n,t).reduce(function(r,n){if(n.inverted||!n.conditions)return r;return Object.keys(n.conditions).reduce(function(r,t){var e=n.conditions[t];if(!e||e.constructor!==Object)i(r,t,e);return r},r)},{})}function v(r,n,t,e){var i=[];var u=[];var o=r.rulesFor(n,t);for(var f=0;f<o.length;f++){var a=o[f];var c=a.inverted?i:u;if(!a.conditions)if(a.inverted)break;else return i.length?{$and:i}:{};else c.push(e(a))}if(!u.length)return null;return i.length?{$or:u,$and:i}:{$or:u}}function l(r){if(!r.ast)throw new Error('Ability rule "'+JSON.stringify(r)+'" does not have "ast" property. So, cannot be used to generate AST');return r.inverted?new n.CompoundCondition("not",[r.ast]):r.ast}function h(r,t,e){var i=v(r,t,e,l);if(i===null)return null;if(!i.$and)return i.$or?n.buildOr(i.$or):n.buildAnd([]);if(i.$or)i.$and.push(n.buildOr(i.$or));return n.buildAnd(i.$and)}r.AccessibleFields=c;r.packRules=o;r.permittedFieldsOf=a;r.rulesToAST=h;r.rulesToFields=s;r.rulesToQuery=v;r.unpackRules=f});
//# sourceMappingURL=index.js.map
