import{_ as e,d as t,g as n,m as r}from"./lz-string-CMojKG3P.js";import{$ as i,At as a,Gt as o,Q as s,Wt as c,Yt as l,in as u,kt as d}from"./src-DTHkTkyy-Ce1TZuRw.js";import{s as f}from"./esm-CIp61Ze4.js";import{x as p}from"./connect-BSkeWkEI-BYzoVZOI.js";import{d as m,h,m as g,s as _}from"./module-D-kY811l.js";import{t as v}from"./reactor-Nm3jOev1-Cl_iEnvO.js";import{t as y}from"./pglite-seed-BtXWFpHf-BZ6di5-I.js";import{useCallback as b,useMemo as x,useSyncExternalStore as S}from"react";import{jsx as C}from"react/jsx-runtime";async function w(e){await new Promise(t=>e.block(()=>t()))}async function T(e){await new Promise((t,n)=>{e.Module.FS.syncfs(!1,e=>e?n(e):t())})}var E=[`documentType`,`documentId`,`scope`,`branch`,`index`,`skip`];function D(){let t=a(),n=c(),r=l(),i=r?.reactorModule?.reactor,o=r?.reactorModule?.queue,s=b(async()=>{if(!t)return[];let n=await f`
      SELECT
        c.table_name,
        c.column_name,
        c.data_type,
        c.is_nullable
      FROM information_schema.columns c
      INNER JOIN information_schema.tables t
        ON c.table_name = t.table_name
        AND c.table_schema = t.table_schema
      WHERE c.table_schema = ${e}
        AND t.table_type = 'BASE TABLE'
      ORDER BY c.table_name, c.ordinal_position
    `.execute(t),r=new Map;for(let e of n.rows)r.has(e.table_name)||r.set(e.table_name,[]),r.get(e.table_name).push({name:e.column_name,dataType:e.data_type,isNullable:e.is_nullable===`YES`});return Array.from(r).map(([e,t])=>{let n=t.map(e=>e.name);return{name:e,columns:[...E.filter(e=>n.includes(e)).map(e=>t.find(t=>t.name===e)),...t.filter(e=>!E.includes(e.name))]}})},[t]),u=b(async(n,r)=>{if(!t)return{columns:[],rows:[],total:null};let i=r.limit,a=r.offset,o=r.sort,s=r.filters,c=f.raw(`${e}."${n}"`),l=f``;if(s?.clauses&&s.clauses.length>0){let e=[];for(let t=0;t<s.clauses.length;t++){let n=s.clauses[t];if(!n)continue;let r=f.raw(`"${n.column}"`),i;if(n.operator===`IS NULL`)i=f`${r} IS NULL`;else if(n.operator===`IS NOT NULL`)i=f`${r} IS NOT NULL`;else if(n.operator===`LIKE`||n.operator===`ILIKE`){let e=n.value;i=f`${r} ${n.operator===`LIKE`?f`LIKE`:f`ILIKE`} ${e}`}else{let e=f.raw(n.operator),t=n.value,a=t;t!==``&&!isNaN(Number(t))&&t.trim()!==``&&(a=Number(t)),i=f`${r} ${e} ${a}`}if(e.push(i),t<s.clauses.length-1){let n=s.connectors?.[t]??`AND`;e.push(f.raw(n))}}e.length>0&&(l=f`WHERE ${f.join(e,f` `)}`)}let u;if(o){let e=f.raw(`"${o.column}"`),t=o.direction===`desc`?f`DESC`:f`ASC`;u=f`
          SELECT * FROM ${c}
          ${l}
          ORDER BY ${e} ${t}
          LIMIT ${i} OFFSET ${a}
        `}else u=f`
          SELECT * FROM ${c}
          ${l}
          LIMIT ${i} OFFSET ${a}
        `;let d=await u.execute(t),p=await f`
        SELECT COUNT(*) as count FROM ${c}
        ${l}
      `.execute(t),m=p.rows[0]?parseInt(p.rows[0].count,10):null,h=d.rows.length>0?Object.keys(d.rows[0]):[];return{columns:[...E.filter(e=>h.includes(e)),...h.filter(e=>!E.includes(e))],rows:d.rows,total:m}},[t]),d=b(async()=>{if(n){o&&await w(o);try{let e=await(await(await m(g(_()??null)))({pg:n})).text(),t=new Blob([e],{type:`text/sql`}),r=URL.createObjectURL(t),i=document.createElement(`a`);i.href=r,i.download=`database-export-${Date.now()}.sql`,i.click(),URL.revokeObjectURL(r)}finally{o?.unblock()}}},[n,o]),p=b(async t=>{n&&(o&&await w(o),i&&await i.kill().completed,await n.transaction(async n=>{let r=await n.query(`SELECT nspname FROM pg_namespace
           WHERE nspname NOT IN ('pg_catalog', 'pg_toast', 'information_schema', 'public')
             AND nspname NOT LIKE 'pg_temp_%'
             AND nspname NOT LIKE 'pg_toast_temp_%'`);for(let{nspname:e}of r.rows)await n.exec(`DROP SCHEMA IF EXISTS "${e}" CASCADE`);try{await n.exec(`SET standard_conforming_strings = off;`)}catch{}await n.exec(t),await n.exec(`SET search_path TO ${e}`)}),await T(n),window.location.reload(),await new Promise(()=>{}))},[n,o,i]);return{getTables:s,getTableRows:u,getDefaultSort:b(e=>{if(e===`Operation`)return{column:`timestampUtcMs`,direction:`desc`}},[]),onExportDb:d,onImportDb:p}}var O=[16,17];function k(){let e=S(h,_,()=>void 0),t=b(async e=>{console.info(`[debug-inspector] Reset requested: PG${e}`);try{localStorage.setItem(y,String(e)),console.info(`[debug-inspector] Clearing reactor storage...`),await v(),console.info(`[debug-inspector] Storage cleared. Reloading...`),window.location.reload()}catch(e){throw console.error(`[debug-inspector] Reset failed:`,e),localStorage.removeItem(y),e}},[]);return{supportedPgVersions:O,currentPgVersion:e??null,onResetToPgVersion:t}}function A(){let e=l()?.reactorModule,n=x(()=>{if(e)return new t(e.keyframeStore,e.operationStore,e.writeCache,e.documentView,e.documentModelRegistry)},[e]),r=b(async(e,t)=>{if(!n)throw Error(`Reactor module not available`);return n.validateDocument(e,t)},[n]),i=b(async(e,t)=>{if(!n)throw Error(`Reactor module not available`);return n.rebuildKeyframes(e,t)},[n]),a=b(async(e,t)=>{if(!n)throw Error(`Reactor module not available`);return n.rebuildSnapshots(e,t)},[n]);if(n)return{onValidate:r,onRebuildKeyframes:i,onRebuildSnapshots:a}}function j(){let e=l()?.reactorModule?.processorManager,t=x(()=>e!=null,[e]),n=b(async()=>e?e.getAll().map(e=>({processorId:e.processorId,factoryId:e.factoryId,driveId:e.driveId,processorIndex:e.processorIndex,lastOrdinal:e.lastOrdinal,status:e.status,lastError:e.lastError,lastErrorTimestamp:e.lastErrorTimestamp})):[],[e]),r=b(async t=>{if(!e)return;let n=e.get(t);n&&await n.retry()},[e]);if(t)return{getProcessors:n,onRetry:r}}function M(){let e=l()?.reactorModule?.queue,t=x(()=>{if(e instanceof r)return e},[e]),n=b(async()=>{if(!t)return{isPaused:!1,pendingJobs:[],executingJobs:[],totalPending:0,totalExecuting:0};let e=t.getPendingJobs(),n=t.getExecutingJobIds(),r=[];for(let e of n.values())for(let n of e){let e=t.getJob(n);e&&r.push(e)}return{isPaused:t.paused,pendingJobs:e,executingJobs:r,totalPending:e.length,totalExecuting:r.length}},[t]),i=b(async()=>{t&&t.pause()},[t]),a=b(async()=>{t&&await t.resume()},[t]);if(t)return{getQueueState:n,onPause:i,onResume:a}}function N(){let e=u();if(!e)throw Error(`Sync manager not found`);let t=d();return{getRemotes:b(()=>Promise.resolve(e.list()),[e]),removeRemote:b(t=>e.remove(t),[e]),addRemoteManual:b(async e=>{await s(e,void 0,{pollBehavior:n.Manual})},[]),triggerPull:b(t=>e.triggerPull(t),[e]),connectionStates:t}}var P=25,F=()=>{let t=o()?.type===`inspector`,{getTables:n,getTableRows:r,getDefaultSort:a,onExportDb:s,onImportDb:c}=D(),{getRemotes:l,removeRemote:u,addRemoteManual:d,triggerPull:f,connectionStates:m}=N(),h=M(),g=j(),_=A(),{currentPgVersion:v,supportedPgVersions:y,onResetToPgVersion:b}=k();return C(p,{open:t,onOpenChange:e=>{e||i()},dbExplorerProps:{schema:e,getTables:n,getTableRows:r,getDefaultSort:a,pageSize:P,onExportDb:s,onImportDb:c,pgVersionControl:{currentPgVersion:v,supportedPgVersions:y,onResetToPgVersion:b}},remotesInspectorProps:{getRemotes:l,removeRemote:u,addRemoteManual:d,triggerPull:f,connectionStates:m},queueInspectorProps:h,processorsInspectorProps:g,integrityInspectorProps:_})};export{F as InspectorModal};
//# sourceMappingURL=InspectorModal-EDrjiXPs-CkN_9VHS.js.map