import{X as m,Y as v,Z as k,r as a,_ as I,j as e,$ as D,a0 as R,a1 as P}from"./index-CULuikBf.js";import{y as g,r as $,S as E,D as M,a as L,b as O,c as _,d as V,e as z,f as B,g as q,h as A,i as W,j as X,k as Y,l as Z,m as F}from"./ActionsDropdown-q-9MDOpc.js";import{a6 as G,a7 as H,a4 as J}from"./getPseudoElementBounds-BYLHjZjA.js";import{B as K}from"./Breadcrumbs--TWNuJCy.js";function te(){const l=m.useParams(),i=m.useSearch(),c=v(),d=k(),b=a.useRef(i.table),n=a.useRef(null),[r,h]=a.useState(i.table),[s,u]=a.useState(!1),[o,f]=a.useState(null),x=a.useMemo(()=>new I(l.databaseId),[l.databaseId]),y=a.useMemo(()=>({databaseId:l.databaseId,type:"d1"}),[l.databaseId]),S=a.useCallback(t=>{h(t),b.current!==t&&(b.current=t,c({replace:!0,search:{table:t},to:"."}))},[c]),p=a.useCallback(async()=>{var t;u(!0);try{await Promise.all([d.invalidate(),(t=n.current)==null?void 0:t.refreshSchema(),new Promise(w=>setTimeout(w,250))])}finally{u(!1)}},[d]),j=a.useCallback(async()=>{await p(),c({replace:!0,search:{table:void 0},to:"."})},[p,c]),T=a.useCallback(()=>{r&&f({schemaName:"main",tableName:r})},[r]),N=a.useCallback(()=>{f(null)},[]),C=a.useCallback(()=>{var t;o&&((t=n.current)==null||t.closeTableTabs(o.schemaName,o.tableName),j())},[o,j]);return e.jsxs("div",{className:"flex flex-col h-full",children:[e.jsxs(K,{icon:D,title:"D1",items:[e.jsx(R,{className:"flex items-center gap-1.5",params:{databaseId:l.databaseId},search:{table:void 0},to:"/d1/$databaseId",children:l.databaseId},"database-id"),e.jsx(Q,{studioRef:n},"table-selector")],children:[e.jsx("div",{className:"flex-1"}),e.jsx(g,{"aria-label":"Refresh tables",className:"disabled:cursor-progress",disabled:s,onClick:p,shape:"square",children:e.jsx($,{className:s?"animate-spin":void 0,size:14})}),e.jsx(E,{currentTable:r,driver:x}),e.jsx(g,{"aria-label":"Edit table schema",disabled:!r,icon:G,onClick:()=>{var t;r&&((t=n.current)==null||t.openEditTableTab("main",r))},children:"Edit Schema"}),e.jsx(g,{"aria-label":"Delete table",disabled:!r,icon:H,onClick:T,variant:"secondary-destructive",children:"Delete Table"})]}),o&&e.jsx(M,{closeModal:N,driver:x,isOpen:!0,onSuccess:C,schemaName:o.schemaName,tableName:o.tableName}),e.jsx("div",{className:"flex-1 overflow-hidden",children:e.jsx(L,{driver:x,initialTable:i.table,onTableChange:S,ref:n,resource:y},l.databaseId)})]})}function Q({studioRef:l}){const i=m.useLoaderData(),c=v(),d=m.useSearch(),[b,n]=a.useState(!1),r=a.useCallback(s=>{s!==null&&c({search:{table:s},to:"."})},[c]),h=a.useCallback(()=>{var s;n(!1),(s=l.current)==null||s.openCreateTableTab()},[l]);return e.jsxs(O,{onOpenChange:n,onValueChange:r,open:b,value:d.table??null,children:[e.jsxs(_,{className:"inline-flex items-center gap-1 p-2 -mx-1.5 rounded-md bg-transparent text-sm text-text cursor-pointer border-none transition-colors hover:bg-border/50 data-popup-open:bg-border/50",children:[e.jsx(V,{placeholder:"Select table"}),e.jsx(z,{children:e.jsx(B,{className:"w-3.5 h-3.5 text-text-secondary"})})]}),e.jsx(q,{children:e.jsx(A,{align:"start",alignItemWithTrigger:!1,side:"bottom",sideOffset:4,children:e.jsxs(W,{className:"min-w-36 max-h-72 bg-bg border border-border rounded-lg shadow-[0_4px_12px_rgba(0,0,0,0.15)] z-100 overflow-hidden transition-[opacity,transform] duration-150 data-starting-style:opacity-0 data-starting-style:-translate-y-1 data-ending-style:opacity-0 data-ending-style:-translate-y-1",children:[e.jsx("div",{className:"p-1",children:e.jsxs("button",{className:"flex items-center gap-2 w-full py-1.5 px-2 rounded-md text-sm text-text cursor-pointer transition-colors select-none outline-none hover:bg-bg-secondary dark:hover:bg-bg-tertiary",onClick:h,type:"button",children:[e.jsx("span",{className:"flex items-center w-4",children:e.jsx(X,{className:"w-3.5 h-3.5"})}),"Create table"]})}),e.jsx("div",{className:"mx-1 border-t border-border"}),e.jsx(Y,{className:"p-1",children:i.tables.length>0?i.tables.map(s=>{const u=d.table===s.value?J:P;return e.jsxs(Z,{className:"flex items-center gap-2 w-full py-1.5 px-2 rounded-md text-sm text-text cursor-pointer transition-colors select-none outline-none data-highlighted:bg-bg-secondary dark:data-highlighted:bg-bg-tertiary",value:s.value,children:[e.jsx("span",{className:"flex items-center w-4",children:e.jsx(u,{className:"w-3.5 h-3.5"})}),e.jsx(F,{children:s.label})]},s.value)}):e.jsx("span",{className:"flex justify-center items-center gap-2 w-full py-1.5 px-2 text-sm text-text-secondary",children:"No tables"})})]})})})]},"table-select")}export{te as component};
