const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["assets/TableSchemaView-C0NFMaU8.js","assets/vendor-codemirror-D5amxuPd.js","assets/vendor-ui-BLV2MnSs.js","assets/vendor-ag-grid-NsbaVf01.js","assets/ExportDialog-CysjZ1zB.js"])))=>i.map(i=>d[i]);
import{j as e,s as Ue,R as Qe,o as He,M as ke,P as mt,k as ft}from"./vendor-codemirror-D5amxuPd.js";import{r as i,g as ht,R as We,c as ue,X as ge,C as Je,a as pt,L as re,b as pe,D as Ve,W as gt,E as bt,T as oe,S as vt,d as $e,e as be,U as le,f as _e,h as Ke,i as Ne,j as Ge,k as ze,l as jt,P as yt,m as wt,n as St,o as Nt,p as Ye,q as Ct,B as kt,s as Et,H as Tt,F as $t,t as _t}from"./vendor-ui-BLV2MnSs.js";import{r as Xe,t as Dt,A as It}from"./vendor-ag-grid-NsbaVf01.js";(function(){const a=document.createElement("link").relList;if(a&&a.supports&&a.supports("modulepreload"))return;for(const n of document.querySelectorAll('link[rel="modulepreload"]'))r(n);new MutationObserver(n=>{for(const o of n)if(o.type==="childList")for(const l of o.addedNodes)l.tagName==="LINK"&&l.rel==="modulepreload"&&r(l)}).observe(document,{childList:!0,subtree:!0});function s(n){const o={};return n.integrity&&(o.integrity=n.integrity),n.referrerPolicy&&(o.referrerPolicy=n.referrerPolicy),n.crossOrigin==="use-credentials"?o.credentials="include":n.crossOrigin==="anonymous"?o.credentials="omit":o.credentials="same-origin",o}function r(n){if(n.ep)return;n.ep=!0;const o=s(n);fetch(n.href,o)}})();var Ee={},Le=Xe;Ee.createRoot=Le.createRoot,Ee.hydrateRoot=Le.hydrateRoot;const Rt={},Pe=t=>{let a;const s=new Set,r=(x,g)=>{const j=typeof x=="function"?x(a):x;if(!Object.is(j,a)){const y=a;a=g??(typeof j!="object"||j===null)?j:Object.assign({},a,j),s.forEach(S=>S(a,y))}},n=()=>a,d={setState:r,getState:n,getInitialState:()=>u,subscribe:x=>(s.add(x),()=>s.delete(x)),destroy:()=>{(Rt?"production":void 0)!=="production"&&console.warn("[DEPRECATED] The `destroy` method will be unsupported in a future version. Instead use unsubscribe function returned by subscribe. Everything will be garbage-collected if store is garbage-collected."),s.clear()}},u=a=t(r,n,d);return d},zt=t=>t?Pe(t):Pe;var Ze={exports:{}},et={},tt={exports:{}},st={};/**
 * @license React
 * use-sync-external-store-shim.production.js
 *
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */var ie=i;function Lt(t,a){return t===a&&(t!==0||1/t===1/a)||t!==t&&a!==a}var Pt=typeof Object.is=="function"?Object.is:Lt,Ot=ie.useState,Mt=ie.useEffect,Ft=ie.useLayoutEffect,qt=ie.useDebugValue;function At(t,a){var s=a(),r=Ot({inst:{value:s,getSnapshot:a}}),n=r[0].inst,o=r[1];return Ft(function(){n.value=s,n.getSnapshot=a,Ce(n)&&o({inst:n})},[t,s,a]),Mt(function(){return Ce(n)&&o({inst:n}),t(function(){Ce(n)&&o({inst:n})})},[t]),qt(s),s}function Ce(t){var a=t.getSnapshot;t=t.value;try{var s=a();return!Pt(t,s)}catch{return!0}}function Bt(t,a){return a()}var Ut=typeof window>"u"||typeof window.document>"u"||typeof window.document.createElement>"u"?Bt:At;st.useSyncExternalStore=ie.useSyncExternalStore!==void 0?ie.useSyncExternalStore:Ut;tt.exports=st;var Qt=tt.exports;/**
 * @license React
 * use-sync-external-store-shim/with-selector.production.js
 *
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */var ve=i,Ht=Qt;function Wt(t,a){return t===a&&(t!==0||1/t===1/a)||t!==t&&a!==a}var Jt=typeof Object.is=="function"?Object.is:Wt,Vt=Ht.useSyncExternalStore,Kt=ve.useRef,Gt=ve.useEffect,Yt=ve.useMemo,Xt=ve.useDebugValue;et.useSyncExternalStoreWithSelector=function(t,a,s,r,n){var o=Kt(null);if(o.current===null){var l={hasValue:!1,value:null};o.current=l}else l=o.current;o=Yt(function(){function d(y){if(!u){if(u=!0,x=y,y=r(y),n!==void 0&&l.hasValue){var S=l.value;if(n(S,y))return g=S}return g=y}if(S=g,Jt(x,y))return S;var m=r(y);return n!==void 0&&n(S,m)?(x=y,S):(x=y,g=m)}var u=!1,x,g,j=s===void 0?null:s;return[function(){return d(a())},j===null?void 0:function(){return d(j())}]},[a,s,r,n]);var c=Vt(t,o[0],o[1]);return Gt(function(){l.hasValue=!0,l.value=c},[c]),Xt(c),c};Ze.exports=et;var Zt=Ze.exports;const es=ht(Zt),at={},{useDebugValue:ts}=We,{useSyncExternalStoreWithSelector:ss}=es;let Oe=!1;const as=t=>t;function ns(t,a=as,s){(at?"production":void 0)!=="production"&&s&&!Oe&&(console.warn("[DEPRECATED] Use `createWithEqualityFn` instead of `create` or use `useStoreWithEqualityFn` instead of `useStore`. They can be imported from 'zustand/traditional'. https://github.com/pmndrs/zustand/discussions/1937"),Oe=!0);const r=ss(t.subscribe,t.getState,t.getServerState||t.getInitialState,a,s);return ts(r),r}const Me=t=>{(at?"production":void 0)!=="production"&&typeof t!="function"&&console.warn("[DEPRECATED] Passing a vanilla store will be unsupported in a future version. Instead use `import { useStore } from 'zustand'`.");const a=typeof t=="function"?zt(t):t,s=(r,n)=>ns(a,r,n);return Object.assign(s,a),s},je=t=>t?Me(t):Me,rs={};function os(t,a){let s;try{s=t()}catch{return}return{getItem:n=>{var o;const l=d=>d===null?null:JSON.parse(d,void 0),c=(o=s.getItem(n))!=null?o:null;return c instanceof Promise?c.then(l):l(c)},setItem:(n,o)=>s.setItem(n,JSON.stringify(o,void 0)),removeItem:n=>s.removeItem(n)}}const ce=t=>a=>{try{const s=t(a);return s instanceof Promise?s:{then(r){return ce(r)(s)},catch(r){return this}}}catch(s){return{then(r){return this},catch(r){return ce(r)(s)}}}},ls=(t,a)=>(s,r,n)=>{let o={getStorage:()=>localStorage,serialize:JSON.stringify,deserialize:JSON.parse,partialize:b=>b,version:0,merge:(b,C)=>({...C,...b}),...a},l=!1;const c=new Set,d=new Set;let u;try{u=o.getStorage()}catch{}if(!u)return t((...b)=>{console.warn(`[zustand persist middleware] Unable to update item '${o.name}', the given storage is currently unavailable.`),s(...b)},r,n);const x=ce(o.serialize),g=()=>{const b=o.partialize({...r()});let C;const v=x({state:b,version:o.version}).then(I=>u.setItem(o.name,I)).catch(I=>{C=I});if(C)throw C;return v},j=n.setState;n.setState=(b,C)=>{j(b,C),g()};const y=t((...b)=>{s(...b),g()},r,n);let S;const m=()=>{var b;if(!u)return;l=!1,c.forEach(v=>v(r()));const C=((b=o.onRehydrateStorage)==null?void 0:b.call(o,r()))||void 0;return ce(u.getItem.bind(u))(o.name).then(v=>{if(v)return o.deserialize(v)}).then(v=>{if(v)if(typeof v.version=="number"&&v.version!==o.version){if(o.migrate)return o.migrate(v.state,v.version);console.error("State loaded from storage couldn't be migrated since no migrate function was provided")}else return v.state}).then(v=>{var I;return S=o.merge(v,(I=r())!=null?I:y),s(S,!0),g()}).then(()=>{C==null||C(S,void 0),l=!0,d.forEach(v=>v(S))}).catch(v=>{C==null||C(void 0,v)})};return n.persist={setOptions:b=>{o={...o,...b},b.getStorage&&(u=b.getStorage())},clearStorage:()=>{u==null||u.removeItem(o.name)},getOptions:()=>o,rehydrate:()=>m(),hasHydrated:()=>l,onHydrate:b=>(c.add(b),()=>{c.delete(b)}),onFinishHydration:b=>(d.add(b),()=>{d.delete(b)})},m(),S||y},is=(t,a)=>(s,r,n)=>{let o={storage:os(()=>localStorage),partialize:m=>m,version:0,merge:(m,b)=>({...b,...m}),...a},l=!1;const c=new Set,d=new Set;let u=o.storage;if(!u)return t((...m)=>{console.warn(`[zustand persist middleware] Unable to update item '${o.name}', the given storage is currently unavailable.`),s(...m)},r,n);const x=()=>{const m=o.partialize({...r()});return u.setItem(o.name,{state:m,version:o.version})},g=n.setState;n.setState=(m,b)=>{g(m,b),x()};const j=t((...m)=>{s(...m),x()},r,n);n.getInitialState=()=>j;let y;const S=()=>{var m,b;if(!u)return;l=!1,c.forEach(v=>{var I;return v((I=r())!=null?I:j)});const C=((b=o.onRehydrateStorage)==null?void 0:b.call(o,(m=r())!=null?m:j))||void 0;return ce(u.getItem.bind(u))(o.name).then(v=>{if(v)if(typeof v.version=="number"&&v.version!==o.version){if(o.migrate)return[!0,o.migrate(v.state,v.version)];console.error("State loaded from storage couldn't be migrated since no migrate function was provided")}else return[!1,v.state];return[!1,void 0]}).then(v=>{var I;const[R,k]=v;if(y=o.merge(k,(I=r())!=null?I:j),s(y,!0),R)return x()}).then(()=>{C==null||C(y,void 0),y=r(),l=!0,d.forEach(v=>v(y))}).catch(v=>{C==null||C(void 0,v)})};return n.persist={setOptions:m=>{o={...o,...m},m.storage&&(u=m.storage)},clearStorage:()=>{u==null||u.removeItem(o.name)},getOptions:()=>o,rehydrate:()=>S(),hasHydrated:()=>l,onHydrate:m=>(c.add(m),()=>{c.delete(m)}),onFinishHydration:m=>(d.add(m),()=>{d.delete(m)})},o.skipHydration||S(),y||j},cs=(t,a)=>"getStorage"in a||"serialize"in a||"deserialize"in a?((rs?"production":void 0)!=="production"&&console.warn("[DEPRECATED] `getStorage`, `serialize` and `deserialize` options are deprecated. Use `storage` option instead."),ls(t,a)):is(t,a),De=cs,Te="/api/v1";async function P(t,a){const s={...a==null?void 0:a.headers};a!=null&&a.body&&(s["Content-Type"]??(s["Content-Type"]="application/json"));const r=await fetch(`${Te}${t}`,{...a,headers:s});if(!r.ok){const n=await r.json().catch(()=>({detail:r.statusText}));throw new Error(n.detail||`HTTP ${r.status}`)}if(r.status!==204)return r.json()}const A={getSessions:()=>P("/sessions"),createSession:t=>P("/sessions",{method:"POST",body:JSON.stringify(t)}),getSession:t=>P(`/sessions/${t}`),updateSession:(t,a)=>P(`/sessions/${t}`,{method:"PUT",body:JSON.stringify(a)}),deleteSession:t=>P(`/sessions/${t}`,{method:"DELETE"}),testSession:t=>P(`/sessions/${t}/test`,{method:"POST"}),probeConnection:t=>P("/sessions/probe",{method:"POST",body:JSON.stringify(t)}),getDatabases:t=>P(`/sessions/${t}/databases`),getTables:(t,a)=>P(`/sessions/${t}/databases/${a}/tables`),getColumns:(t,a,s)=>P(`/sessions/${t}/databases/${a}/tables/${s}/columns`),getIndexes:(t,a,s)=>P(`/sessions/${t}/databases/${a}/tables/${s}/indexes`),getCreateSql:(t,a,s)=>P(`/sessions/${t}/databases/${a}/tables/${s}/create_sql`),executeQuery:(t,a,s,r)=>P(`/sessions/${t}/query`,{method:"POST",body:JSON.stringify({sql:a,database:s,limit:r})}),cellUpdate:(t,a)=>P(`/sessions/${t}/cell-update`,{method:"POST",body:JSON.stringify(a)}),rowInsert:(t,a)=>P(`/sessions/${t}/row-insert`,{method:"POST",body:JSON.stringify(a)}),rowDelete:(t,a)=>P(`/sessions/${t}/rows`,{method:"DELETE",body:JSON.stringify(a)}),createTable:(t,a,s)=>P(`/sessions/${t}/databases/${a}/tables`,{method:"POST",body:JSON.stringify(s)}),dropTable:(t,a,s)=>P(`/sessions/${t}/databases/${a}/tables/${s}`,{method:"DELETE"}),truncateTable:(t,a,s)=>P(`/sessions/${t}/databases/${a}/tables/${s}/truncate`,{method:"POST"}),createIndex:(t,a,s,r)=>P(`/sessions/${t}/databases/${a}/tables/${s}/indexes`,{method:"POST",body:JSON.stringify(r)}),dropIndex:(t,a,s,r)=>P(`/sessions/${t}/databases/${a}/tables/${s}/indexes/${r}`,{method:"DELETE"}),setPrimaryKey:(t,a,s,r)=>P(`/sessions/${t}/databases/${a}/tables/${s}/primary-key`,{method:"POST",body:JSON.stringify({columns:r})}),dropPrimaryKey:(t,a,s)=>P(`/sessions/${t}/databases/${a}/tables/${s}/primary-key`,{method:"DELETE"}),addColumn:(t,a,s,r)=>P(`/sessions/${t}/databases/${a}/tables/${s}/columns`,{method:"POST",body:JSON.stringify(r)}),modifyColumn:(t,a,s,r,n)=>P(`/sessions/${t}/databases/${a}/tables/${s}/columns/${r}`,{method:"PUT",body:JSON.stringify(n)}),dropColumn:(t,a,s,r)=>P(`/sessions/${t}/databases/${a}/tables/${s}/columns/${r}`,{method:"DELETE"}),exportConfig:async t=>{const a=await fetch(`${Te}/config/export`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({passphrase:t})});if(!a.ok){const d=await a.json().catch(()=>({detail:a.statusText}));throw new Error(d.detail||`HTTP ${a.status}`)}const s=await a.blob(),n=(a.headers.get("Content-Disposition")??"").match(/filename="([^"]+)"/),o=n?n[1]:"lagun_sessions.json",l=URL.createObjectURL(s),c=document.createElement("a");c.href=l,c.download=o,c.click(),URL.revokeObjectURL(l)},importConfig:async(t,a)=>{const s=new FormData;s.append("file",t),s.append("passphrase",a);const r=await fetch(`${Te}/config/import`,{method:"POST",body:s});if(!r.ok){const n=await r.json().catch(()=>({detail:r.statusText}));throw new Error(n.detail||`HTTP ${r.status}`)}return r.json()}},ee=je()(De((t,a)=>({sessions:[],activeSessionId:null,loading:!1,error:null,loadSessions:async()=>{t({loading:!0,error:null});try{const s=await A.getSessions();t({sessions:s,loading:!1})}catch(s){t({error:String(s),loading:!1})}},setActiveSession:s=>t({activeSessionId:s}),createSession:async s=>{const r=await A.createSession(s);return t(n=>({sessions:[...n.sessions,r]})),r},updateSession:async(s,r)=>{const n=await A.updateSession(s,r);return t(o=>({sessions:o.sessions.map(l=>l.id===s?n:l)})),n},deleteSession:async s=>{await A.deleteSession(s),t(r=>({sessions:r.sessions.filter(n=>n.id!==s),activeSessionId:r.activeSessionId===s?null:r.activeSessionId}))},testSession:async s=>A.testSession(s)}),{name:"lagun-session-ui",version:1,partialize:t=>({activeSessionId:t.activeSessionId})}));function ds(){var t;return((t=crypto.randomUUID)==null?void 0:t.call(crypto))??"10000000-1000-4000-8000-100000000000".replace(/[018]/g,a=>(+a^crypto.getRandomValues(new Uint8Array(1))[0]&15>>+a/4).toString(16))}function Fe(){return`tab-${ds()}`}const te=je()(De((t,a)=>({tabs:[],activeTabId:null,openQueryTab:(s,r)=>{const n=Fe(),o={id:n,label:r?`Query — ${r}`:"Query",type:"query",sessionId:s,database:r};t(l=>({tabs:[...l.tabs,o],activeTabId:n}))},openTableTab:(s,r,n)=>{const o=a().tabs.find(d=>d.type==="table"&&d.sessionId===s&&d.database===r&&d.table===n);if(o){t({activeTabId:o.id});return}const l=Fe(),c={id:l,label:n,type:"table",sessionId:s,database:r,table:n};t(d=>({tabs:[...d.tabs,c],activeTabId:l}))},closeTab:s=>{t(r=>{var l;const n=r.tabs.filter(c=>c.id!==s);let o=r.activeTabId;if(o===s){const c=r.tabs.findIndex(d=>d.id===s);o=((l=n[Math.max(0,c-1)])==null?void 0:l.id)??null}return{tabs:n,activeTabId:o}})},setActiveTab:s=>t({activeTabId:s}),setSql:(s,r)=>t(n=>({tabs:n.tabs.map(o=>o.id===s?{...o,sql:r}:o)}))}),{name:"lagun-tabs",version:2,partialize:t=>({tabs:t.tabs,activeTabId:t.activeTabId})})),B=i.forwardRef(({variant:t="secondary",size:a="md",className:s,children:r,...n},o)=>e.jsx("button",{ref:o,className:ue("inline-flex items-center justify-center gap-1.5 font-medium rounded-md transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-brand-500 disabled:opacity-50 disabled:cursor-not-allowed",{"bg-brand-600 hover:bg-brand-500 text-white":t==="primary","bg-surface-700 hover:bg-surface-600 text-slate-200 border border-surface-600":t==="secondary","hover:bg-surface-800 text-slate-400 hover:text-slate-200":t==="ghost","bg-red-700 hover:bg-red-600 text-white":t==="danger"},{"px-2 py-1 text-xs":a==="sm","px-3 py-1.5 text-sm":a==="md","px-4 py-2 text-base":a==="lg"},s),...n,children:r}));B.displayName="Button";function ye({open:t,onClose:a,title:s,children:r,footer:n,width:o="max-w-lg"}){return i.useEffect(()=>{if(!t)return;const l=c=>{c.key==="Escape"&&a()};return window.addEventListener("keydown",l),()=>window.removeEventListener("keydown",l)},[t,a]),t?e.jsxs("div",{className:"fixed inset-0 z-50 flex items-center justify-center p-4",children:[e.jsx("div",{className:"absolute inset-0 bg-black/60 backdrop-blur-sm",onClick:a}),e.jsxs("div",{className:`relative bg-surface-900 border border-surface-700 rounded-lg shadow-2xl w-full ${o} flex flex-col max-h-[90vh]`,children:[e.jsxs("div",{className:"flex items-center justify-between px-4 py-3 border-b border-surface-700",children:[e.jsx("h2",{className:"text-sm font-semibold text-slate-100",children:s}),e.jsx(B,{variant:"ghost",size:"sm",onClick:a,className:"p-1",children:e.jsx(ge,{size:14})})]}),e.jsx("div",{className:"p-4 overflow-y-auto flex-1",children:r}),n&&e.jsx("div",{className:"px-4 py-3 border-t border-surface-700 flex justify-end gap-2",children:n})]})]}):null}const V=i.forwardRef(({label:t,error:a,className:s,...r},n)=>{const o=r.id??(t?`input-${t.toLowerCase().replace(/\s+/g,"-").replace(/[^a-z0-9-]/g,"")}`:void 0);return e.jsxs("div",{className:"flex flex-col gap-1",children:[t&&e.jsx("label",{htmlFor:o,className:"text-xs font-medium text-slate-400 uppercase tracking-wide",children:t}),e.jsx("input",{ref:n,id:o,className:ue("bg-surface-800 border border-surface-700 rounded-md px-3 py-1.5 text-sm text-slate-100 placeholder-slate-500","focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-brand-500","disabled:opacity-50 disabled:cursor-not-allowed",a&&"border-red-500",s),...r}),a&&e.jsx("p",{className:"text-xs text-red-400",children:a})]})});V.displayName="Input";const qe={name:"",host:"localhost",port:"3306",username:"root",password:"",default_db:"",query_limit:"100",ssl_enabled:!1};function nt({open:t,onClose:a,session:s}){const{createSession:r,updateSession:n,testSession:o}=ee(),[l,c]=i.useState(qe),[d,u]=i.useState(!1),[x,g]=i.useState(!1),[j,y]=i.useState(null),[S,m]=i.useState([]),[b,C]=i.useState([]),[v,I]=i.useState(!1),[R,k]=i.useState(null),[h,_]=i.useState(null);i.useEffect(()=>{s?(c({name:s.name,host:s.host,port:String(s.port),username:s.username,password:"",default_db:s.default_db??"",query_limit:String(s.query_limit),ssl_enabled:s.ssl_enabled}),C(s.selected_databases??[])):(c(qe),C([])),_(null),y(null),m([]),k(null)},[s,t]);const T=(f,D)=>c(W=>({...W,[f]:D})),M=async()=>{u(!0),_(null);try{const f={name:l.name,host:l.host,port:parseInt(l.port),username:l.username,...l.password?{password:l.password}:{},default_db:l.default_db||void 0,query_limit:parseInt(l.query_limit),ssl_enabled:l.ssl_enabled,selected_databases:b};s?await n(s.id,f):await r({...f,password:l.password}),a()}catch(f){_(String(f))}finally{u(!1)}},U=async()=>{g(!0),y(null);try{let f;s?f=await o(s.id):f=await A.probeConnection({host:l.host,port:parseInt(l.port),username:l.username,password:l.password,ssl_enabled:l.ssl_enabled}),f.ok?y({ok:!0,msg:`Connected! MySQL ${f.server_version} — ${f.latency_ms}ms`}):y({ok:!1,msg:f.error??"Connection failed"})}finally{g(!1)}},F=async()=>{I(!0),k(null);try{let f;if(s?f=await o(s.id):f=await A.probeConnection({host:l.host,port:parseInt(l.port),username:l.username,password:l.password,ssl_enabled:l.ssl_enabled}),f.ok){const D=f.databases??[];m(D),C(W=>W.filter(se=>D.includes(se)))}else k(f.error??"Connection failed")}catch(f){k(String(f))}finally{I(!1)}},z=f=>{C(D=>D.includes(f)?D.filter(W=>W!==f):[...D,f])},G=()=>{C(f=>f.length===S.length?[]:[...S])};return e.jsx(ye,{open:t,onClose:a,title:s?"Edit Connection":"New Connection",footer:e.jsxs(e.Fragment,{children:[e.jsx(B,{variant:"ghost",onClick:a,children:"Cancel"}),e.jsxs(B,{variant:"secondary",onClick:U,disabled:x,children:[x?e.jsx(re,{size:12,className:"animate-spin"}):null,"Test"]}),e.jsxs(B,{variant:"primary",onClick:M,disabled:d,children:[d?e.jsx(re,{size:12,className:"animate-spin"}):null,s?"Save":"Create"]})]}),children:e.jsxs("div",{className:"flex flex-col gap-3",children:[j&&e.jsxs("div",{className:`flex items-center gap-2 text-xs px-3 py-2 rounded ${j.ok?"bg-green-900/40 text-green-300":"bg-red-900/40 text-red-300"}`,children:[j.ok?e.jsx(Je,{size:12}):e.jsx(pt,{size:12}),j.msg]}),h&&e.jsx("p",{className:"text-xs text-red-400",children:h}),e.jsx(V,{label:"Connection Name",value:l.name,onChange:f=>T("name",f.target.value),placeholder:"My Database"}),e.jsxs("div",{className:"grid grid-cols-3 gap-2",children:[e.jsx("div",{className:"col-span-2",children:e.jsx(V,{label:"Host",value:l.host,onChange:f=>T("host",f.target.value),placeholder:"localhost"})}),e.jsx(V,{label:"Port",type:"number",value:l.port,onChange:f=>T("port",f.target.value)})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsx(V,{label:"Username",value:l.username,onChange:f=>T("username",f.target.value),placeholder:"root"}),e.jsx(V,{label:"Password",type:"password",value:l.password,onChange:f=>T("password",f.target.value),placeholder:s?"(unchanged)":""})]}),e.jsx(V,{label:"Default Database (optional)",value:l.default_db,onChange:f=>T("default_db",f.target.value),placeholder:"my_db"}),e.jsx(V,{label:"Row Limit",type:"number",value:l.query_limit,onChange:f=>T("query_limit",f.target.value)}),e.jsxs("label",{className:"flex items-center gap-2 text-sm text-slate-300 cursor-pointer",children:[e.jsx("input",{type:"checkbox",checked:l.ssl_enabled,onChange:f=>T("ssl_enabled",f.target.checked),className:"rounded"}),"Enable SSL"]}),e.jsxs("div",{className:"flex flex-col gap-1.5",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"text-xs font-medium text-slate-400 uppercase tracking-wide",children:"Databases"}),e.jsxs("div",{className:"flex items-center gap-2",children:[S.length>0&&e.jsx("button",{type:"button",onClick:G,className:"text-xs text-brand-400 hover:text-brand-300",children:b.length===S.length?"Deselect all":"Select all"}),e.jsxs("button",{type:"button",onClick:F,disabled:v,title:"Fetch databases from server",className:"flex items-center gap-1 px-2 py-0.5 rounded text-xs bg-surface-700 hover:bg-surface-600 text-slate-300 disabled:opacity-50",children:[v?e.jsx(re,{size:11,className:"animate-spin"}):e.jsx(pe,{size:11}),"Fetch"]})]})]}),R&&e.jsx("p",{className:"text-xs text-red-400",children:R}),S.length===0&&!v&&!R&&e.jsx("p",{className:"text-xs text-slate-500 italic",children:"Click Fetch to load available databases from the server."}),S.length>0&&e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"max-h-40 overflow-y-auto rounded border border-surface-700 bg-surface-950 divide-y divide-surface-800",children:S.map(f=>e.jsxs("label",{className:"flex items-center gap-2 px-3 py-1.5 cursor-pointer hover:bg-surface-800",children:[e.jsx("input",{type:"checkbox",checked:b.includes(f),onChange:()=>z(f),className:"rounded"}),e.jsx(Ve,{size:11,className:"text-yellow-400 flex-shrink-0"}),e.jsx("span",{className:"text-xs text-slate-300",children:f})]},f))}),e.jsx("p",{className:"text-xs text-slate-500",children:b.length===0?"No selection — all databases will be shown":`${b.length} of ${S.length} selected`})]})]})]})})}function us({onNew:t}){const{sessions:a,activeSessionId:s,setActiveSession:r,deleteSession:n}=ee(),{openQueryTab:o}=te(),[l,c]=i.useState(null),[d,u]=i.useState(null);return a.length===0?e.jsx("div",{className:"px-3 py-4 text-center text-xs text-slate-500",children:"No connections yet."}):e.jsxs("div",{className:"py-1",children:[a.map(x=>e.jsxs("div",{className:ue("flex items-center px-3 py-1.5 cursor-pointer group transition-colors",s===x.id?"bg-brand-600/20 text-brand-300":"hover:bg-surface-800 text-slate-300"),onClick:()=>r(x.id),children:[e.jsx(gt,{size:12,className:"flex-shrink-0 mr-2 text-green-400"}),e.jsx("span",{className:"flex-1 truncate text-xs",children:x.name}),e.jsx("span",{className:"text-slate-500 text-xs mr-1",children:x.host}),e.jsxs("div",{className:"relative",children:[e.jsx("button",{onClick:g=>{g.stopPropagation(),u(d===x.id?null:x.id)},className:"opacity-0 group-hover:opacity-100 p-0.5 rounded hover:bg-surface-700 text-slate-400 hover:text-slate-200",children:e.jsx(bt,{size:12})}),d===x.id&&e.jsxs("div",{className:"absolute right-0 top-6 bg-surface-800 border border-surface-700 rounded shadow-lg z-50 py-1 w-40",onMouseLeave:()=>u(null),children:[e.jsxs("button",{className:"flex items-center gap-2 w-full px-3 py-1.5 text-xs hover:bg-surface-700 text-slate-200",onClick:g=>{g.stopPropagation(),o(x.id),u(null)},children:[e.jsx(oe,{size:12})," New Query"]}),e.jsxs("button",{className:"flex items-center gap-2 w-full px-3 py-1.5 text-xs hover:bg-surface-700 text-slate-200",onClick:g=>{g.stopPropagation(),c(x),u(null)},children:[e.jsx(vt,{size:12})," Edit"]}),e.jsxs("button",{className:"flex items-center gap-2 w-full px-3 py-1.5 text-xs hover:bg-surface-700 text-red-400",onClick:g=>{g.stopPropagation(),n(x.id),u(null)},children:[e.jsx($e,{size:12})," Delete"]})]})]})]},x.id)),l&&e.jsx(nt,{open:!!l,onClose:()=>c(null),session:l})]})}function xs({open:t,onClose:a}){const[s,r]=i.useState(""),[n,o]=i.useState(""),[l,c]=i.useState(!1),[d,u]=i.useState(null),x=()=>{r(""),o(""),u(null),a()},g=async()=>{if(u(null),!s){u("Enter a passphrase to protect the exported passwords.");return}if(s!==n){u("Passphrases do not match.");return}c(!0);try{await A.exportConfig(s),x()}catch(j){u(String(j))}finally{c(!1)}};return e.jsx(ye,{open:t,onClose:x,title:"Export Connections",width:"max-w-md",footer:e.jsxs(e.Fragment,{children:[e.jsx(B,{variant:"ghost",onClick:x,children:"Cancel"}),e.jsxs(B,{variant:"primary",onClick:g,disabled:l,children:[l?e.jsx(re,{size:12,className:"animate-spin mr-1"}):e.jsx(be,{size:12,className:"mr-1"}),"Download"]})]}),children:e.jsxs("div",{className:"flex flex-col gap-4",children:[e.jsx("p",{className:"text-xs text-slate-400",children:"Export all saved connections to an encrypted JSON file. You will need this passphrase to import the file on any Lagun instance."}),e.jsx(V,{label:"Passphrase",type:"password",value:s,onChange:j=>r(j.target.value),placeholder:"Strong passphrase",autoComplete:"new-password"}),e.jsx(V,{label:"Confirm passphrase",type:"password",value:n,onChange:j=>o(j.target.value),placeholder:"Repeat passphrase",autoComplete:"new-password",onKeyDown:j=>j.key==="Enter"&&g()}),d&&e.jsx("p",{className:"text-xs text-red-400",children:d})]})})}function ms({open:t,onClose:a}){const{loadSessions:s}=ee(),[r,n]=i.useState(null),[o,l]=i.useState(""),[c,d]=i.useState(!1),[u,x]=i.useState(null),[g,j]=i.useState(null),y=()=>{n(null),l(""),x(null),j(null),a()},S=async()=>{if(!r){j("Select a file first.");return}if(!o){j("Enter the passphrase used during export.");return}d(!0),j(null);try{const m=await A.importConfig(r,o);x(m),await s()}catch(m){j(String(m))}finally{d(!1)}};return e.jsx(ye,{open:t,onClose:y,title:"Import Connections",width:"max-w-md",footer:u?e.jsx(B,{variant:"primary",onClick:y,children:"Done"}):e.jsxs(e.Fragment,{children:[e.jsx(B,{variant:"ghost",onClick:y,children:"Cancel"}),e.jsxs(B,{variant:"primary",onClick:S,disabled:c||!r,children:[c?e.jsx(re,{size:12,className:"animate-spin mr-1"}):e.jsx(le,{size:12,className:"mr-1"}),"Import"]})]}),children:u?e.jsxs("div",{className:"flex flex-col items-center gap-3 py-4",children:[e.jsx(Je,{size:32,className:"text-green-400"}),e.jsx("p",{className:"text-sm text-slate-200 font-medium",children:"Import complete"}),e.jsxs("p",{className:"text-xs text-slate-400",children:[u.imported," connection",u.imported!==1?"s":""," imported",u.skipped>0?`, ${u.skipped} skipped`:"","."]})]}):e.jsxs("div",{className:"flex flex-col gap-4",children:[e.jsx("p",{className:"text-xs text-slate-400",children:"Select a Lagun export file (.json) to restore saved connections."}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs font-medium text-slate-400 uppercase tracking-wide block mb-1",children:"Export file"}),e.jsx("input",{type:"file",accept:".json,application/json",onChange:m=>{var b;n(((b=m.target.files)==null?void 0:b[0])??null),j(null)},className:`text-sm text-slate-300 file:mr-3 file:py-1 file:px-3 file:rounded file:border-0
                         file:text-xs file:font-medium file:bg-surface-700 file:text-slate-200
                         hover:file:bg-surface-600 cursor-pointer`})]}),e.jsx(V,{label:"Passphrase",type:"password",value:o,onChange:m=>l(m.target.value),placeholder:"Passphrase used during export",autoComplete:"current-password",onKeyDown:m=>m.key==="Enter"&&S()}),g&&e.jsx("p",{className:"text-xs text-red-400",children:g})]})})}const me={},fe={},he={},de=je((t,a)=>({databases:{},tables:{},columns:{},loadingDbs:new Set,loadingTables:new Set,loadDatabases:async s=>{const{databases:r}=a();if(r[s])return r[s];if(me[s])return me[s];t(o=>({loadingDbs:new Set([...o.loadingDbs,s])}));const n=A.getDatabases(s).then(o=>(t(l=>({databases:{...l.databases,[s]:o},loadingDbs:new Set([...l.loadingDbs].filter(c=>c!==s))})),o)).catch(()=>(t(o=>({loadingDbs:new Set([...o.loadingDbs].filter(l=>l!==s))})),[])).finally(()=>{delete me[s]});return me[s]=n,n},loadTables:async(s,r)=>{const n=`${s}/${r}`,{tables:o}=a();if(o[n])return o[n];if(fe[n])return fe[n];t(c=>({loadingTables:new Set([...c.loadingTables,n])}));const l=A.getTables(s,r).then(c=>(t(d=>({tables:{...d.tables,[n]:c},loadingTables:new Set([...d.loadingTables].filter(u=>u!==n))})),c)).catch(()=>(t(c=>({loadingTables:new Set([...c.loadingTables].filter(d=>d!==n))})),[])).finally(()=>{delete fe[n]});return fe[n]=l,l},loadColumns:async(s,r,n)=>{const o=`${s}/${r}/${n}`,{columns:l}=a();if(l[o])return l[o];if(he[o])return he[o];const c=A.getColumns(s,r,n).then(d=>(t(u=>({columns:{...u.columns,[o]:d}})),d)).catch(()=>[]).finally(()=>{delete he[o]});return he[o]=c,c},invalidateSession:s=>{t(r=>{const n={...r.databases};delete n[s];const o=Object.fromEntries(Object.entries(r.tables).filter(([c])=>!c.startsWith(`${s}/`))),l=Object.fromEntries(Object.entries(r.columns).filter(([c])=>!c.startsWith(`${s}/`)));return{databases:n,tables:o,columns:l}})},invalidateTable:(s,r,n)=>{const o=`${s}/${r}/${n}`;t(l=>{const c={...l.columns};return delete c[o],{columns:c}})}})),Z=i.forwardRef(({label:t,error:a,className:s,children:r,...n},o)=>e.jsxs("div",{className:"flex flex-col gap-1",children:[t&&e.jsx("label",{className:"text-xs font-medium text-slate-400 uppercase tracking-wide",children:t}),e.jsx("select",{ref:o,className:ue("bg-surface-800 border border-surface-700 rounded-md px-3 py-1.5 text-sm text-slate-100","focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-brand-500","disabled:opacity-50 disabled:cursor-not-allowed",a&&"border-red-500",s),...n,children:r}),a&&e.jsx("p",{className:"text-xs text-red-400",children:a})]}));Z.displayName="Select";function rt({open:t,onClose:a,sessionId:s,database:r,table:n,onImportComplete:o}){const[l,c]=i.useState(null),[d,u]=i.useState(null),[x,g]=i.useState(null),[j,y]=i.useState(!1),[S,m]=i.useState(","),[b,C]=i.useState(""),[v,I]=i.useState('"'),[R,k]=i.useState('"'),[h,_]=i.useState("crlf"),[T,M]=i.useState("utf-8"),F={crlf:`\r
`,lf:`
`,cr:"\r"}[h]||`\r
`,[z,G]=i.useState(!0),[f,D]=i.useState(n??""),[W,se]=i.useState("insert"),[ae,w]=i.useState(!1),[$,O]=i.useState(!1),[E,J]=i.useState(null),X=i.useRef(null),we=de(p=>p.tables[`${s}/${r}`]??[]),{loadTables:N}=de();i.useEffect(()=>{N(s,r)},[s,r,N]);const q=S==="custom"?b:S,L=i.useCallback(()=>JSON.stringify({database:r,table:f,delimiter:q,quotechar:v,escapechar:R,lineterminator:F,encoding:T,first_row_header:z,strategy:W}),[r,f,q,v,R,F,T,z,W]),Q=i.useRef();i.useEffect(()=>{if(!l){u(null),g(null);return}return clearTimeout(Q.current),Q.current=setTimeout(async()=>{y(!0),g(null);try{const p=new FormData;p.append("file",l),p.append("config",L());const H=await fetch(`/api/v1/sessions/${s}/import/preview`,{method:"POST",body:p});H.ok?(u(await H.json()),g(null)):(g(await H.text()),u(null))}catch(p){g(String(p)),u(null)}finally{y(!1)}},300),()=>clearTimeout(Q.current)},[l,q,v,R,h,T,z,s,L]);const K=p=>{p&&p.length>0&&(c(p[0]),J(null))},xe=p=>{p.preventDefault(),K(p.dataTransfer.files)},Se=async()=>{if(!(!l||!f)){O(!0),J(null);try{const p=new FormData;p.append("file",l),p.append("config",L());const Y=await(await fetch(`/api/v1/sessions/${s}/import`,{method:"POST",body:p})).json();J(Y),Y.ok&&(o==null||o())}catch(p){J({ok:!1,rows_processed:0,rows_imported:0,method:"unknown",error:String(p)})}finally{O(!1)}}};return e.jsx(ye,{open:t,onClose:a,title:`Import into ${r}${f?"."+f:""}`,width:"max-w-2xl",footer:e.jsxs(e.Fragment,{children:[e.jsx(B,{variant:"ghost",onClick:a,children:"Cancel"}),e.jsx(B,{variant:"primary",onClick:Se,disabled:$||!l||!f,children:$?"Importing…":"Import"})]}),children:e.jsxs("div",{className:"flex flex-col gap-4",children:[e.jsxs("div",{className:"border-2 border-dashed border-surface-700 rounded-lg p-6 text-center cursor-pointer hover:border-surface-600 transition-colors",onClick:()=>{var p;return(p=X.current)==null?void 0:p.click()},onDragOver:p=>p.preventDefault(),onDrop:xe,children:[e.jsx("input",{ref:X,type:"file",accept:".csv,.tsv,.txt",className:"hidden",onChange:p=>K(p.target.files)}),l?e.jsxs("div",{className:"text-sm",children:[e.jsx(le,{size:20,className:"mx-auto mb-2 text-brand-400"}),e.jsx("p",{className:"text-slate-200",children:l.name}),e.jsxs("p",{className:"text-xs text-slate-500 mt-1",children:[(l.size/1024).toFixed(1)," KB"]})]}):e.jsxs("div",{className:"text-sm text-slate-500",children:[e.jsx(le,{size:20,className:"mx-auto mb-2"}),e.jsx("p",{children:"Drop a CSV file here or click to select"}),e.jsx("p",{className:"text-xs mt-1",children:".csv, .tsv, .txt"})]})]}),j&&e.jsx("p",{className:"text-xs text-slate-500",children:"Loading preview…"}),x&&e.jsx("p",{className:"text-xs text-red-400",children:x}),d&&e.jsxs("div",{className:"overflow-x-auto max-h-48 border border-surface-700 rounded",children:[e.jsxs("table",{className:"text-xs w-full",children:[e.jsx("thead",{children:e.jsx("tr",{className:"bg-surface-800",children:d.columns.map((p,H)=>e.jsx("th",{className:"px-2 py-1 text-left text-slate-300 font-medium whitespace-nowrap border-b border-surface-700",children:p},H))})}),e.jsx("tbody",{children:d.rows.map((p,H)=>e.jsx("tr",{className:"hover:bg-surface-800/50",children:p.map((Y,ne)=>e.jsx("td",{className:"px-2 py-1 text-slate-400 whitespace-nowrap border-b border-surface-800",children:Y},ne))},H))})]}),d.total_lines_sampled>d.rows.length&&e.jsxs("p",{className:"text-xs text-slate-500 px-2 py-1",children:["Showing ",d.rows.length," of ",d.total_lines_sampled,"+ rows"]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs(Z,{label:"Target Table",value:f,onChange:p=>D(p.target.value),children:[e.jsx("option",{value:"",children:"Select table…"}),we.map(p=>e.jsx("option",{value:p.name,children:p.name},p.name))]}),e.jsxs(Z,{label:"Import Strategy",value:W,onChange:p=>se(p.target.value),children:[e.jsx("option",{value:"insert",children:"INSERT (fail on duplicate)"}),e.jsx("option",{value:"insert_ignore",children:"INSERT IGNORE (skip duplicates)"}),e.jsx("option",{value:"replace",children:"REPLACE INTO (overwrite)"})]})]}),e.jsxs("label",{className:"flex items-center gap-2 text-xs text-slate-300 cursor-pointer",children:[e.jsx("input",{type:"checkbox",checked:z,onChange:p=>G(p.target.checked),className:"rounded border-surface-600 bg-surface-800 text-brand-500 focus:ring-brand-500"}),"First row contains column names"]}),e.jsxs("div",{children:[e.jsxs("button",{type:"button",className:"flex items-center gap-1 text-xs text-slate-400 hover:text-slate-200 transition-colors",onClick:()=>w(!ae),children:[ae?e.jsx(_e,{size:12}):e.jsx(Ke,{size:12}),"CSV Format Options"]}),ae&&e.jsxs("div",{className:"mt-3 flex flex-col gap-3 pl-4 border-l border-surface-700",children:[e.jsxs("div",{className:"flex gap-3",children:[e.jsxs(Z,{label:"Delimiter",value:S,onChange:p=>m(p.target.value),children:[e.jsx("option",{value:",",children:"Comma (,)"}),e.jsx("option",{value:";",children:"Semicolon (;)"}),e.jsx("option",{value:"\\t",children:"Tab"}),e.jsx("option",{value:"|",children:"Pipe (|)"}),e.jsx("option",{value:"custom",children:"Custom"})]}),S==="custom"&&e.jsx(V,{label:"Custom",value:b,onChange:p=>C(p.target.value.slice(0,1)),className:"w-16"})]}),e.jsxs("div",{className:"flex gap-3",children:[e.jsxs(Z,{label:"Quote Character",value:v,onChange:p=>I(p.target.value),children:[e.jsx("option",{value:'"',children:'Double Quote (")'}),e.jsx("option",{value:"'",children:"Single Quote (')"}),e.jsx("option",{value:"",children:"None"})]}),e.jsxs(Z,{label:"Escape Character",value:R,onChange:p=>k(p.target.value),children:[e.jsx("option",{value:'"',children:'Double Quote (")'}),e.jsx("option",{value:"\\\\",children:"Backslash (\\)"}),e.jsx("option",{value:"",children:"None"})]})]}),e.jsxs("div",{className:"flex gap-3",children:[e.jsxs(Z,{label:"Line Ending",value:h,onChange:p=>_(p.target.value),children:[e.jsx("option",{value:"crlf",children:"CRLF (Windows)"}),e.jsx("option",{value:"lf",children:"LF (Unix/Mac)"}),e.jsx("option",{value:"cr",children:"CR (Legacy Mac)"})]}),e.jsxs(Z,{label:"Encoding",value:T,onChange:p=>M(p.target.value),children:[e.jsx("option",{value:"utf-8",children:"UTF-8"}),e.jsx("option",{value:"utf-8-sig",children:"UTF-8 with BOM"}),e.jsx("option",{value:"ascii",children:"ASCII"})]})]})]})]}),E&&e.jsx("div",{className:`p-3 rounded text-xs ${E.ok?"bg-green-900/30 border border-green-800 text-green-300":"bg-red-900/30 border border-red-800 text-red-300"}`,children:E.ok?e.jsxs(e.Fragment,{children:["Imported ",E.rows_imported," rows via ",E.method==="load_data"?"LOAD DATA":"batch insert",".",E.warnings&&E.warnings.length>0&&e.jsxs("ul",{className:"mt-1 list-disc list-inside text-yellow-400",children:[E.warnings.slice(0,5).map((p,H)=>e.jsx("li",{children:p},H)),E.warnings.length>5&&e.jsxs("li",{children:["…and ",E.warnings.length-5," more"]})]})]}):e.jsxs(e.Fragment,{children:["Import failed: ",E.error]})})]})})}const fs=Object.freeze(Object.defineProperty({__proto__:null,default:rt},Symbol.toStringTag,{value:"Module"}));function hs(t){const a=`lagun-bookmarks-${t}`,[s,r]=i.useState(()=>{try{const l=localStorage.getItem(a);return new Set(l?JSON.parse(l):[])}catch{return new Set}});return{bookmarks:s,toggle:(l,c)=>{const d=`${l}/${c}`;r(u=>{const x=new Set(u);return x.has(d)?x.delete(d):x.add(d),localStorage.setItem(a,JSON.stringify([...x])),x})},isBookmarked:(l,c)=>s.has(`${l}/${c}`)}}function ps({sessionId:t,selectedDatabases:a}){const{databases:s,loadDatabases:r,loadTables:n,tables:o,invalidateSession:l}=de(),{openTableTab:c,openQueryTab:d}=te(),[u,x]=i.useState(new Set),[g,j]=i.useState(""),[y,S]=i.useState(!1),[m,b]=i.useState(null),[C,v]=i.useState(null),{bookmarks:I,toggle:R,isBookmarked:k}=hs(t);i.useEffect(()=>{r(t)},[t,r]),i.useEffect(()=>{S(!1),j("")},[t]);const h=()=>{l(t),r(t),x(new Set),j("")},_=w=>{j(w),w&&F.forEach($=>{x(O=>new Set([...O,$])),o[`${t}/${$}`]||n(t,$)})},T=async w=>{const $=new Set(u);$.has(w)?$.delete(w):($.add(w),await n(t,w)),x($)},M=()=>{const w=!y;S(w),w&&F.forEach($=>{[...I].some(E=>E.startsWith(`${$}/`))&&(x(E=>new Set([...E,$])),o[`${t}/${$}`]||n(t,$))})},U=s[t]??[],F=a&&a.length>0?U.filter(w=>a.includes(w)):U,z=g.toLowerCase(),G=F.map(w=>{let O=o[`${t}/${w}`]??[];return y&&(O=O.filter(E=>k(w,E.name))),z&&(O=O.filter(E=>E.name.toLowerCase().includes(z))),{db:w,tbls:O}}).filter(({db:w,tbls:$})=>y?$.length>0:!z||$.length>0||w.toLowerCase().includes(z)),f=(w,$,O)=>{w.preventDefault(),w.stopPropagation(),b({x:w.clientX,y:w.clientY,db:$,table:O})},D=()=>b(null),W=async(w,$)=>{if(!confirm(`Drop table ${w}.${$}? This cannot be undone.`))return;await A.dropTable(t,w,$);const O=`${t}/${w}`;de.setState(E=>({tables:{...E.tables,[O]:(E.tables[O]??[]).filter(J=>J.name!==$)}})),D()},se=async(w,$)=>{confirm(`Truncate table ${w}.${$}? All rows will be deleted.`)&&(await A.truncateTable(t,w,$),D())},ae=y&&I.size===0;return e.jsxs("div",{className:"py-1",onClick:D,children:[e.jsxs("div",{className:"flex items-center justify-between px-3 py-1",children:[e.jsx("span",{className:"text-xs font-medium text-slate-500 uppercase tracking-wide",children:"Databases"}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx("button",{onClick:M,title:y?"Show all tables":"Show bookmarks only",className:`p-0.5 transition-colors ${y?"text-yellow-400":"text-slate-500 hover:text-slate-300"}`,children:e.jsx(Ne,{size:10,fill:y?"currentColor":"none"})}),e.jsx("button",{onClick:h,className:"p-0.5 text-slate-500 hover:text-slate-300 transition-colors",children:e.jsx(pe,{size:10})})]})]}),e.jsx("div",{className:"px-2 pb-1",children:e.jsxs("div",{className:"flex items-center gap-1.5 bg-surface-800 rounded px-2 py-1",children:[e.jsx(Ge,{size:10,className:"text-slate-500 flex-shrink-0"}),e.jsx("input",{type:"text",value:g,onChange:w=>_(w.target.value),placeholder:y?"Filter bookmarks…":"Filter tables…",className:"bg-transparent text-xs text-slate-300 placeholder-slate-600 flex-1 outline-none min-w-0"}),g&&e.jsx("button",{onClick:()=>j(""),className:"text-slate-500 hover:text-slate-300",children:e.jsx(ge,{size:10})})]})}),ae?e.jsxs("div",{className:"flex flex-col items-center gap-2 px-4 py-8 text-center",children:[e.jsx(Ne,{size:18,className:"text-slate-700"}),e.jsxs("p",{className:"text-xs text-slate-600",children:["No bookmarks yet.",e.jsx("br",{}),"Hover a table and click ★ to add one."]})]}):G.map(({db:w,tbls:$})=>{const O=u.has(w);return e.jsxs("div",{children:[e.jsxs("button",{className:"flex items-center gap-1.5 w-full px-2 py-1 hover:bg-surface-800 text-slate-300 group",onClick:()=>T(w),onContextMenu:E=>f(E,w),children:[O?e.jsx(_e,{size:12,className:"flex-shrink-0"}):e.jsx(Ke,{size:12,className:"flex-shrink-0"}),e.jsx(Ve,{size:12,className:"flex-shrink-0 text-yellow-400"}),e.jsx("span",{className:"text-xs truncate flex-1 text-left",children:w}),e.jsx("span",{role:"button",title:"New query on this database",onClick:E=>{E.stopPropagation(),d(t,w)},className:"opacity-0 group-hover:opacity-100 p-0.5 hover:text-brand-400",children:e.jsx(oe,{size:10})})]}),O&&$.map(E=>{const J=k(w,E.name);return e.jsxs("button",{className:"flex items-center gap-1.5 w-full pl-7 pr-2 py-0.5 hover:bg-surface-800 text-slate-400 hover:text-slate-200 group",onClick:()=>c(t,w,E.name),onContextMenu:X=>f(X,w,E.name),children:[e.jsx(ze,{size:11,className:"flex-shrink-0 text-slate-500"}),e.jsx("span",{className:"text-xs truncate flex-1 text-left",children:E.name}),e.jsx("span",{role:"button",title:J?"Remove bookmark":"Bookmark table",onClick:X=>{X.stopPropagation(),R(w,E.name)},className:`p-0.5 transition-colors ${J?"text-yellow-400":"opacity-0 group-hover:opacity-100 text-slate-500 hover:text-yellow-400"}`,children:e.jsx(Ne,{size:10,fill:J?"currentColor":"none"})})]},E.name)})]},w)}),m&&e.jsx("div",{className:"fixed bg-surface-800 border border-surface-700 rounded shadow-lg z-50 py-1 w-44",style:{top:m.y,left:m.x},onClick:w=>w.stopPropagation(),children:m.table?e.jsxs(e.Fragment,{children:[e.jsxs("button",{className:"flex items-center gap-2 w-full px-3 py-1.5 text-xs hover:bg-surface-700 text-slate-200",onClick:()=>{c(t,m.db,m.table),D()},children:[e.jsx(ze,{size:12})," Open Table"]}),e.jsxs("button",{className:"flex items-center gap-2 w-full px-3 py-1.5 text-xs hover:bg-surface-700 text-slate-200",onClick:()=>{d(t,m.db),D()},children:[e.jsx(oe,{size:12})," New Query"]}),e.jsxs("button",{className:"flex items-center gap-2 w-full px-3 py-1.5 text-xs hover:bg-surface-700 text-slate-200",onClick:()=>{v({db:m.db,table:m.table}),D()},children:[e.jsx(le,{size:12})," Import CSV"]}),e.jsx("hr",{className:"border-surface-700 my-1"}),e.jsxs("button",{className:"flex items-center gap-2 w-full px-3 py-1.5 text-xs hover:bg-surface-700 text-yellow-400",onClick:()=>se(m.db,m.table),children:[e.jsx(jt,{size:12})," Truncate"]}),e.jsxs("button",{className:"flex items-center gap-2 w-full px-3 py-1.5 text-xs hover:bg-surface-700 text-red-400",onClick:()=>W(m.db,m.table),children:[e.jsx($e,{size:12})," Drop Table"]})]}):e.jsxs("button",{className:"flex items-center gap-2 w-full px-3 py-1.5 text-xs hover:bg-surface-700 text-slate-200",onClick:()=>{d(t,m.db),D()},children:[e.jsx(oe,{size:12})," New Query"]})}),C&&e.jsx(rt,{open:!!C,onClose:()=>v(null),sessionId:t,database:C.db,table:C.table})]})}const gs={sm:"w-7 h-7",md:"w-9 h-9",lg:"w-14 h-14"},bs={sm:"text-sm",md:"text-base",lg:"text-xl"};function ot({size:t="md",showText:a=!0,className:s=""}){return e.jsxs("div",{className:`flex items-center gap-2 ${s}`,children:[e.jsxs("svg",{viewBox:"0 0 128 128",xmlns:"http://www.w3.org/2000/svg",className:`${gs[t]} flex-shrink-0`,children:[e.jsx("rect",{x:"4",y:"4",width:"120",height:"120",rx:"24",fill:"#f0f9ff"}),e.jsxs("g",{fill:"none",stroke:"#2563eb",strokeWidth:"3",strokeLinecap:"round",children:[e.jsx("path",{d:"M 28 34 Q 38 26 48 34 T 68 34 T 88 34 T 100 34"}),e.jsx("path",{d:"M 28 50 Q 38 42 48 50 T 68 50 T 88 50 T 100 50"}),e.jsx("path",{d:"M 28 66 Q 38 58 48 66 T 68 66 T 88 66 T 100 66"})]}),e.jsxs("g",{fill:"none",stroke:"#0891b2",strokeWidth:"2.5",strokeLinecap:"round",children:[e.jsx("line",{x1:"28",y1:"82",x2:"100",y2:"82"}),e.jsx("line",{x1:"28",y1:"94",x2:"100",y2:"94"}),e.jsx("line",{x1:"52",y1:"82",x2:"52",y2:"94"}),e.jsx("line",{x1:"76",y1:"82",x2:"76",y2:"94"})]}),e.jsx("circle",{cx:"64",cy:"74",r:"3",fill:"#0284c7"})]}),a&&e.jsx("span",{className:`font-semibold text-slate-100 ${bs[t]} tracking-tight`,children:"Lagun"})]})}function vs(){const{sessions:t,activeSessionId:a}=ee(),[s,r]=i.useState(!1),[n,o]=i.useState(!1),[l,c]=i.useState(!1);return e.jsxs("aside",{className:"w-64 flex-shrink-0 bg-surface-900 border-r border-surface-800 flex flex-col overflow-hidden",children:[e.jsxs("div",{className:"flex items-center justify-between px-3 py-3 border-b border-surface-800",children:[e.jsx(ot,{size:"sm",showText:!0}),e.jsxs("div",{className:"flex items-center gap-0.5",children:[e.jsx(B,{variant:"ghost",size:"sm",onClick:()=>c(!0),title:"Import connections",className:"p-1",children:e.jsx(le,{size:14})}),e.jsx(B,{variant:"ghost",size:"sm",onClick:()=>o(!0),title:"Export connections",className:"p-1",children:e.jsx(be,{size:14})}),e.jsx(B,{variant:"ghost",size:"sm",onClick:()=>r(!0),title:"New connection",className:"p-1",children:e.jsx(yt,{size:14})})]})]}),e.jsx("div",{className:"flex-shrink-0 border-b border-surface-800",children:e.jsx(us,{onNew:()=>r(!0)})}),e.jsx("div",{className:"flex-1 overflow-y-auto",children:a&&(()=>{const d=t.find(u=>u.id===a);return e.jsx(ps,{sessionId:a,selectedDatabases:d==null?void 0:d.selected_databases})})()}),e.jsx(nt,{open:s,onClose:()=>r(!1)}),e.jsx(xs,{open:n,onClose:()=>o(!1)}),e.jsx(ms,{open:l,onClose:()=>c(!1)})]})}function js(){const{tabs:t,activeTabId:a,setActiveTab:s,closeTab:r}=te();return t.length===0?null:e.jsx("div",{className:"flex items-center bg-surface-900 border-b border-surface-800 overflow-x-auto flex-shrink-0",children:t.map(n=>e.jsxs("button",{onClick:()=>s(n.id),className:ue("flex items-center gap-1.5 px-3 py-2 text-xs border-r border-surface-800 whitespace-nowrap min-w-0 group transition-colors",a===n.id?"bg-surface-950 text-slate-100 border-t-2 border-t-brand-500":"text-slate-400 hover:text-slate-200 hover:bg-surface-800"),children:[n.type==="query"?e.jsx(oe,{size:12,className:"flex-shrink-0"}):e.jsx(wt,{size:12,className:"flex-shrink-0"}),e.jsx("span",{className:"truncate max-w-[120px]",children:n.label}),e.jsx("span",{role:"button",onClick:o=>{o.stopPropagation(),r(n.id)},className:"ml-0.5 opacity-0 group-hover:opacity-100 hover:text-red-400 transition-opacity",children:e.jsx(ge,{size:10})})]},n.id))})}const ys="modulepreload",ws=function(t){return"/"+t},Ae={},Ie=function(a,s,r){let n=Promise.resolve();if(s&&s.length>0){document.getElementsByTagName("link");const l=document.querySelector("meta[property=csp-nonce]"),c=(l==null?void 0:l.nonce)||(l==null?void 0:l.getAttribute("nonce"));n=Promise.allSettled(s.map(d=>{if(d=ws(d),d in Ae)return;Ae[d]=!0;const u=d.endsWith(".css"),x=u?'[rel="stylesheet"]':"";if(document.querySelector(`link[href="${d}"]${x}`))return;const g=document.createElement("link");if(g.rel=u?"stylesheet":ys,u||(g.as="script"),g.crossOrigin="",g.href=d,c&&g.setAttribute("nonce",c),document.head.appendChild(g),u)return new Promise((j,y)=>{g.addEventListener("load",j),g.addEventListener("error",()=>y(new Error(`Unable to preload CSS for ${d}`)))})}))}function o(l){const c=new Event("vite:preloadError",{cancelable:!0});if(c.payload=l,window.dispatchEvent(c),!c.defaultPrevented)throw l}return n.then(l=>{for(const c of l||[])c.status==="rejected"&&o(c.reason);return a().catch(o)})};function Ss(){var t;return((t=crypto.randomUUID)==null?void 0:t.call(crypto))??"10000000-1000-4000-8000-100000000000".replace(/[018]/g,a=>(+a^crypto.getRandomValues(new Uint8Array(1))[0]&15>>+a/4).toString(16))}const Re=je()(De(t=>({entries:[],addEntry:a=>t(s=>({entries:[{...a,id:Ss(),timestamp:new Date().toISOString()},...s.entries].slice(0,200)})),clearLog:()=>t({entries:[]})}),{name:"lagun-query-log",version:1,partialize:t=>({entries:t.entries})})),Ns=[100,500,1e3,5e3,1e4];function Cs({value:t,onChange:a,onRun:s,running:r,database:n,schema:o,limit:l,onLimitChange:c}){const d=x=>{(x.ctrlKey||x.metaKey)&&x.key==="Enter"&&(x.preventDefault(),r||s())},u=i.useMemo(()=>Ue({dialect:ke,schema:o??{},defaultSchema:n}),[o,n]);return e.jsxs("div",{className:"flex flex-col h-full",children:[e.jsxs("div",{className:"flex items-center justify-between px-3 py-1.5 bg-surface-900 border-b border-surface-800",children:[e.jsx("div",{className:"flex items-center gap-2",children:n&&e.jsx("span",{className:"text-xs text-slate-500 bg-surface-800 px-2 py-0.5 rounded",children:n})}),e.jsxs("div",{className:"flex items-center gap-2",children:[c&&e.jsxs("div",{className:"flex items-center gap-1 text-xs text-slate-500",children:[e.jsx("span",{children:"Limit"}),e.jsx("select",{value:l,onChange:x=>c(Number(x.target.value)),className:"bg-surface-800 border border-surface-700 rounded px-1.5 py-0.5 text-xs text-slate-300 focus:outline-none focus:ring-1 focus:ring-brand-500",children:Ns.map(x=>e.jsx("option",{value:x,children:x.toLocaleString()},x))})]}),e.jsx("kbd",{className:"hidden sm:inline-flex items-center px-1.5 py-0.5 text-[10px] font-mono text-slate-500 bg-surface-800 border border-surface-700 rounded",children:"Ctrl+↵"}),e.jsxs(B,{variant:"primary",size:"sm",onClick:s,disabled:r||!t.trim(),title:"Run (Ctrl+Enter)",children:[r?e.jsx(re,{size:12,className:"animate-spin"}):e.jsx(St,{size:12}),"Run"]})]})]}),e.jsx("div",{className:"flex-1 overflow-hidden",onKeyDown:d,children:e.jsx(Qe,{value:t,onChange:a,extensions:[u],theme:He,className:"h-full text-sm",style:{height:"100%"},basicSetup:{lineNumbers:!0,highlightActiveLine:!0,autocompletion:!0,foldGutter:!0}})})]})}function ks({x:t,y:a,items:s,onClose:r}){const n=i.useRef(null),[o,l]=i.useState({left:t,top:a});return i.useLayoutEffect(()=>{if(!n.current)return;const{width:c,height:d}=n.current.getBoundingClientRect();l({left:Math.min(t,window.innerWidth-c-8),top:Math.min(a,window.innerHeight-d-8)})},[t,a]),i.useEffect(()=>{const c=u=>{u.key==="Escape"&&r()},d=u=>{n.current&&!n.current.contains(u.target)&&r()};return window.addEventListener("keydown",c),window.addEventListener("mousedown",d),()=>{window.removeEventListener("keydown",c),window.removeEventListener("mousedown",d)}},[r]),Xe.createPortal(e.jsx("div",{ref:n,className:"fixed z-[9999] bg-surface-900 border border-surface-700 rounded-md shadow-2xl py-1 min-w-[180px]",style:{left:o.left,top:o.top},onContextMenu:c=>c.preventDefault(),children:s.map((c,d)=>c.type==="separator"?e.jsx("div",{className:"border-t border-surface-700 my-1"},d):e.jsxs("button",{className:`flex items-center gap-2 w-full px-3 py-1.5 text-xs text-left ${c.danger?"text-red-400 hover:bg-red-950/40":"text-slate-300 hover:bg-surface-800"}`,onClick:c.onClick,children:[c.icon,c.label]},d))}),document.body)}async function Be(t){if(navigator.clipboard){await navigator.clipboard.writeText(t);return}const a=document.createElement("textarea");a.value=t,a.style.position="fixed",a.style.opacity="0",document.body.appendChild(a),a.focus(),a.select();const s=document.execCommand("copy");if(document.body.removeChild(a),!s)throw new Error("Copy failed")}const Es=Dt.withParams({backgroundColor:"#0f172a",foregroundColor:"#cbd5e1",headerBackgroundColor:"#1e293b",headerTextColor:"#94a3b8",borderColor:"#1e293b",rowHoverColor:"#1e293b",selectedRowBackgroundColor:"#1e3a5f",oddRowBackgroundColor:"#0f172a",fontFamily:"ui-monospace, monospace",fontSize:12,cellTextColor:"#cbd5e1",headerFontSize:12,headerFontWeight:600}),Ts=new Set(["datetime","timestamp","date","time"]);function $s(t){const a=o=>String(o).padStart(2,"0"),s=new Date,r=`${s.getFullYear()}-${a(s.getMonth()+1)}-${a(s.getDate())}`,n=`${a(s.getHours())}:${a(s.getMinutes())}:${a(s.getSeconds())}`;return t==="date"?r:t==="time"?n:`${r} ${n}`}function lt({result:t,onCellEdit:a,primaryKeyColumns:s=[],editable:r=!1,selectable:n,onSelectionChange:o,columns:l,onDeleteRows:c}){const[d,u]=i.useState(null),x=i.useRef(null),g=i.useMemo(()=>t.columns.map(h=>({field:h,headerName:h,editable:r&&h!=="__rowIndex",resizable:!0,sortable:!0,filter:!0,cellStyle:{fontFamily:"ui-monospace, monospace",fontSize:"12px"},headerClass:"text-xs font-semibold"})),[t.columns,r]),j=i.useMemo(()=>t.rows.map((h,_)=>{const T={__ag_rowId:String(_)};return t.columns.forEach((M,U)=>{T[M]=h[U]}),T}),[t.rows,t.columns]),y=i.useCallback(h=>h.data.__ag_rowId,[]),S=i.useMemo(()=>n?{mode:"multiRow",checkboxes:!0,headerCheckbox:!0,enableClickSelection:!1}:void 0,[n]),m=i.useMemo(()=>({minWidth:80,flex:1}),[]),b=i.useCallback(h=>{!a||!h.colDef.field||a({column:h.colDef.field,newValue:h.newValue,data:h.data})},[a]),C=i.useCallback(h=>{o==null||o(h.api.getSelectedRows())},[o]),v=i.useCallback(h=>{const _=h.event;if(!(_!=null&&_.shiftKey)){x.current=h.rowIndex??null;return}const T=h.rowIndex??0,M=x.current??T,U=Math.min(M,T),F=Math.max(M,T);h.api.deselectAll(),h.api.forEachNode(z=>{z.rowIndex!==null&&z.rowIndex>=U&&z.rowIndex<=F&&z.setSelected(!0)})},[]),I=i.useCallback(h=>{const _=h.event;if(!_)return;_.preventDefault();const T=h.api.getSelectedRows();u({x:_.clientX,y:_.clientY,columnName:h.colDef.field??"",cellValue:h.value,rowData:h.data??{},selectedRows:T})},[]),R=i.useCallback(()=>u(null),[]),k=i.useMemo(()=>{if(!d)return[];const h=l==null?void 0:l.find(z=>z.name===d.columnName),_=h&&Ts.has(h.data_type.toLowerCase()),T=d.selectedRows.length>0?d.selectedRows:[d.rowData],{__ag_rowId:M,...U}=d.rowData,F=[{type:"item",label:"Copy cell",icon:e.jsx(Ct,{size:12}),onClick:()=>{Be(String(d.cellValue??"")).catch(()=>{}),R()}},{type:"item",label:"Copy row as JSON",icon:e.jsx(kt,{size:12}),onClick:()=>{Be(JSON.stringify(U,null,2)).catch(()=>{}),R()}}];if(r&&(h!=null&&h.is_nullable&&(F.push({type:"separator"}),F.push({type:"item",label:"Set to NULL",icon:e.jsx(Nt,{size:12}),onClick:()=>{a==null||a({column:d.columnName,newValue:null,data:d.rowData}),R()}})),_)){const z=$s(h.data_type.toLowerCase());F.push({type:"item",label:`Set to NOW()  ${z}`,icon:e.jsx(Ye,{size:12}),onClick:()=>{a==null||a({column:d.columnName,newValue:z,data:d.rowData}),R()}})}if(c&&s.length>0){const z=T.length>1?`Delete ${T.length} rows`:"Delete row";F.push({type:"separator"}),F.push({type:"item",label:z,danger:!0,icon:e.jsx($e,{size:12}),onClick:()=>{c(T),R()}})}return F},[d,l,r,a,c,s,R]);return t.error?e.jsx("div",{className:"p-4 bg-red-950/50 border border-red-800 rounded m-3",children:e.jsx("p",{className:"text-xs font-mono text-red-300",children:t.error})}):e.jsxs("div",{className:"h-full",children:[e.jsx(It,{theme:Es,columnDefs:g,rowData:j,getRowId:y,onCellValueChanged:b,onSelectionChanged:C,onRowClicked:n?v:void 0,onCellContextMenu:I,onBodyScroll:R,defaultColDef:m,rowSelection:S,suppressContextMenu:!0,preventDefaultOnContextMenu:!0,singleClickEdit:!0,stopEditingWhenCellsLoseFocus:!0}),d&&e.jsx(ks,{x:d.x,y:d.y,items:k,onClose:R})]})}function it({result:t,running:a}){return a?e.jsx("div",{className:"flex items-center gap-3 px-3 py-1.5 bg-surface-900 border-t border-surface-800 text-xs text-slate-400",children:e.jsx("span",{className:"animate-pulse",children:"Executing..."})}):t?e.jsx("div",{className:"flex items-center gap-4 px-3 py-1.5 bg-surface-900 border-t border-surface-800 text-xs",children:t.error?e.jsxs("span",{className:"flex items-center gap-1 text-red-400",children:[e.jsx(Et,{size:11})," Error"]}):e.jsxs(e.Fragment,{children:[e.jsxs("span",{className:"flex items-center gap-1 text-slate-400",children:[e.jsx(Tt,{size:11}),t.row_count.toLocaleString()," ",t.row_count===1?"row":"rows"]}),t.affected_rows!=null&&e.jsxs("span",{className:"text-slate-400",children:[t.affected_rows," affected"]}),e.jsxs("span",{className:"flex items-center gap-1 text-slate-500",children:[e.jsx(Ye,{size:11}),t.exec_time_ms,"ms"]}),t.insert_id?e.jsxs("span",{className:"text-slate-500",children:["insert_id=",t.insert_id]}):null]})}):null}const _s=i.lazy(()=>Ie(()=>import("./TableSchemaView-C0NFMaU8.js"),__vite__mapDeps([0,1,2,3]))),ct=i.lazy(()=>Ie(()=>import("./ExportDialog-CysjZ1zB.js"),__vite__mapDeps([4,1,2,3]))),Ds=i.lazy(()=>Ie(()=>Promise.resolve().then(()=>fs),void 0)),Is=[100,500,1e3,5e3,1e4];function Rs({tab:t}){return t.type==="query"?e.jsx(zs,{tab:t}):e.jsx(Ls,{tab:t})}function zs({tab:t}){const a=te(k=>{var h;return((h=k.tabs.find(_=>_.id===t.id))==null?void 0:h.sql)??""}),s=te(k=>k.setSql),[r,n]=i.useState(a),o=i.useRef(r);o.current=r,i.useEffect(()=>{const k=setTimeout(()=>s(t.id,r),500);return()=>clearTimeout(k)},[r,t.id,s]),i.useEffect(()=>()=>{s(t.id,o.current)},[t.id,s]),i.useEffect(()=>{n(a)},[t.id]);const[l,c]=i.useState(null),[d,u]=i.useState(!1),[x,g]=i.useState(1e3),[j,y]=i.useState(!1),{tables:S,columns:m,loadTables:b,loadColumns:C}=de(),v=Re(k=>k.addEntry),I=i.useMemo(()=>{if(!t.database)return{};const k=S[`${t.sessionId}/${t.database}`]??[],h={};for(const _ of k){const T=`${t.sessionId}/${t.database}/${_.name}`;h[_.name]=(m[T]??[]).map(M=>M.name)}return h},[t.sessionId,t.database,S,m]);i.useEffect(()=>{t.database&&b(t.sessionId,t.database).then(k=>{for(const h of k)C(t.sessionId,t.database,h.name)})},[t.sessionId,t.database]);const R=async()=>{if(!r.trim())return;u(!0);const k=Date.now();try{const h=await A.executeQuery(t.sessionId,r,t.database,x);c(h),v({sql:r.trim(),sessionId:t.sessionId,database:t.database,rowCount:h.row_count??void 0,execTimeMs:h.exec_time_ms??Date.now()-k,error:h.error??void 0})}finally{u(!1)}};return e.jsxs("div",{className:"flex flex-col h-full",children:[e.jsx("div",{className:"h-48 flex-shrink-0 border-b border-surface-800",children:e.jsx(Cs,{value:r,onChange:n,onRun:R,running:d,database:t.database,schema:I,limit:x,onLimitChange:g})}),e.jsx("div",{className:"flex-1 overflow-hidden",children:l?e.jsx(lt,{result:l}):e.jsx("div",{className:"flex items-center justify-center h-full text-slate-600 text-sm",children:"Press Ctrl+Enter to run a query"})}),e.jsxs("div",{className:"flex items-center border-t border-surface-800",children:[e.jsx("div",{className:"flex-1",children:e.jsx(it,{result:l,running:d})}),l&&!l.error&&l.columns.length>0&&e.jsxs("button",{onClick:()=>y(!0),className:"flex items-center gap-1 px-3 py-1.5 text-xs text-slate-500 hover:text-slate-300 transition-colors",title:"Export result",children:[e.jsx(be,{size:11})," Export"]})]}),j&&t.database&&r.trim()&&e.jsx(i.Suspense,{fallback:null,children:e.jsx(ct,{open:j,onClose:()=>y(!1),sessionId:t.sessionId,database:t.database,table:"result",sql:r.trim()})})]})}function Ls({tab:t}){const[a,s]=i.useState("schema"),[r,n]=i.useState(null),[o,l]=i.useState([]),[c,d]=i.useState(!1),[u,x]=i.useState(!1),[g,j]=i.useState(1e3),[y,S]=i.useState(null),[m,b]=i.useState([]),[C,v]=i.useState(!1),[I,R]=i.useState(!1),[k,h]=i.useState(""),[_,T]=i.useState(""),[M,U]=i.useState(""),[F,z]=i.useState(!1),G=Re(N=>N.addEntry),f=o.filter(N=>N.is_primary_key).map(N=>N.name),D=async(N,q,L)=>{if(!t.database||!t.table)return;const Q=N??g,K=q!==void 0?q:k,xe=L!==void 0?L:M;b([]),r===null?d(!0):x(!0);const Se=Date.now();try{const p=await A.getColumns(t.sessionId,t.database,t.table);l(p);let H=`SELECT * FROM \`${t.database}\`.\`${t.table}\``;const Y=[];if(K.trim()&&p.length>0){const dt=K.replace(/\\/g,"\\\\").replace(/'/g,"''").replace(/%/g,"\\%").replace(/_/g,"\\_"),ut=p.map(xt=>`\`${xt.name}\` LIKE '%${dt}%'`);Y.push(`(${ut.join(" OR ")})`)}xe.trim()&&Y.push(`(${xe.trim()})`),Y.length>0&&(H+=` WHERE ${Y.join(" AND ")}`);const ne=await A.executeQuery(t.sessionId,H,void 0,Q);n(ne),G({sql:H,sessionId:t.sessionId,database:t.database,rowCount:ne.row_count??void 0,execTimeMs:ne.exec_time_ms??Date.now()-Se,error:ne.error??void 0})}finally{d(!1),x(!1)}};i.useEffect(()=>{a==="data"&&!r&&D()},[a]),i.useEffect(()=>{s("schema"),n(null),l([]),h(""),T(""),U(""),z(!1)},[t.sessionId,t.database,t.table]),i.useEffect(()=>{if(a!=="data")return;const N=setTimeout(()=>{D(void 0,k,M)},400);return()=>clearTimeout(N)},[k]);const W=async N=>{if(!t.database||!t.table||f.length===0)return;const q={};f.forEach(K=>{q[K]=N.data[K]});const L=Date.now(),Q=await A.cellUpdate(t.sessionId,{database:t.database,table:t.table,primary_key:q,column:N.column,new_value:N.newValue});Q.ok?(S(`✓ ${Q.sql_executed}`),G({sql:Q.sql_executed,sessionId:t.sessionId,database:t.database,affectedRows:Q.affected_rows??void 0,execTimeMs:Date.now()-L}),D()):(S(`✗ ${Q.error}`),G({sql:Q.sql_executed||`UPDATE ${t.database}.${t.table}`,sessionId:t.sessionId,database:t.database,execTimeMs:Date.now()-L,error:Q.error??void 0})),setTimeout(()=>S(null),4e3)},se=async N=>{if(!t.database||!t.table||f.length===0)return;const q=N.map(Q=>Object.fromEntries(f.map(K=>[K,Q[K]]))),L=await A.rowDelete(t.sessionId,{database:t.database,table:t.table,primary_keys:q});L.ok?(S(`✓ Deleted ${L.affected_rows} row${L.affected_rows!==1?"s":""}`),G({sql:`DELETE FROM \`${t.database}\`.\`${t.table}\` (${L.affected_rows} row${L.affected_rows!==1?"s":""})`,sessionId:t.sessionId,database:t.database,affectedRows:L.affected_rows,execTimeMs:0}),D()):S(`✗ ${L.error}`),setTimeout(()=>S(null),4e3)},ae=N=>{j(N),r&&D(N)},w=i.useCallback(()=>{U(_),D(void 0,k,_)},[_,k]),$=()=>{T(""),U(""),D(void 0,k,"")},O=()=>{h("")},E=i.useRef(w);E.current=w;const J=i.useMemo(()=>Ue({dialect:ke}),[]),X=i.useMemo(()=>{const N=o.map(q=>({label:q.name,type:"property",boost:99}));return ke.language.data.of({autocomplete:q=>{const L=q.matchBefore(/\w+/);return!L&&!q.explicit?null:{from:L?L.from:q.pos,options:N,validFor:/^\w*$/}}})},[o]),we=i.useMemo(()=>mt.highest(ft.of([{key:"Enter",run:()=>(E.current(),!0)},{key:"Mod-Enter",run:()=>(E.current(),!0)}])),[]);return e.jsxs("div",{className:"flex flex-col h-full",children:[e.jsxs("div",{className:"flex items-center gap-2 px-3 py-1.5 bg-surface-900 border-b border-surface-800",children:[e.jsxs("span",{className:"text-xs text-slate-400",children:[t.database,".",t.table]}),e.jsx("div",{className:"flex-1"}),a==="data"&&e.jsxs("div",{className:"relative flex items-center",children:[e.jsx(Ge,{size:11,className:"absolute left-2 text-slate-500 pointer-events-none"}),e.jsx("input",{type:"text",value:k,onChange:N=>h(N.target.value),placeholder:"Search all columns…",className:"bg-surface-800 border border-surface-700 rounded pl-6 pr-6 py-0.5 text-xs text-slate-300 placeholder-slate-600 focus:outline-none focus:ring-1 focus:ring-brand-500 w-44"}),k&&e.jsx("button",{onClick:O,className:"absolute right-1.5 text-slate-500 hover:text-slate-300",children:e.jsx(ge,{size:10})})]}),a==="data"&&e.jsxs("button",{onClick:()=>z(N=>!N),className:`flex items-center gap-1 px-2 py-0.5 text-xs rounded transition-colors ${F||M?"text-brand-400 bg-brand-950 border border-brand-800":"text-slate-500 hover:text-slate-300"}`,title:"Toggle WHERE filter",children:[e.jsx($t,{size:11}),M?"Filtered":"Filter"]}),a==="data"&&e.jsxs("div",{className:"flex items-center gap-1 text-xs text-slate-500",children:[e.jsx("span",{children:"Limit"}),e.jsx("select",{value:g,onChange:N=>ae(Number(N.target.value)),className:"bg-surface-800 border border-surface-700 rounded px-1.5 py-0.5 text-xs text-slate-300 focus:outline-none focus:ring-1 focus:ring-brand-500",children:Is.map(N=>e.jsx("option",{value:N,children:N.toLocaleString()},N))})]}),a==="data"&&u&&e.jsx(pe,{size:12,className:"animate-spin text-slate-400"}),e.jsxs("div",{className:"flex rounded overflow-hidden border border-surface-700",children:[e.jsx("button",{className:`px-2.5 py-0.5 text-xs transition-colors ${a==="schema"?"bg-brand-600 text-white":"bg-surface-800 text-slate-400 hover:text-slate-200"}`,onClick:()=>s("schema"),children:"Schema"}),e.jsx("button",{className:`px-2.5 py-0.5 text-xs transition-colors ${a==="data"?"bg-brand-600 text-white":"bg-surface-800 text-slate-400 hover:text-slate-200"}`,onClick:()=>s("data"),children:"Data"})]}),a==="data"&&e.jsxs("button",{onClick:()=>R(!0),className:"flex items-center gap-1 px-2 py-0.5 text-xs text-slate-500 hover:text-slate-300 transition-colors",title:"Import data",children:[e.jsx(le,{size:11})," Import"]}),a==="data"&&r&&!r.error&&e.jsxs("button",{onClick:()=>v(!0),className:"flex items-center gap-1 px-2 py-0.5 text-xs text-slate-500 hover:text-slate-300 transition-colors",title:"Export data",children:[e.jsx(be,{size:11})," Export"]}),a==="data"&&e.jsx(B,{variant:"ghost",size:"sm",onClick:()=>D(),title:"Refresh",disabled:c||u,children:e.jsx(pe,{size:12,className:c||u?"animate-spin":""})})]}),a==="data"&&F&&e.jsxs("div",{className:"flex items-center gap-2 px-3 py-1.5 bg-surface-900 border-b border-surface-700",children:[e.jsx("span",{className:"text-xs text-slate-500 font-mono shrink-0",children:"WHERE"}),e.jsx("div",{className:"flex-1 rounded overflow-hidden border border-surface-700 focus-within:ring-1 focus-within:ring-brand-500",children:e.jsx(Qe,{value:_,onChange:N=>{T(N),!N.trim()&&M&&(U(""),D(void 0,k,""))},extensions:[J,X,we],theme:He,height:"28px",placeholder:"e.g. id = 5 AND name LIKE 'John%'",basicSetup:{lineNumbers:!1,foldGutter:!1,highlightActiveLine:!1,autocompletion:!0}})}),e.jsxs("div",{className:"flex items-center gap-1.5 shrink-0",children:[e.jsx("kbd",{className:"hidden sm:inline-flex items-center px-1.5 py-0.5 text-[10px] font-mono text-slate-500 bg-surface-800 border border-surface-700 rounded",children:"Ctrl+↵"}),e.jsx("button",{onClick:w,className:"px-2.5 py-0.5 text-xs bg-brand-600 hover:bg-brand-500 text-white rounded transition-colors",children:"Apply"})]}),M&&e.jsx("button",{onClick:$,className:"px-2 py-0.5 text-xs text-slate-500 hover:text-slate-300 transition-colors shrink-0",children:"Clear"})]}),e.jsx("div",{className:"flex-1 overflow-hidden",children:a==="schema"&&t.database&&t.table?e.jsx(i.Suspense,{fallback:e.jsx("div",{className:"flex items-center justify-center h-full text-slate-500 text-sm",children:"Loading schema…"}),children:e.jsx(_s,{sessionId:t.sessionId,database:t.database,table:t.table})}):a==="data"?c?e.jsxs("div",{className:"flex items-center justify-center h-full text-slate-500 text-sm",children:["Loading ",t.table,"…"]}):r?e.jsx(lt,{result:r,editable:f.length>0,primaryKeyColumns:f,onCellEdit:W,onDeleteRows:se,selectable:!0,onSelectionChange:b,columns:o}):e.jsx("div",{className:"flex items-center justify-center h-full text-slate-600 text-sm",children:"No data loaded"}):null}),y&&e.jsx("div",{className:"px-3 py-1.5 bg-surface-900 border-t border-surface-800",children:e.jsx("p",{className:"text-xs font-mono text-slate-400 truncate",children:y})}),a==="data"&&r&&e.jsx(it,{result:r,running:!1}),C&&t.database&&t.table&&e.jsx(i.Suspense,{fallback:null,children:e.jsx(ct,{open:C,onClose:()=>v(!1),sessionId:t.sessionId,database:t.database,table:t.table,pkValues:m.length>0?m.map(N=>{const q=f.length>0?f:o.map(L=>L.name);return Object.fromEntries(q.map(L=>[L,N[L]]))}):void 0})}),I&&t.database&&e.jsx(i.Suspense,{fallback:null,children:e.jsx(Ds,{open:I,onClose:()=>R(!1),sessionId:t.sessionId,database:t.database,table:t.table,onImportComplete:()=>D()})})]})}function Ps(t){const a=new Date(t),s=String(a.getHours()).padStart(2,"0"),r=String(a.getMinutes()).padStart(2,"0"),n=String(a.getSeconds()).padStart(2,"0");return`${s}:${r}:${n}`}function Os(t){return t.affectedRows!=null?`${t.affectedRows} affected`:t.rowCount!=null?`${t.rowCount} rows`:"—"}function Ms(){const[t,a]=i.useState(!1),{entries:s,clearLog:r}=Re();return e.jsxs("div",{className:"flex-shrink-0 border-t border-surface-800 bg-surface-950",children:[e.jsxs("div",{className:"flex items-center gap-2 px-3 h-7 cursor-pointer select-none",onClick:()=>a(n=>!n),children:[e.jsx(oe,{size:12,className:"text-slate-500"}),e.jsx("span",{className:"text-xs text-slate-400 font-medium",children:"Query Log"}),s.length>0&&e.jsx("span",{className:"text-xs bg-surface-800 text-slate-400 rounded px-1 py-0.5 leading-none",children:s.length}),e.jsx("div",{className:"flex-1"}),t&&s.length>0&&e.jsx("button",{onClick:n=>{n.stopPropagation(),r()},className:"text-xs text-slate-500 hover:text-slate-300 transition-colors px-1",children:"Clear"}),t?e.jsx(_e,{size:12,className:"text-slate-500"}):e.jsx(_t,{size:12,className:"text-slate-500"})]}),t&&e.jsx("div",{className:"h-[180px] overflow-y-auto",children:s.length===0?e.jsx("div",{className:"flex items-center justify-center h-full text-slate-600 text-xs",children:"No queries logged yet"}):e.jsxs("table",{className:"w-full text-xs border-collapse",children:[e.jsx("thead",{className:"sticky top-0 bg-surface-900",children:e.jsxs("tr",{className:"text-slate-500",children:[e.jsx("th",{className:"text-left px-2 py-1 font-medium w-16",children:"Time"}),e.jsx("th",{className:"text-left px-2 py-1 font-medium",children:"SQL"}),e.jsx("th",{className:"text-left px-2 py-1 font-medium w-24",children:"Rows"}),e.jsx("th",{className:"text-left px-2 py-1 font-medium w-16",children:"ms"}),e.jsx("th",{className:"text-left px-2 py-1 font-medium w-12",children:"Status"})]})}),e.jsx("tbody",{children:s.map(n=>e.jsxs("tr",{className:`border-t border-surface-800 ${n.error?"bg-red-950/30":""}`,children:[e.jsx("td",{className:"px-2 py-0.5 text-slate-500 tabular-nums",children:Ps(n.timestamp)}),e.jsx("td",{className:"px-2 py-0.5 font-mono text-slate-300 max-w-0 w-full truncate",title:n.sql,children:n.sql.length>80?n.sql.slice(0,80)+"…":n.sql}),e.jsx("td",{className:"px-2 py-0.5 text-slate-400 tabular-nums",children:Os(n)}),e.jsx("td",{className:"px-2 py-0.5 text-slate-400 tabular-nums",children:n.execTimeMs}),e.jsx("td",{className:"px-2 py-0.5",children:n.error?e.jsx("span",{className:"text-red-400",title:n.error,children:"✗"}):e.jsx("span",{className:"text-green-400",children:"✓"})})]},n.id))})]})})]})}function Fs(){const{tabs:t,activeTabId:a}=te();return e.jsxs("div",{className:"flex h-screen overflow-hidden bg-surface-950 text-slate-100",children:[e.jsx(vs,{}),e.jsxs("div",{className:"flex flex-col flex-1 min-w-0 overflow-hidden",children:[e.jsx(js,{}),e.jsx("div",{className:"flex-1 overflow-hidden",children:t.length===0?e.jsxs("div",{className:"flex flex-col items-center justify-center h-full text-slate-500 gap-4",children:[e.jsx(ot,{size:"lg",showText:!1,className:"opacity-40"}),e.jsx("p",{className:"text-sm",children:"Select a table from the sidebar or open a query tab"})]}):t.map(s=>e.jsx("div",{className:`h-full ${a===s.id?"":"hidden"}`,children:e.jsx(Rs,{tab:s})},s.id))}),e.jsx(Ms,{})]})]})}function qs(){const t=ee(a=>a.loadSessions);return i.useEffect(()=>{t().then(()=>{const a=new Set(ee.getState().sessions.map(n=>n.id)),{activeSessionId:s}=ee.getState();s&&!a.has(s)&&ee.getState().setActiveSession(null);const{tabs:r}=te.getState();for(const n of r)a.has(n.sessionId)||te.getState().closeTab(n.id)})},[t]),e.jsx(Fs,{})}Ee.createRoot(document.getElementById("root")).render(e.jsx(We.StrictMode,{children:e.jsx(qs,{})}));export{B,V as I,ye as M,Z as S,A as a,Be as c,de as u};
