import{buildOr as r,buildAnd as n,CompoundCondition as t}from"@ucast/mongo2js";function i(r){return Array.isArray(r)?r:[r]}var e=new Set(["__proto__","constructor","prototype"]);function u(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 o=function r(n){return Array.isArray(n)?n.join(","):n};function f(r,n){return r.map(function(r){var t=[o(r.action||r.actions),typeof n==="function"?i(r.subject).map(n).join(","):o(r.subject),r.conditions||0,r.inverted?1:0,r.fields?o(r.fields):0,r.reason||""];while(t.length>0&&!t[t.length-1])t.pop();return t})}function a(r,n){return r.map(function(r){var t=r[0],i=r[1],e=r[2],u=r[3],o=r[4],f=r[5];var a=i.split(",");var c={inverted:!!u,action:t.split(","),subject:typeof n==="function"?a.map(n):a};if(e)c.conditions=e;if(o)c.fields=o.split(",");if(f)c.reason=f;return c})}function c(r,n,t,i){var e=r.detectSubjectType(t);var u=r.possibleRulesFor(n,e);var o=new Set;var f=o.delete.bind(o);var a=o.add.bind(o);var c=u.length;while(c--){var v=u[c];if(v.matchesConditions(t)){var s=v.inverted?f:a;i.fieldsFrom(v).forEach(s)}}return Array.from(o)}var v=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 c(this.t,this.i,n,{fieldsFrom:this.o(n)})};n.of=function r(n){return c(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 i=n.conditions[t];if(!i||i.constructor!==Object)u(r,t,i);return r},r)},{})}function h(r,n,t,i){var e=[];var u=[];var o=r.rulesFor(n,t);for(var f=0;f<o.length;f++){var a=o[f];var c=a.inverted?e:u;if(!a.conditions)if(a.inverted)break;else return e.length?{$and:e}:{};else c.push(i(a))}if(!u.length)return null;return e.length?{$or:u,$and:e}:{$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 t("not",[r.ast]):r.ast}function p(t,i,e){var u=h(t,i,e,l);if(u===null)return null;if(!u.$and)return u.$or?r(u.$or):n([]);if(u.$or)u.$and.push(r(u.$or));return n(u.$and)}export{v as AccessibleFields,f as packRules,c as permittedFieldsOf,p as rulesToAST,s as rulesToFields,h as rulesToQuery,a as unpackRules};
//# sourceMappingURL=index.js.map
