import{a as e,i as t,n,r,t as i}from"./TableSelect-DD9asiuX.js";import{t as a}from"./Pencil.es-CzCcQnsk.js";import{n as o}from"./dialog-oqh8l3l3zutpibxx-CQxG7a37.js";import{t as s}from"./Breadcrumbs-CaGSlxI5.js";import{Gn as c,Hn as l,Jn as u,Kn as d,Tt as f,Un as p,Wn as m,c as h,l as g,nt as _,st as v,zn as y}from"./index-DUQaQqLX.js";var b=u(c()),x=d(),S=y(`__root__`);function C(){let c=h.useParams(),u=h.useLoaderData(),d=h.useSearch(),y=l(),C=m(),w=S.useLoaderData(),T=p(),E=(0,b.useRef)(d.table),D=(0,b.useRef)(null),[O,k]=(0,b.useState)(d.table),[A,j]=(0,b.useState)(!1),[M,N]=(0,b.useState)(null),P=(0,b.useMemo)(()=>new g(c.databaseId),[c.databaseId]),F=(0,b.useMemo)(()=>_(w.workers,T.location.searchStr)?.bindings?.d1?.find(e=>e.id===c.databaseId)?.bindingName,[w.workers,T.location.searchStr,c.databaseId]),I=(0,b.useMemo)(()=>({databaseId:c.databaseId,type:`d1`}),[c.databaseId]),L=(0,b.useCallback)(e=>{k(e),E.current!==e&&(E.current=e,y({replace:!0,search:t=>({...t,table:e}),to:`.`}))},[y]),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(),y({replace:!0,search:e=>({...e,table:void 0}),to:`.`})},[R,y]),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)(s,{icon:v,title:`D1`,items:[(0,x.jsx)(`span`,{className:`flex items-center gap-1.5`,children:F&&F!==c.databaseId?(0,x.jsxs)(x.Fragment,{children:[F,(0,x.jsxs)(`span`,{className:`text-kumo-subtle`,children:[`(`,c.databaseId,`)`]})]}):c.databaseId},`database-id`),(0,x.jsx)(i,{selectedTable:d.table,studioRef:D,tables:u.tables},`table-selector`)],children:[(0,x.jsx)(`div`,{className:`flex-1`}),(0,x.jsx)(f,{"aria-label":`Refresh tables`,className:`disabled:cursor-progress`,disabled:A,onClick:R,shape:`square`,children:(0,x.jsx)(e,{className:A?`animate-spin`:void 0,size:14})}),(0,x.jsx)(n,{currentTable:O,driver:P}),(0,x.jsx)(f,{"aria-label":`Edit table schema`,disabled:!O,icon:a,onClick:()=>{O&&D.current?.openEditTableTab(`main`,O)},children:`Edit Schema`}),(0,x.jsx)(f,{"aria-label":`Delete table`,disabled:!O,icon:o,onClick:B,variant:`secondary-destructive`,children:`Delete Table`})]}),M&&(0,x.jsx)(r,{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)(t,{driver:P,initialTable:d.table,onTableChange:L,ref:D,resource:I},c.databaseId)})]})}export{C as component};