const d=["view","comment","edit","manage"];function y(a,e){return d.indexOf(a)>=d.indexOf(e)}const i="wkmsreact_rbac",n={grant:a=>`share.grant.${a}`,byOwner:(a,e)=>`share.byowner.${a}.${e}`,byGrantee:(a,e)=>`share.bygrantee.${a}.${e}`,grantPrefix:"share.grant.",byOwnerPrefix:a=>`share.byowner.${a}.`,byGranteePrefix:a=>`share.bygrantee.${a}.`};function l(){return crypto.randomUUID()}class u{constructor(e){this.relay=e}async grant(e){const t={grantId:l(),scope:e.scope,nodeId:e.nodeId,nodeName:e.nodeName,ownerUuid:e.ownerUuid,ownerName:e.ownerName,granteeUuid:e.granteeUuid,granteeName:e.granteeName,permission:e.permission,canClone:e.canClone??!1,createdAt:new Date().toISOString(),status:"active",note:e.note};return await this.relay.kvPut(i,n.grant(t.grantId),t),await this.relay.kvPut(i,n.byOwner(t.ownerUuid,t.grantId),{grantId:t.grantId}),await this.relay.kvPut(i,n.byGrantee(t.granteeUuid,t.grantId),{grantId:t.grantId}),t}async revoke(e){const t=await this.relay.kvGet(i,n.grant(e));if(!t)return;const s={...t.value,status:"revoked"};return await this.relay.kvPut(i,n.grant(e),s),s}async grantsByIndex(e){const s=(await Promise.all(e.map(r=>this.relay.kvGet(i,r)))).filter(r=>!!r).map(r=>r.value.grantId);return(await Promise.all(s.map(r=>this.relay.kvGet(i,n.grant(r))))).filter(r=>!!r).map(r=>r.value).sort((r,c)=>c.createdAt.localeCompare(r.createdAt))}async listShared(e){const t=await this.relay.kvKeys(i,`${n.byOwnerPrefix(e)}>`);return this.grantsByIndex(t.filter(s=>s.startsWith(n.byOwnerPrefix(e))))}async listSharedWithMe(e){const t=await this.relay.kvKeys(i,`${n.byGranteePrefix(e)}>`);return(await this.grantsByIndex(t.filter(o=>o.startsWith(n.byGranteePrefix(e))))).filter(o=>o.status==="active")}async effectivePermission(e,t,s){const o=(await this.listSharedWithMe(e)).filter(r=>r.scope===t&&r.nodeId===s);return o.length===0?null:o.reduce((r,c)=>y(c.permission,r)?c.permission:r,"view")}}export{d as PERMISSION_ORDER,n as SHARE_KEYS,u as ShareService,y as permAtLeast};
//# sourceMappingURL=share_service-Dj3HWfwH.js.map
