import{b as u,f6 as l,y as s,z as f,ba as i,dD as m}from"./index-DijM3Brt.js";import{a as c}from"./api-D8P27G7r.js";function P(t){const e=t.replace(/\/api\/space\/.+/,""),a=t.replace(/\/$/,"").split("/"),r=a.at(-4),n=a.at(-2);return s(e,"space",r,"apps",n,`explorer?key=/apps/${n}/.did-objects/${n}/`)}function U(t){const e=t.replace(/\/api\/space\/.+/,""),r=t.replace(/\/$/,"").split("/").at(-4);return s(e,"space",r)}function h(t){return t.replace(/\/$/,"").split("/").at(-4)}async function C(t){try{if(!i(t))return!1;const{headers:e}=await c.head(t,{timeout:5e3});return e["x-listable"]==="true"&&e["x-readable"]==="true"&&e["x-writeable"]==="true"}catch(e){return console.error(e),!1}}function L(t){return t?.find(e=>e.key===u.BLOCKLET_CONFIGURABLE_KEY.BLOCKLET_APP_BACKUP_ENDPOINT)?.value||""}function y(t){return t?.replace(/\/api\/space\/.+/,"")}function E(t){const e=new URL(t);return s(e.origin,e.pathname)}async function _(t){try{if(!i(t))return!1;const e=E(t),a=s(e,"space/api/did/one-click-authorization/token"),{status:r,data:n}=await c.get(a,{timeout:5e3});return r===200&&m(n)}catch(e){return console.error(e),!1}}async function w(t,{timeout:e}={timeout:3e3}){try{if(!i(t))return"";const{headers:a}=await c.head(t,{timeout:e}),r=a["x-space-name"];return r?decodeURIComponent(r):""}catch(a){return console.error(a),""}}const A=t=>t?.configs?.find(e=>e.key===u.BLOCKLET_CONFIGURABLE_KEY.BLOCKLET_APP_SPACE_ENDPOINT)?.value||null;async function b(t){const e={};return e.did=h(t),e.name=await w(t),e.url=y(t),e.endpoint=t,e}async function k(t,e={timeout:3e4,withSearchParams:!0}){if(!l(t))throw new Error(`url(${t}) must be a string`);const a=t.startsWith("http")?t:`https://${t}`,r=new URL(a),{data:n}=await c.get(s(r.origin,"/__blocklet__.js?type=json"),{timeout:e.timeout}),o=n.componentMountPoints.findLast(d=>d.did==="z8iZnaYxnkMD5AKRjTKiCb8pQr1ut8UantAcf")?.mountPoint;if(!o)throw new Error(`MountPoint(${o}) not found`);const p=s(r.origin,o);return e.withSearchParams&&r.searchParams.get("spaceDid")?f(p,{spaceDid:r.searchParams.get("spaceDid")}):p}export{L as a,A as b,b as c,w as d,h as e,U as f,k as g,P as h,_ as i,C as j};
