import{c as e,n as t,s as n,t as r,z as i}from"./useRouterState-DyVgzXuA.js";import{t as a}from"./button-cdxnqcgzwko8ooha-DePtDphh.js";import{o,s}from"./preload-helper-DVdLIjcg.js";import{a as c,i as l,n as u,r as d,t as f}from"./TableSelect-CfhTton7.js";import"./api-CJ3Zq7MY.js";import{t as p}from"./Pencil.es-DFYQ2b5f.js";import"./sqlite-Cw873T5t.js";import{n as m}from"./dialog-oqh8l3l3zutpibxx-C3V2uifQ.js";import{t as h}from"./Breadcrumbs-PAPh5eEw.js";import{c as g,l as _,p as v,v as y}from"./index-DO0z1AkS.js";var b=i(n()),x=e(),S=o(`__root__`);function C(){let e=g.useParams(),n=g.useLoaderData(),i=g.useSearch(),o=s(),C=t(),w=S.useLoaderData(),T=r(),E=(0,b.useRef)(i.table),D=(0,b.useRef)(null),[O,k]=(0,b.useState)(i.table),[A,j]=(0,b.useState)(!1),[M,N]=(0,b.useState)(null),P=(0,b.useMemo)(()=>new _(e.databaseId),[e.databaseId]),F=(0,b.useMemo)(()=>v(w.workers,T.location.searchStr)?.bindings?.d1?.find(t=>t.id===e.databaseId)?.bindingName,[w.workers,T.location.searchStr,e.databaseId]),I=(0,b.useMemo)(()=>({databaseId:e.databaseId,type:`d1`}),[e.databaseId]),L=(0,b.useCallback)(e=>{k(e),E.current!==e&&(E.current=e,o({replace:!0,search:t=>({...t,table:e}),to:`.`}))},[o]),R=(0,b.useCallback)(async()=>{j(!0);try{await Promise.all([C.invalidate(),D.current?.refreshSchema(),new Promise(e=>setTimeout(e,250))])}finally{j(!1)}},[C]),z=(0,b.useCallback)(async()=>{await R(),o({replace:!0,search:e=>({...e,table:void 0}),to:`.`})},[R,o]),B=(0,b.useCallback)(()=>{O&&N({schemaName:`main`,tableName:O})},[O]),V=(0,b.useCallback)(()=>{N(null)},[]),H=(0,b.useCallback)(()=>{M&&(D.current?.closeTableTabs(M.schemaName,M.tableName),z())},[M,z]);return(0,x.jsxs)(`div`,{className:`flex h-full flex-col`,children:[(0,x.jsxs)(h,{icon:y,title:`D1`,items:[(0,x.jsx)(`span`,{className:`flex items-center gap-1.5`,children:F&&F!==e.databaseId?(0,x.jsxs)(x.Fragment,{children:[F,(0,x.jsxs)(`span`,{className:`text-kumo-subtle`,children:[`(`,e.databaseId,`)`]})]}):e.databaseId},`database-id`),(0,x.jsx)(f,{selectedTable:i.table,studioRef:D,tables:n.tables},`table-selector`)],children:[(0,x.jsx)(`div`,{className:`flex-1`}),(0,x.jsx)(a,{"aria-label":`Refresh tables`,className:`disabled:cursor-progress`,disabled:A,onClick:R,shape:`square`,children:(0,x.jsx)(c,{className:A?`animate-spin`:void 0,size:14})}),(0,x.jsx)(u,{currentTable:O,driver:P}),(0,x.jsx)(a,{"aria-label":`Edit table schema`,disabled:!O,icon:p,onClick:()=>{O&&D.current?.openEditTableTab(`main`,O)},children:`Edit Schema`}),(0,x.jsx)(a,{"aria-label":`Delete table`,disabled:!O,icon:m,onClick:B,variant:`secondary-destructive`,children:`Delete Table`})]}),M&&(0,x.jsx)(d,{closeModal:V,driver:P,isOpen:!0,onSuccess:H,schemaName:M.schemaName,tableName:M.tableName}),(0,x.jsx)(`div`,{className:`flex-1 overflow-hidden bg-kumo-elevated`,children:(0,x.jsx)(l,{driver:P,initialTable:i.table,onTableChange:L,ref:D,resource:I},e.databaseId)})]})}export{C as component};