"use strict";exports.id=1997,exports.ids=[1997],exports.modules={51997:(a,b,c)=>{c.a(a,async(a,d)=>{try{c.d(b,{claimEmailInvitesViaCloud:()=>r,mirrorCloudMembershipsForUser:()=>s}),c(77598);var e=c(84104),f=c(81909),g=c(16343),h=c(37937),i=c(79336),j=a([e,f,g,h,i]);function k(){}function l(a,b={}){try{let c=(0,f.r6)(a,{agent_id:b.agent_id,project_id:b.project_id});if(c)return c}catch{}return process.env[a]||null}function m(a,b={}){for(let[c,d]of Object.entries(b)){if(!/^[A-Za-z_][A-Za-z0-9_]*$/.test(c))throw Error(`Invalid filter column: ${c}`);a.searchParams.set(c,String(d).includes(".")?String(d):`eq.${d}`)}}function n({baseUrl:a,apikey:b,bearer:c,agentId:d=null}){let e=`${a.replace(/\/+$/g,"")}/rest/v1`;function f(a={}){return{Authorization:`Bearer ${c}`,apikey:b,Accept:"application/json",...a}}async function h(a){let b={};try{b=await a.json()}catch{}return b.message||b.error||a.statusText||`HTTP ${a.status}`}return{async select(a,{filters:b={},select:c="*",limit:i=100}={}){let j=new URL(`${e}/${a}`);j.searchParams.set("select",c),j.searchParams.set("limit",String(Math.min(Math.max(Number(i)||1,1),1e3))),m(j,b);let k=await (0,g.N)("shared_cloud_select",d)(j,{headers:f()});if(!k.ok)throw Error(`shared_cloud select(${a}) failed: ${await h(k)}`);return k.json()},async upsert(a,b,{onConflict:c}={}){let i=Array.isArray(b)?b:[b],j=new URL(`${e}/${a}`);c&&j.searchParams.set("on_conflict",String(c));let k=await (0,g.N)("shared_cloud_upsert",d)(j,{method:"POST",headers:f({"Content-Type":"application/json",Prefer:"resolution=merge-duplicates,return=representation"}),body:JSON.stringify(i)});if(!k.ok)throw Error(`shared_cloud upsert(${a}) failed: ${await h(k)}`);return k.json()},async delete(a,{filters:b}={}){if(!b||!Object.keys(b).length)throw Error("shared_cloud delete requires filters; bulk delete is blocked.");let c=new URL(`${e}/${a}`);m(c,b);let i=await (0,g.N)("shared_cloud_delete",d)(c,{method:"DELETE",headers:f({Prefer:"return=representation"})});if(!i.ok)throw Error(`shared_cloud delete(${a}) failed: ${await h(i)}`);return i.json()},async rpc(a,b={}){if(!/^[A-Za-z_][A-Za-z0-9_]*$/.test(a))throw Error(`Invalid rpc function name: ${a}`);let c=new URL(`${e}/rpc/${a}`),i=await (0,g.N)("shared_cloud_rpc",d)(c,{method:"POST",headers:f({"Content-Type":"application/json"}),body:JSON.stringify(b??{})});if(!i.ok)throw Error(`shared_cloud rpc(${a}) failed: ${await h(i)}`);return i.json()}}}function o(a={}){let b=l("SUPABASE_URL",a),c=l("SUPABASE_ANON_KEY",a);return!!(b&&c)}function p(a,b={}){if(k(),!a)throw Error("getSharedCloudUserClient: an access token (user JWT) is required");let c=l("SUPABASE_URL",b),d=l("SUPABASE_ANON_KEY",b);if(!c||!d)throw Error("Shared cloud (user) not configured: set SUPABASE_URL and SUPABASE_ANON_KEY.");return n({baseUrl:c,apikey:d,bearer:a,agentId:b.agent_id??null})}function q(a,b,c,d){let f=(0,e.Lf)();if(!f.prepare("SELECT id FROM organizations WHERE id = ?").get(a)){let b=(0,e.kF)();f.prepare(`INSERT INTO organizations (id, slug, name, plan, created_at, updated_at)
       VALUES (?, ?, ?, 'free', ?, ?) ON CONFLICT(id) DO NOTHING`).run(a,`org-${String(a).slice(-8)}`,d||String(a),b,b)}f.prepare("SELECT id FROM org_memberships WHERE org_id = ? AND user_id = ?").get(a,b)||f.prepare("INSERT INTO org_memberships (id, org_id, user_id, role, accepted_at) VALUES (?, ?, ?, ?, ?)").run((0,e.Si)("mem"),a,b,c,(0,e.kF)())}async function r(a,b={}){let c,d=a?.supabase_access_token;if(!d||!o(b))return[];try{c=await p(d,b).rpc("claim_email_invites")}catch{return[]}let e=Array.isArray(c)?c:[],f=[];for(let b of e){let c=b.granted_role||b.role;b.org_id&&(q(b.org_id,a.user_id,c),f.push({org_id:b.org_id,role:c}))}return f}async function s(a,b={}){let c,d,e=a?.user_id;if(!e)return[];let f=a?.supabase_access_token||null,g=function({serviceReady:a,userReady:b,accessToken:c}){return a?"admin":b&&c?"user":null}({serviceReady:function(a={}){let b=l("SUPABASE_URL",a),c=l("SUPABASE_SERVICE_ROLE_KEY",a);return!!(b&&c)}(b),userReady:o(b),accessToken:f});if(!g)return[];try{c="admin"===g?function(a={}){let b=l("SUPABASE_URL",a),c=l("SUPABASE_SERVICE_ROLE_KEY",a);if(!b||!c)throw Error("Shared cloud not configured: set SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY.");return n({baseUrl:b,apikey:c,bearer:c,agentId:a.agent_id??null})}(b):p(f,b)}catch{return[]}try{d=await c.select("org_memberships",{filters:{user_id:e},select:"org_id,role",limit:100})}catch{return[]}if(!Array.isArray(d)||0===d.length)return[];let h=[...new Set(d.map(a=>a.org_id).filter(Boolean))],i=new Map;try{if(h.length){let a=await c.select("organizations",{filters:{id:`in.(${h.join(",")})`},select:"id,name",limit:1e3});for(let b of Array.isArray(a)?a:[])b.id&&i.set(b.id,b.name)}}catch{}let j=[];for(let a of d)a.org_id&&(q(a.org_id,e,a.role,i.get(a.org_id)),j.push({org_id:a.org_id,role:a.role}));return j}[e,f,g,h,i]=j.then?(await j)():j,Object.freeze(["private","team","org","link"]),Object.freeze(["org","team","private"]),Object.freeze(["dashboard_space","project"]),Object.freeze(["folder","file","artifact"]),Object.freeze(["user","team","org"]),Object.freeze(["user","org"]),Object.freeze(["read","write","admin"]),Object.freeze({read:1,write:2,admin:3}),d()}catch(a){d(a)}})}};