import{__awaiter as r,__generator as e}from"tslib";import{create as s}from"zustand";import{appsecApi as n}from"../apis/scopeApis.js";import i from"./useEnvStore.js";var t=function(r,e){return!!e&&function(r){return r?"string"==typeof r?[r]:[r._id,r.id,r.resourceId,r.resource_id].filter(function(r){return"string"==typeof r&&r.length>0}):[]}(r).includes(e)},o=function(r,e){for(var s,n,i=new Set,o=0,a=r;o<a.length;o++){var c=a[o];if(t(c.resourceId,e))for(var u=0,l=null!==(n=null===(s=c.role)||void 0===s?void 0:s.permissions)&&void 0!==n?n:[];u<l.length;u++){var d=l[u];i.add(d.name)}}return i},a=s(function(s,t){return{accessData:[],isLoading:!1,error:null,fetchPermissions:function(i,t){return r(void 0,void 0,void 0,function(){var r,o,a;return e(this,function(e){switch(e.label){case 0:if(!i)return[2];s({isLoading:!0,error:null}),e.label=1;case 1:return e.trys.push([1,3,,4]),[4,n.get("/users/me",{params:{appId:i,resourceId:t}})];case 2:return r=e.sent(),s({accessData:(null===(a=r.data)||void 0===a?void 0:a.data)||[],isLoading:!1}),[3,4];case 3:return o=e.sent(),console.error("Failed to fetch permissions:",o),s({error:o.message||"Failed to fetch permissions",isLoading:!1}),[3,4];case 4:return[2]}})})},hasPermission:function(r,e){var s,n=t().accessData,a=null===(s=i.getState().rbacConfig)||void 0===s?void 0:s.applicationResourceId;if(a&&o(n,a).has(r))return!0;if(e&&o(n,e).has(r))return!0;return!1},hasGlobalPermission:function(r,e){return!!e&&(t().hasApplicationAccess(e)||t().hasPermission(r,e))},hasApplicationAccess:function(r){if(!r)return!1;var e=t().accessData;return o(e,r).has("ilotj.scope.access.application.user.add")},reset:function(){return s({accessData:[],isLoading:!1,error:null})}}});export{a as usePermissionsStore};
//# sourceMappingURL=usePermissionsStore.js.map
