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