import{r as e,t}from"./jsx-runtime-DAs1UGHr.js";import{i as n,l as r,r as i}from"./matchContext-DoIVbOYX.js";import{Ot as a,Rt as o,Vt as s}from"./preload-helper-D7v3KC5t.js";import{i as c,n as l,r as u,t as d}from"./TableSelect-BhR_4hPl.js";import{i as f}from"./input-group-COo-wz5O-CICcurki.js";import{t as p}from"./Breadcrumbs-XAqvoXsj.js";import{t as m}from"./Pencil.es-uQesYP5q.js";import"./sqlite-DV-SZb4q.js";import{n as h}from"./dialog-CpCeOqSZ-C9oynb3Y.js";import{c as g,d as _,g as v,l as y}from"./index-DJyJEBBo.js";var b=e(r()),x=t(),S=o(`__root__`);function C(){let e=g.useParams(),t=g.useLoaderData(),r=g.useSearch(),o=s(),C=n(),w=S.useLoaderData(),T=i(),E=(0,b.useRef)(r.table),D=(0,b.useRef)(null),[O,k]=(0,b.useState)(r.table),[A,j]=(0,b.useState)(!1),[M,N]=(0,b.useState)(null),P=(0,b.useMemo)(()=>new y(e.databaseId),[e.databaseId]),F=(0,b.useMemo)(()=>_(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)(p,{icon:v,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)(d,{selectedTable:r.table,studioRef:D,tables:t.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)(f,{className:A?`animate-spin`:void 0,size:14})}),(0,x.jsx)(l,{currentTable:O,driver:P}),(0,x.jsx)(a,{"aria-label":`Edit table schema`,disabled:!O,icon:m,onClick:()=>{O&&D.current?.openEditTableTab(`main`,O)},children:`Edit Schema`}),(0,x.jsx)(a,{"aria-label":`Delete table`,disabled:!O,icon:h,onClick:B,variant:`secondary-destructive`,children:`Delete Table`})]}),M&&(0,x.jsx)(u,{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)(c,{driver:P,initialTable:r.table,onTableChange:L,ref:D,resource:I},e.databaseId)})]})}export{C as component};