d1s 0.3.12 → 0.3.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/README.md +1 -1
  2. package/dist/.next/BUILD_ID +1 -1
  3. package/dist/.next/app-build-manifest.json +33 -33
  4. package/dist/.next/app-path-routes-manifest.json +9 -9
  5. package/dist/.next/build-manifest.json +2 -2
  6. package/dist/.next/server/app/_not-found/page_client-reference-manifest.js +1 -1
  7. package/dist/.next/server/app/_not-found.html +1 -1
  8. package/dist/.next/server/app/_not-found.rsc +1 -1
  9. package/dist/.next/server/app/api/cloudflare/route_client-reference-manifest.js +1 -1
  10. package/dist/.next/server/app/api/databases/route_client-reference-manifest.js +1 -1
  11. package/dist/.next/server/app/api/migrations/list/route_client-reference-manifest.js +1 -1
  12. package/dist/.next/server/app/api/migrations/sync/route_client-reference-manifest.js +1 -1
  13. package/dist/.next/server/app/api/migrations/sync-files/route_client-reference-manifest.js +1 -1
  14. package/dist/.next/server/app/api/pro/route_client-reference-manifest.js +1 -1
  15. package/dist/.next/server/app/api/pull-data/confirm/route_client-reference-manifest.js +1 -1
  16. package/dist/.next/server/app/api/pull-remote/confirm/route_client-reference-manifest.js +1 -1
  17. package/dist/.next/server/app/api/pull-remote/preview/route_client-reference-manifest.js +1 -1
  18. package/dist/.next/server/app/api/push-remote/confirm/route_client-reference-manifest.js +1 -1
  19. package/dist/.next/server/app/api/push-remote/preview/route_client-reference-manifest.js +1 -1
  20. package/dist/.next/server/app/api/query/route_client-reference-manifest.js +1 -1
  21. package/dist/.next/server/app/api/schema/route_client-reference-manifest.js +1 -1
  22. package/dist/.next/server/app/api/tables/[name]/import/route_client-reference-manifest.js +1 -1
  23. package/dist/.next/server/app/api/tables/[name]/route_client-reference-manifest.js +1 -1
  24. package/dist/.next/server/app/api/tables/route_client-reference-manifest.js +1 -1
  25. package/dist/.next/server/app/api/time-travel/preview/route_client-reference-manifest.js +1 -1
  26. package/dist/.next/server/app/api/time-travel/restore/route_client-reference-manifest.js +1 -1
  27. package/dist/.next/server/app/api/update/route.js +1 -1
  28. package/dist/.next/server/app/api/update/route_client-reference-manifest.js +1 -1
  29. package/dist/.next/server/app/index.html +2 -2
  30. package/dist/.next/server/app/index.rsc +2 -2
  31. package/dist/.next/server/app/page.js +1 -1
  32. package/dist/.next/server/app/page_client-reference-manifest.js +1 -1
  33. package/dist/.next/server/app-paths-manifest.json +9 -9
  34. package/dist/.next/server/pages/404.html +1 -1
  35. package/dist/.next/server/pages/500.html +1 -1
  36. package/dist/.next/server/pages-manifest.json +1 -1
  37. package/dist/.next/static/chunks/app/{page-8357e24c136e1cb2.js → page-39d6f7bc281c828c.js} +1 -1
  38. package/dist/.next/trace +3 -3
  39. package/package.json +1 -1
  40. /package/dist/.next/static/{pmKtFRQ7K5CL_jqemxlSj → ghHdNJKQy3Hmu0UqGdL7v}/_buildManifest.js +0 -0
  41. /package/dist/.next/static/{pmKtFRQ7K5CL_jqemxlSj → ghHdNJKQy3Hmu0UqGdL7v}/_ssgManifest.js +0 -0
@@ -1 +1 @@
1
- (self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[974],{930:(e,t,a)=>{"use strict";a.r(t),a.d(t,{default:()=>tU});var s=a(5155),l=a(2115);let n={rE:"0.3.12"};var r=a(1270),o=a(9940),i=a(8770),c=a(9007),d=a(5582),u=a(7528),m=a(109),x=a(2033),h=a(7513),f=a(8844),p=a(2593),g=a(3537);let b=l.forwardRef((e,t)=>{let{className:a,type:l,...n}=e;return(0,s.jsx)("input",{type:l,className:(0,g.cn)("flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-base shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",a),ref:t,...n})});b.displayName="Input";var j=a(3101);let v=(0,j.F)("relative w-full rounded-lg border px-4 py-3 text-sm [&>svg+div]:translate-y-[-3px] [&>svg]:absolute [&>svg]:left-4 [&>svg]:top-4 [&>svg]:text-foreground [&>svg~*]:pl-7",{variants:{variant:{default:"bg-background text-foreground",destructive:"border-destructive/50 text-destructive dark:border-destructive [&>svg]:text-destructive"}},defaultVariants:{variant:"default"}}),N=l.forwardRef((e,t)=>{let{className:a,variant:l,...n}=e;return(0,s.jsx)("div",{ref:t,role:"alert",className:(0,g.cn)(v({variant:l}),a),...n})});N.displayName="Alert",l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("h5",{ref:t,className:(0,g.cn)("mb-1 font-medium leading-none tracking-tight",a),...l})}).displayName="AlertTitle";let w=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("div",{ref:t,className:(0,g.cn)("text-sm [&_p]:leading-relaxed",a),...l})});w.displayName="AlertDescription";var y=a(5736),C=a(3263),S=a(2251),k=a(5797);let R=y.bL,E=y.l9;y.YJ,y.ZL,y.Pb,y.z6,l.forwardRef((e,t)=>{let{className:a,inset:l,children:n,...r}=e;return(0,s.jsxs)(y.ZP,{ref:t,className:(0,g.cn)("flex cursor-default gap-2 select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none focus:bg-accent data-[state=open]:bg-accent [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",l&&"pl-8",a),...r,children:[n,(0,s.jsx)(C.A,{className:"ml-auto"})]})}).displayName=y.ZP.displayName,l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(y.G5,{ref:t,className:(0,g.cn)("z-50 min-w-[8rem] overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-lg data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",a),...l})}).displayName=y.G5.displayName;let T=l.forwardRef((e,t)=>{let{className:a,sideOffset:l=4,...n}=e;return(0,s.jsx)(y.ZL,{children:(0,s.jsx)(y.UC,{ref:t,sideOffset:l,className:(0,g.cn)("z-50 min-w-[8rem] overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-md","data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",a),...n})})});T.displayName=y.UC.displayName;let A=l.forwardRef((e,t)=>{let{className:a,inset:l,...n}=e;return(0,s.jsx)(y.q7,{ref:t,className:(0,g.cn)("relative flex cursor-default select-none items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-none transition-colors focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 [&>svg]:size-4 [&>svg]:shrink-0",l&&"pl-8",a),...n})});A.displayName=y.q7.displayName,l.forwardRef((e,t)=>{let{className:a,children:l,checked:n,...r}=e;return(0,s.jsxs)(y.H_,{ref:t,className:(0,g.cn)("relative flex cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm outline-none transition-colors focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",a),checked:n,...r,children:[(0,s.jsx)("span",{className:"absolute left-2 flex h-3.5 w-3.5 items-center justify-center",children:(0,s.jsx)(y.VF,{children:(0,s.jsx)(S.A,{className:"h-4 w-4"})})}),l]})}).displayName=y.H_.displayName,l.forwardRef((e,t)=>{let{className:a,children:l,...n}=e;return(0,s.jsxs)(y.hN,{ref:t,className:(0,g.cn)("relative flex cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm outline-none transition-colors focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",a),...n,children:[(0,s.jsx)("span",{className:"absolute left-2 flex h-3.5 w-3.5 items-center justify-center",children:(0,s.jsx)(y.VF,{children:(0,s.jsx)(k.A,{className:"h-2 w-2 fill-current"})})}),l]})}).displayName=y.hN.displayName,l.forwardRef((e,t)=>{let{className:a,inset:l,...n}=e;return(0,s.jsx)(y.JU,{ref:t,className:(0,g.cn)("px-2 py-1.5 text-sm font-semibold",l&&"pl-8",a),...n})}).displayName=y.JU.displayName,l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(y.wv,{ref:t,className:(0,g.cn)("-mx-1 my-1 h-px bg-muted",a),...l})}).displayName=y.wv.displayName;var _=a(3190),O=a(2467);let I=(0,j.F)("inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",{variants:{variant:{default:"bg-primary text-primary-foreground shadow hover:bg-primary/90",destructive:"bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90",outline:"border border-input bg-background shadow-sm hover:bg-accent hover:text-accent-foreground",secondary:"bg-secondary text-secondary-foreground shadow-sm hover:bg-secondary/80",ghost:"hover:bg-accent hover:text-accent-foreground",link:"text-primary underline-offset-4 hover:underline"},size:{default:"h-9 px-4 py-2",sm:"h-8 rounded-md px-3 text-xs",lg:"h-10 rounded-md px-8",icon:"h-9 w-9"}},defaultVariants:{variant:"default",size:"default"}}),L=l.forwardRef((e,t)=>{let{className:a,variant:l,size:n,asChild:r=!1,...o}=e,i=r?O.DX:"button";return(0,s.jsx)(i,{className:(0,g.cn)(I({variant:l,size:n,className:a})),ref:t,...o})});L.displayName="Button";var U=a(9034);let M=l.forwardRef((e,t)=>{let{className:a,children:l,...n}=e;return(0,s.jsxs)(U.bL,{ref:t,className:(0,g.cn)("relative overflow-hidden",a),...n,children:[(0,s.jsx)(U.LM,{className:"h-full w-full rounded-[inherit]",children:l}),(0,s.jsx)(D,{}),(0,s.jsx)(U.OK,{})]})});M.displayName=U.bL.displayName;let D=l.forwardRef((e,t)=>{let{className:a,orientation:l="vertical",...n}=e;return(0,s.jsx)(U.VM,{ref:t,orientation:l,className:(0,g.cn)("flex touch-none select-none transition-colors","vertical"===l&&"h-full w-2.5 border-l border-l-transparent p-[1px]","horizontal"===l&&"h-2.5 flex-col border-t border-t-transparent p-[1px]",a),...n,children:(0,s.jsx)(U.lr,{className:"relative flex-1 rounded-full bg-border"})})});D.displayName=U.VM.displayName;let F=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("div",{className:"relative w-full overflow-auto",children:(0,s.jsx)("table",{ref:t,className:(0,g.cn)("w-full caption-bottom text-sm",a),...l})})});F.displayName="Table";let z=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("thead",{ref:t,className:(0,g.cn)("[&_tr]:border-b",a),...l})});z.displayName="TableHeader";let P=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("tbody",{ref:t,className:(0,g.cn)("[&_tr:last-child]:border-0",a),...l})});P.displayName="TableBody",l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("tfoot",{ref:t,className:(0,g.cn)("border-t bg-muted/50 font-medium [&>tr]:last:border-b-0",a),...l})}).displayName="TableFooter";let B=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("tr",{ref:t,className:(0,g.cn)("border-b transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted",a),...l})});B.displayName="TableRow";let W=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("th",{ref:t,className:(0,g.cn)("h-10 px-2 text-left align-middle font-medium text-muted-foreground [&:has([role=checkbox])]:pr-0 [&>[role=checkbox]]:translate-y-[2px]",a),...l})});W.displayName="TableHead";let Y=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("td",{ref:t,className:(0,g.cn)("p-2 align-middle [&:has([role=checkbox])]:pr-0 [&>[role=checkbox]]:translate-y-[2px]",a),...l})});Y.displayName="TableCell",l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("caption",{ref:t,className:(0,g.cn)("mt-4 text-sm text-muted-foreground",a),...l})}).displayName="TableCaption";var q=a(6615);let H=function(e){let{preview:t=[],onConfirm:a,onCancel:n,isConfirming:r,error:o,isLoading:i,success:c,databaseName:d,migrationsDir:u}=e,[m,h]=(0,l.useState)(!0);if(i)return(0,s.jsx)("div",{className:"fixed bottom-4 right-4 z-50 font-sans w-[600px]",children:(0,s.jsx)("div",{className:"rounded-lg border p-4 shadow-lg bg-background",children:(0,s.jsxs)("div",{className:"flex items-start gap-3",children:[(0,s.jsx)(x.A,{className:"h-5 w-5 text-neutral-600 shrink-0 mt-0.5 animate-spin"}),(0,s.jsx)("div",{className:"flex-1 space-y-3",children:(0,s.jsxs)("div",{className:"space-y-1",children:[(0,s.jsxs)("p",{className:"font-medium text-neutral-800",children:["Preparing Push Migration",d?" \xb7 ".concat(d):""]}),(0,s.jsx)("p",{className:"text-sm text-neutral-600",children:"Checking local migrations..."})]})})]})})});if(!(null==t?void 0:t.length))return(0,s.jsx)("div",{className:"fixed bottom-4 right-4 z-50 w-[600px]",children:(0,s.jsx)("div",{className:"rounded-lg border p-4",children:(0,s.jsx)("p",{className:"text-sm font-medium",children:"No migrations to preview"})})});let f=t.filter(e=>"will be applied"===e.action).length,p=t.filter(e=>"skipped"===e.action).length;return t.filter(e=>"applied"===e.action).length,(0,s.jsx)("div",{className:"fixed bottom-4 right-4 z-50 font-sans w-[600px]",children:(0,s.jsx)("div",{className:(0,g.cn)("rounded-lg border p-4 shadow-lg",o?"bg-destructive/15":"bg-background"),children:(0,s.jsxs)("div",{className:"flex flex-col gap-3",children:[(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between",children:[(0,s.jsxs)("h5",{className:"font-medium text-neutral-900",children:["Push Migration Preview",d&&(0,s.jsx)("span",{className:"ml-2 text-xs font-normal text-neutral-500 font-mono",children:d})]}),(0,s.jsx)("button",{onClick:n,className:"text-neutral-500 hover:text-neutral-900 p-1 rounded-md hover:bg-neutral-100",children:(0,s.jsx)(q.A,{className:"h-4 w-4"})})]}),(0,s.jsxs)("p",{className:"text-sm text-neutral-700",children:[f," migration(s) \uD83D\uDD52️ pending, ",p," to ⏭️ skip"]}),(0,s.jsx)("p",{className:"text-xs text-neutral-400 font-mono",children:u?(0,s.jsxs)(s.Fragment,{children:["\uD83D\uDCC1 ",u]}):(0,s.jsx)("span",{className:"text-amber-600",children:'⚠ migrations_dir not set — using default "migrations/"'})}),o?(0,s.jsx)("p",{className:"text-sm text-destructive font-medium mt-1",children:o.message}):c&&(0,s.jsx)("p",{className:"text-sm text-green-700 font-medium mt-1",children:c})]}),(0,s.jsxs)("div",{children:[(0,s.jsx)("button",{onClick:()=>h(!m),className:"text-sm font-medium hover:underline",children:m?"Hide Details":"Show Details"}),m&&(0,s.jsx)(M,{className:"h-[480px] mt-2 rounded border border-neutral-200",children:(0,s.jsxs)(F,{children:[(0,s.jsx)(z,{children:(0,s.jsxs)(B,{children:[(0,s.jsx)(W,{children:"Migration"}),(0,s.jsx)(W,{className:"text-center w-24",children:"Status"})]})}),(0,s.jsx)(P,{children:t.map((e,t)=>(0,s.jsxs)(B,{children:[(0,s.jsx)(Y,{className:"font-mono text-xs",children:e.name}),(0,s.jsx)(Y,{className:"text-center align-middle",children:(e=>{switch(e){case"will be applied":return"\uD83D\uDD52️";case"applied":return"✅";case"skipped":return"⏭️";case"failed":return"❌";default:return""}})(e.action)})]},t))})]})})]}),(0,s.jsxs)("p",{className:"text-xs text-neutral-400",children:["Seed files (",(0,s.jsx)("span",{className:"font-mono",children:"*seed*.sql"}),") are local-only and excluded from remote sync."]}),(0,s.jsxs)("div",{className:"flex gap-2 pt-1",children:[(0,s.jsx)(L,{onClick:a,disabled:r||0===f,className:"flex-1",children:r?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(x.A,{className:"mr-2 h-4 w-4 animate-spin"}),"Confirming..."]}):"Confirm Push"}),(0,s.jsx)(L,{variant:"outline",onClick:n,disabled:r,className:"flex-1",children:"Cancel"})]})]})})})},K=function(e){let{preview:t=[],onConfirm:a,onCancel:n,onDataPull:r,isConfirming:o,isDataPulling:i,error:c,isLoading:d,success:u,databaseName:m,migrationsDir:h}=e;console.log("PullPreviewToast rendered with:",{previewLength:t.length,isConfirming:o,error:c,isLoading:d}),(0,l.useEffect)(()=>(console.log("PullPreviewToast mounted/updated"),()=>console.log("PullPreviewToast unmounted")),[]);let[f,p]=(0,l.useState)(!1);if(d)return console.log("Rendering loading state"),(0,s.jsx)("div",{className:"fixed bottom-4 right-4 z-50 font-sans w-[600px]",children:(0,s.jsx)("div",{className:"rounded-lg border p-4 shadow-lg bg-background",children:(0,s.jsxs)("div",{className:"flex items-start gap-3",children:[(0,s.jsx)(x.A,{className:"h-5 w-5 text-neutral-600 shrink-0 mt-0.5 animate-spin"}),(0,s.jsx)("div",{className:"flex-1 space-y-3",children:(0,s.jsxs)("div",{className:"space-y-1",children:[(0,s.jsxs)("p",{className:"font-medium text-neutral-800",children:["Preparing Pull Migration",m?" \xb7 ".concat(m):""]}),(0,s.jsx)("p",{className:"text-sm text-neutral-600",children:"Checking remote migrations..."})]})})]})})});if(!(null==t?void 0:t.length))return console.log("No preview data available"),(0,s.jsx)("div",{className:"fixed bottom-4 right-4 z-50 w-[600px]",children:(0,s.jsx)("div",{className:"rounded-lg border bg-destructive/15 p-4",children:(0,s.jsx)("div",{className:"flex items-center gap-2",children:(0,s.jsx)("p",{className:"text-sm font-medium text-destructive",children:"No preview data available"})})})});let b=t.filter(e=>(e.migration||e.name||"").endsWith(".sql")),j=b.length;console.log("Migration stats:",{totalMigrations:j});let v=u&&!i&&!c;return(0,s.jsx)("div",{className:"fixed bottom-4 right-4 z-50 font-sans w-[600px]",children:(0,s.jsx)("div",{className:(0,g.cn)("rounded-lg border p-4 shadow-lg",c?"bg-destructive/15":"bg-background"),children:(0,s.jsx)("div",{className:"flex items-start gap-x-1",children:(0,s.jsxs)("div",{className:"flex-1 space-y-3",children:[(0,s.jsxs)("div",{className:"space-y-1",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between",children:[(0,s.jsxs)("h5",{className:(0,g.cn)("font-medium",c?"text-destructive":"text-neutral-900"),children:[c?"Pull Migration Error":"Pull Migration Preview",m&&(0,s.jsx)("span",{className:"ml-2 text-xs font-normal text-neutral-500 font-mono",children:m})]}),(0,s.jsx)("button",{onClick:n,className:"text-neutral-500 hover:text-neutral-900 p-1 rounded-md hover:bg-neutral-100",children:(0,s.jsx)(q.A,{className:"h-4 w-4"})})]}),(0,s.jsx)("p",{className:(0,g.cn)("text-sm",c?"text-destructive/80":"text-neutral-700"),children:"".concat(j," migration(s) will be applied")}),(0,s.jsx)("p",{className:"text-xs text-neutral-400 font-mono",children:h?(0,s.jsxs)(s.Fragment,{children:["\uD83D\uDCC1 ",h]}):(0,s.jsx)("span",{className:"text-amber-600",children:'⚠ migrations_dir not set — using default "migrations/"'})}),c&&(0,s.jsx)("p",{className:"text-sm text-destructive font-medium mt-1",children:c.message}),u&&(0,s.jsx)("p",{className:"text-sm text-green-700 font-medium mt-1",children:u})]}),(0,s.jsxs)("div",{children:[(0,s.jsx)("button",{onClick:()=>p(!f),className:"text-sm font-medium hover:underline",children:f?"Hide Details":"Show Details"}),f&&(0,s.jsx)(M,{className:"h-[480px] mt-2 rounded border border-neutral-200",children:(0,s.jsxs)("div",{className:"w-full",children:[(0,s.jsx)("div",{className:"bg-neutral-50 border-b px-4 py-2",children:(0,s.jsx)("div",{className:"text-sm font-medium text-neutral-600",children:"Migration"})}),(0,s.jsx)("div",{children:b.map((e,t)=>(0,s.jsx)("div",{className:"px-4 py-2 border-b last:border-0",children:(0,s.jsx)("div",{className:"font-mono text-xs",children:e.migration||e.name})},t))})]})})]}),(0,s.jsxs)("p",{className:"text-xs text-neutral-400",children:["Seed files (",(0,s.jsx)("span",{className:"font-mono",children:"*seed*.sql"}),") are local-only and excluded from remote sync."]}),(0,s.jsx)("div",{className:"flex flex-col gap-2 pt-1",children:(0,s.jsxs)("div",{className:"flex gap-2",children:[(0,s.jsx)(L,{onClick:a,disabled:o,className:"flex-1 ".concat(o?"cursor-not-allowed opacity-50":""),children:o?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin"}),"Confirming..."]}):"Confirm Pull"}),(0,s.jsxs)("div",{className:"flex-1 flex flex-col gap-2",children:[(0,s.jsx)(L,{onClick:r,disabled:!v,variant:"outline",className:"w-full ".concat(v?"":"cursor-not-allowed opacity-50"),children:i?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin"}),"Syncing remote data..."]}):"Sync remote data"}),(0,s.jsx)("p",{className:"text-xs text-neutral-500 text-center",children:"75 records per table"})]})]})})]})})})})};function V(){var e,t;let[a]=(0,l.useState)(!1),[n,r]=(0,l.useState)(!1),[o,i]=(0,l.useState)(!1),[c,d]=(0,l.useState)(null),[u,m]=(0,l.useState)(null),[x,h]=(0,l.useState)(null),[f,p]=(0,l.useState)(null),{refreshTables:g,setOperationStatus:b,setLastSyncTime:j,currentDatabase:v}=(0,_.t)(),N=()=>{i(!1),d(null),m(null),h(null),p(null)},w=async()=>{try{h("push"),i(!0),r(!0),m(null),p(null),d({preview:[]});let t=await fetch("/api/push-remote/preview",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({db:null==v?void 0:v.database_name})}),a=await t.json();if(!a.success){var e;throw Error((null==(e=a.error)?void 0:e.message)||"Failed to generate preview")}d(a.details.details)}catch(e){console.error("Push to remote preview failed:",e),m(e),b("error",e.message||"Failed to generate push preview")}finally{r(!1)}},y=async()=>{try{r(!0),m(null),p(null);let t=await fetch("/api/push-remote/confirm",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({db:null==v?void 0:v.database_name})}),a=await t.json();if(!a.success){var e;throw Error((null==(e=a.error)?void 0:e.message)||"Failed to confirm push")}d(e=>({...e,preview:e.preview.map(e=>({...e,action:"will be applied"===e.action?"applied":e.action}))})),p("Applied ".concat(a.details.applied," migration(s), skipped ").concat(a.details.skipped,".")),j(),b("success","Push complete — ".concat(a.details.applied," migration(s) applied"))}catch(e){console.error("Push to remote confirm failed:",e),m(e)}finally{r(!1)}},C=async()=>{try{h("pull"),i(!0),r(!0),m(null),p(null),d({preview:[]});let t=await fetch("/api/pull-remote/preview",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({db:null==v?void 0:v.database_name})}),a=await t.json();if(!a.success){var e;throw Error((null==(e=a.error)?void 0:e.message)||"Failed to generate pull preview")}d(a.details)}catch(e){console.error("Pull to remote preview failed:",e),m(e),b("error",e.message||"Failed to generate pull preview")}finally{r(!1)}},S=async()=>{try{r(!0),m(null),p(null);let t=await fetch("/api/pull-remote/confirm",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({db:null==v?void 0:v.database_name})}),a=await t.json();if(!a.success){var e;throw Error((null==(e=a.error)?void 0:e.message)||"Failed to confirm pull")}p("Pull migrations applied successfully."),j(),b("success","Pull complete"),d(e=>({...e,preview:e.preview.map(e=>({...e,action:"will be applied"===e.action?"applied":e.action}))})),await g()}catch(e){console.error("Pull to remote confirm failed:",e),m(e)}finally{r(!1)}},k=o&&("pull"===x?(0,s.jsx)(K,{preview:(null==c?void 0:c.preview)||[],onConfirm:S,onCancel:N,isConfirming:n,error:u,success:f,isLoading:n&&!(null==c||null==(e=c.preview)?void 0:e.length),databaseName:null==v?void 0:v.database_name,migrationsDir:null==v?void 0:v.migrations_dir}):(0,s.jsx)(H,{preview:(null==c?void 0:c.preview)||[],onConfirm:y,onCancel:N,isConfirming:n,error:u,success:f,isLoading:n&&!(null==c||null==(t=c.preview)?void 0:t.length),databaseName:null==v?void 0:v.database_name,migrationsDir:null==v?void 0:v.migrations_dir}));return{isConnecting:a,isPushing:n,showPreview:o,pushToRemote:w,pullToRemotePreview:C,PreviewUI:k}}function J(e){let{icon:t,label:a,view:l,currentView:n,onNavigate:o,disabled:i,pro:c,locked:d}=e,u=n===l;return(0,s.jsxs)("button",{className:"w-full flex items-center gap-x-1.5 border rounded-md text-sm font-medium transition-colors px-2 py-2\n ".concat(u?"bg-neutral-900 text-white":"bg-white hover:bg-neutral-100"),onClick:()=>o(l),disabled:i,title:d?"Pro license required — run: d1s --license YOUR-KEY":void 0,children:[(0,s.jsx)(t,{className:"h-[16px] w-[16px] flex-shrink-0"}),(0,s.jsx)("span",{className:"flex-1 text-left",children:a}),c&&d&&(0,s.jsx)(r.A,{className:"h-3 w-3 opacity-40 flex-shrink-0"})]})}function Q(e){let{className:t,currentView:a,onNavigate:g,onTableSelect:j}=e,{tables:v,databases:y,currentDatabase:C,sourceMode:S,setSourceMode:k,switchDatabase:O,isLoading:I,error:L,lastSyncTime:U}=(0,_.t)(),[M,D]=(0,l.useState)(""),[F,z]=(0,l.useState)(null),[P,B]=(0,l.useState)(!1),[W,Y]=(0,l.useState)(!1);(0,l.useEffect)(()=>{fetch("/api/pro").then(e=>e.json()).then(e=>Y(!!e.isPro)).catch(()=>{})},[]);let{isConnecting:q,isPushing:H,pushToRemote:K,pullToRemotePreview:Q,PreviewUI:X}=V(),G=(0,l.useMemo)(()=>{let e=[...(null==v?void 0:v.filter(e=>e.toLowerCase().includes(M.toLowerCase())))||[]].sort((e,t)=>e.localeCompare(t)),t=e.indexOf("d1_migrations");return -1!==t&&(e.splice(t,1),e.unshift("d1_migrations")),e},[v,M]),Z=async e=>{try{B(!0),z(e),j(e)}catch(e){console.error("Error selecting table:",e)}finally{B(!1)}};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("aside",{className:"".concat(t," top-[var(--banner-height,0px)] left-0 h-[calc(100vh-var(--banner-height,0px))] bg-neutral-50 p-4"),children:(0,s.jsxs)("div",{className:"flex flex-col h-full font-sans",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between gap-2 mb-3",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)(o.A,{className:"h-5 w-5"}),(0,s.jsx)("span",{className:"font-mono font-bold text-sm",children:"d1-studio"})]}),(0,s.jsxs)("div",{className:"text-xs font-medium text-muted-foreground",children:["v",n.rE]})]}),L&&(0,s.jsxs)(N,{variant:"destructive",className:"mb-4",children:[(0,s.jsx)(i.A,{className:"h-4 w-4"}),(0,s.jsx)(w,{className:"text-sm ml-2",children:L})]}),(0,s.jsxs)("div",{className:"mb-1.5",children:[(0,s.jsx)("div",{className:"text-xs text-muted-foreground mb-1",children:"Database"}),(0,s.jsxs)(R,{children:[(0,s.jsx)(E,{asChild:!0,children:(0,s.jsxs)("button",{className:"flex items-center gap-1.5 text-sm font-semibold hover:text-foreground/80 transition-colors max-w-full w-full text-left",children:[(0,s.jsx)("span",{className:"truncate",children:(null==C?void 0:C.database_name)||"Select database"}),C&&(0,s.jsx)("span",{className:"shrink-0 text-[10px] font-medium px-1.5 py-0.5 rounded ".concat("remote"===S?"bg-sky-100 text-sky-700":"bg-green-100 text-green-700"),children:"remote"===S?"Remote":"Local".concat(C.hasLocal?" ✓":"")}),(0,s.jsx)(c.A,{className:"h-3 w-3 shrink-0 ml-auto opacity-50"})]})}),(0,s.jsx)(T,{align:"start",className:"w-56",children:y.map(e=>(0,s.jsxs)(A,{onClick:()=>O(e.database_name),className:"flex items-center justify-between gap-2 ".concat((null==C?void 0:C.database_name)===e.database_name?"font-semibold bg-accent":""),children:[(0,s.jsx)("span",{className:"truncate",children:e.database_name}),(0,s.jsx)("span",{className:"shrink-0 text-[10px] font-medium px-1.5 py-0.5 rounded ".concat("remote"===S?"bg-sky-100 text-sky-700":"bg-green-100 text-green-700"),children:"remote"===S?"Remote":"Local".concat(e.hasLocal?" ✓":"")})]},e.database_name))})]})]}),(0,s.jsxs)("div",{className:"mb-2",children:[(0,s.jsx)("div",{className:"text-xs text-muted-foreground mb-1",children:"Source"}),(0,s.jsxs)("div",{className:"flex items-center gap-1 rounded-md border bg-white p-1",children:[(0,s.jsx)("button",{onClick:()=>{k("local").catch(()=>{})},className:"flex-1 rounded px-2 py-1.5 text-xs font-semibold transition-colors ".concat("local"===S?"bg-neutral-900 text-white":"text-neutral-700 hover:bg-neutral-100"),children:"Local"}),(0,s.jsx)("button",{onClick:()=>{k("remote").catch(()=>{})},disabled:!W,title:W?"Browse/query remote database":"Pro license required — run: d1s --license YOUR-KEY",className:"flex-1 rounded px-2 py-1.5 text-xs font-semibold transition-colors disabled:opacity-50 disabled:cursor-not-allowed ".concat("remote"===S?"bg-neutral-900 text-white":"text-neutral-700 hover:bg-neutral-100"),children:W||"remote"===S?"Remote":(0,s.jsxs)("span",{className:"inline-flex items-center gap-1",children:[(0,s.jsx)(r.A,{className:"h-3.5 w-3.5 opacity-60"}),"Remote"]})})]}),(0,s.jsx)("div",{className:"text-[11px] text-muted-foreground px-0.5 mt-1",children:"remote"===S?"Remote is read-only. Switch to Local for sync/edit actions.":"Local mode enables Pull/Push and row editing."})]}),(0,s.jsxs)("div",{className:"mb-2",children:[(0,s.jsxs)("div",{className:"flex gap-1.5 mb-1",children:[(0,s.jsxs)("button",{onClick:Q,disabled:!W||"remote"===S||q||H,title:W?"remote"===S?"Switch source to Local to use Pull":void 0:"Pro license required — run: d1s --license YOUR-KEY",className:"flex-1 flex items-center justify-center gap-1 text-xs font-medium border rounded-md px-2 py-1.5 bg-white hover:bg-neutral-100 disabled:opacity-50 disabled:cursor-not-allowed transition-colors",children:[W?(0,s.jsx)(d.A,{className:"h-3.5 w-3.5"}):(0,s.jsx)(r.A,{className:"h-3.5 w-3.5 opacity-40"}),"Pull"]}),(0,s.jsxs)("button",{onClick:K,disabled:!W||"remote"===S||q||H,title:W?"remote"===S?"Switch source to Local to use Push":void 0:"Pro license required — run: d1s --license YOUR-KEY",className:"flex-1 flex items-center justify-center gap-1 text-xs font-medium border rounded-md px-2 py-1.5 bg-white hover:bg-neutral-100 disabled:opacity-50 disabled:cursor-not-allowed transition-colors",children:[W?(0,s.jsx)(u.A,{className:"h-3.5 w-3.5"}):(0,s.jsx)(r.A,{className:"h-3.5 w-3.5 opacity-40"}),"Push"]})]}),(0,s.jsx)("div",{className:"text-[11px] text-muted-foreground px-0.5",children:U?"Remote sync \xb7 ".concat(function(e){if(!e)return null;let t=Math.floor((Date.now()-new Date(e).getTime())/1e3);return t<60?"just now":t<3600?"".concat(Math.floor(t/60)," min ago"):t<86400?"".concat(Math.floor(t/3600),"h ago"):"".concat(Math.floor(t/86400),"d ago")}(U)):"Never synced"})]}),(0,s.jsxs)("div",{className:"relative mt-3 mb-2 bg-white",children:[(0,s.jsx)(m.A,{className:"absolute left-2 top-2.5 h-4 w-4 text-neutral-500"}),(0,s.jsx)(b,{placeholder:"Search tables",value:M,onChange:e=>D(e.target.value),className:"pl-7"})]}),(0,s.jsxs)("div",{className:"flex-1 min-h-0 flex flex-col",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between mb-1 flex-none",children:[(0,s.jsxs)("h3",{className:"text-sm font-medium text-muted-foreground",children:["Tables ",(null==v?void 0:v.length)?"(".concat(v.length,")"):""]}),I&&(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin text-muted-foreground"})]}),(0,s.jsx)("div",{className:"overflow-y-auto flex-1",children:(0,s.jsxs)("div",{className:"pr-2 pb-2",children:[!I&&0===G.length&&(0,s.jsx)("div",{className:"text-sm font-medium text-muted-foreground px-2 py-2",children:M?"No matching tables found":L?"Error loading tables":"No tables available"}),G.map(e=>(0,s.jsx)("button",{className:"w-full text-left text-sm font-medium rounded-md transition-colors px-2 py-2\n ".concat("table-view"===a&&F===e?"bg-[#F0F0F0] text-neutral-900 font-semibold":"text-neutral-700 hover:bg-[#F2F2F2]","\n ").concat(P&&"opacity-50 cursor-not-allowed"),onClick:()=>Z(e),disabled:P,children:e},e))]})})]}),(0,s.jsxs)("div",{className:"pt-2 flex flex-col gap-1",children:[(0,s.jsx)(J,{icon:h.A,label:"SQL editor",view:"sql-editor",currentView:a,onNavigate:g,disabled:P}),(0,s.jsx)(J,{icon:f.A,label:"Schema",view:"schema",currentView:a,onNavigate:g,disabled:P}),(0,s.jsx)(J,{icon:p.A,label:"Time travel",view:"time-travel",currentView:a,onNavigate:g,disabled:P||!W,pro:!0,locked:!W})]})]})}),X]})}var X=a(5778),G=a(2300),Z=a(4338),$=a(4903);function ee(e){let{results:t}=e;return t&&Array.isArray(t)?(0,s.jsxs)("div",{className:"text-xs font-medium text-neutral-900 flex items-center gap-2 mb-2",children:[t.length," ",1===t.length?"row":"rows"," returned"]}):null}function et(e){var t;let{error:a}=e;console.debug("SQLErrorDisplay - Received error prop:",a);let l=a&&"string"==typeof a.message?a:{message:"An unknown error occurred",details:{}};return(0,s.jsxs)("div",{children:[(0,s.jsx)("div",{className:"text-neutral-700 text-sm",children:l.message}),(null==(t=l.details)?void 0:t.stderr)&&(0,s.jsx)("pre",{className:"mt-2 p-2 text-xs text-red-500 whitespace-pre-wrap",children:l.details.stderr})]})}function ea(e){let{query:t,results:a}=e;if(!(null==a?void 0:a.length))return(0,s.jsx)("div",{className:"text-sm text-neutral-500",children:"No results to display."});let l=null==t?void 0:t.trim().toLowerCase().startsWith("pragma table_info"),n=l?["Column Name","Type","Not Null","Default Value","Primary Key"]:Object.keys(a[0]),r=l?a.map(e=>({"Column Name":e.name,Type:e.type,"Not Null":1===e.notnull?"YES":"NO","Default Value":e.dflt_value||"(null)","Primary Key":1===e.pk?"YES":"NO"})):a;return(0,s.jsxs)("div",{className:"flex flex-col h-full",children:[(0,s.jsx)(ee,{results:r}),(0,s.jsx)("div",{className:"flex-1 overflow-auto border rounded-lg",children:(0,s.jsx)("div",{className:"min-w-full",children:(0,s.jsxs)(F,{children:[(0,s.jsx)(z,{children:(0,s.jsx)(B,{className:"bg-muted/50",children:n.map(e=>(0,s.jsx)(W,{className:"font-mono text-xs font-medium text-muted-foreground",children:e},e))})}),(0,s.jsx)(P,{children:r.map((e,t)=>(0,s.jsx)(B,{className:"hover:bg-muted/50",children:n.map(a=>{let l=e[a],n=null===l?"(null)":String(l);return(0,s.jsx)(Y,{className:"font-monospace text-xs whitespace-nowrap",children:n},"".concat(t,"-").concat(a))})},t))})]})})})]})}let es={SYNTAX_ERROR:"SYNTAX_ERROR",EXECUTION_ERROR:"EXECUTION_ERROR",TABLE_NOT_FOUND:"TABLE_NOT_FOUND",TABLE_EXISTS:"TABLE_EXISTS",FOREIGN_KEY_ERROR:"FOREIGN_KEY_ERROR",UNIQUE_CONSTRAINT_ERROR:"UNIQUE_CONSTRAINT_ERROR",NOT_NULL_CONSTRAINT_ERROR:"NOT_NULL_CONSTRAINT_ERROR",CHECK_CONSTRAINT_ERROR:"CHECK_CONSTRAINT_ERROR",UNKNOWN_ERROR:"UNKNOWN_ERROR"};function el(e){if(!e)return{type:es.UNKNOWN_ERROR,message:"An unknown error occurred",details:{}};let t=null,a=e.cmd||e.details&&e.details.command;if(a){let e=a.match(RegExp('--command="(.+?)"\\s*--json',"s"));e&&(t=e[1].replace(/\\n/g,"\n").replace(/\\"/g,'"'))}let s=e.stdout||e.details&&e.details.stdout;if(s)try{var l;let e=JSON.parse(s.trim()),n=(null==e||null==(l=e.error)?void 0:l.text)||"";if(n)return en(n,t,a)}catch(e){}let n=e.stderr||e.details&&e.details.stderr;if(n)return en(n.replace(/\x1B\[[0-9;]*[a-zA-Z]/g,""),t,a);let r=e.message||e.details&&e.details.originalError||"";return r?en(r,t,a):{type:es.UNKNOWN_ERROR,message:e.message||"An unknown error occurred",details:{}}}function en(e,t,a){if(e.includes("env: node: No such file or directory"))return{type:es.EXECUTION_ERROR,message:"Node.js not found — ensure Node.js is installed and in PATH",details:{originalQuery:t}};let s=e.match(/near "([^"]+)": syntax error(?:\s+at offset (\d+))?/);if(s){let[,e,a]=s;return{type:es.SYNTAX_ERROR,message:'Syntax error near "'.concat(e,'"').concat(a?" at position ".concat(a):""),details:{originalQuery:t,errorLocation:{near:e,offset:a?parseInt(a):null}}}}if(e.includes("UNIQUE constraint failed")){let[,a]=e.match(/UNIQUE constraint failed: ([\w.]+)/)||[];return{type:es.UNIQUE_CONSTRAINT_ERROR,message:"UNIQUE constraint failed: ".concat(a||"unknown column"),details:{originalQuery:t}}}if(e.includes("NOT NULL constraint failed")){let[,a]=e.match(/NOT NULL constraint failed: ([\w.]+)/)||[];return{type:es.NOT_NULL_CONSTRAINT_ERROR,message:"NOT NULL constraint failed: ".concat(a||"unknown column"),details:{originalQuery:t}}}if(e.includes("CHECK constraint failed")){let[,a]=e.match(/CHECK constraint failed: (\S+)/)||[];return{type:es.CHECK_CONSTRAINT_ERROR,message:"CHECK constraint failed: ".concat(a||"unknown constraint"),details:{originalQuery:t}}}if(e.includes("FOREIGN KEY constraint failed"))return{type:es.FOREIGN_KEY_ERROR,message:"FOREIGN KEY constraint failed",details:{originalQuery:t}};if(e.includes("no such table")){let[,a]=e.match(/no such table: (\S+)/)||[];return{type:es.TABLE_NOT_FOUND,message:"Table not found: ".concat(a||"unknown"),details:{originalQuery:t}}}if(e.includes("already exists")){let[,a]=e.match(/table (\w+) already exists/)||[];return{type:es.TABLE_EXISTS,message:"Table already exists: ".concat(a||"unknown"),details:{originalQuery:t}}}return{type:es.EXECUTION_ERROR,message:e.replace(/:\s*SQLITE_\w+$/,"").trim()||"Failed to execute SQL command",details:{originalQuery:t}}}let er={"Show Tables":"SELECT name\n FROM sqlite_master \n WHERE type='table' \n AND name NOT LIKE 'sqlite_%';","Table Schema":"-- Table Schema\n-- Replace table_name with your table name\nPRAGMA table_info(table_name);","Table Indexes":"-- Table Indexes\n-- Replace table_name with your table name\nSELECT \n m.name as table_name,\n i.name as index_name,\n i.sql as index_definition\nFROM sqlite_master m\nJOIN sqlite_master i ON m.name = i.tbl_name\nWHERE m.type = 'table'\nAND i.type = 'index'\nAND m.name = 'table_name';","Foreign Keys":"-- Foreign Key Information\n-- Replace table_name with your table name\nPRAGMA foreign_key_list(table_name);","Table Data":"-- Table Data\n-- Replace table_name with your table name\nSELECT * FROM table_name LIMIT 100;","Row Count":"-- Row Count\n-- Replace table_name with your table name\nSELECT COUNT(*) as total_rows FROM table_name;","Column Stats":"-- Column Statistics\n-- Replace table_name and column_name with your values\nSELECT \n COUNT(*) as total_rows,\n COUNT(column_name) as non_null_count,\n COUNT(DISTINCT column_name) as unique_values,\n MIN(column_name) as min_value,\n MAX(column_name) as max_value\nFROM table_name;","Create Table":"CREATE TABLE IF NOT EXISTS new_table (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n name TEXT NOT NULL,\n email TEXT UNIQUE,\n status TEXT CHECK(status IN ('active', 'inactive')) DEFAULT 'active',\n created_at DATETIME DEFAULT CURRENT_TIMESTAMP\n);\n\n-- Create indexes\nCREATE INDEX idx_new_table_name ON new_table(name);\nCREATE UNIQUE INDEX idx_new_table_email ON new_table(email);","Create View":"-- Create a view\nCREATE VIEW view_name AS\nSELECT column1, column2\nFROM table_name\nWHERE condition;"},eo={"Schema Information":["Show Tables","Table Schema","Table Indexes","Foreign Keys"],"Data Queries":["Table Data","Row Count","Column Stats"],"Schema Modification":["Create Table","Create View"]};function ei(e){let{onSelectQuery:t}=e;return(0,s.jsxs)(R,{children:[(0,s.jsx)(E,{asChild:!0,children:(0,s.jsxs)(L,{variant:"outline",size:"sm",children:["Query Templates",(0,s.jsx)(c.A,{className:"ml-2 h-4 w-4"})]})}),(0,s.jsx)(T,{align:"start",className:"w-72",children:Object.entries(eo).map(e=>{let[a,n]=e;return(0,s.jsxs)(l.Fragment,{children:[(0,s.jsx)("div",{className:"px-2 py-1.5 text-sm font-semibold text-neutral-900 font-sans",children:a}),n.map(e=>(0,s.jsx)(A,{onClick:()=>t(e),className:"font-sans text-neutral-800",children:e},e))]},a)})})]})}function ec(e){let{initialQuery:t=""}=e,{executeQuery:a,refreshTables:n}=(0,_.t)(),[r,o]=(0,l.useState)(null),[i,c]=(0,l.useState)(!1),[d,u]=(0,l.useState)(300),[m,h]=(0,l.useState)(!1),f=(0,l.useRef)(null),p=(0,l.useRef)(null);(0,l.useEffect)(()=>{t&&f.current&&(f.current.setValue(t),f.current.focus())},[t]),(0,l.useEffect)(()=>{p.current&&(p.current.style.height="".concat(d,"px"))},[d]);let g=(0,l.useCallback)(e=>{e.preventDefault();let t=e.pageY,a=e=>{u(Math.min(Math.max(d+(e.pageY-t),100),.8*window.innerHeight))},s=()=>{document.removeEventListener("mousemove",a),document.removeEventListener("mouseup",s)};document.addEventListener("mousemove",a),document.addEventListener("mouseup",s)},[d]),b=async()=>{if(!f.current)return;let e=f.current.getValue().trim().replace(/;+$/,"");if(!e)return void o({success:!1,error:{message:"Query cannot be empty."}});try{var t,s,l,r,i;c(!0),o(null);let d=await a(e);/^(CREATE|DROP|ALTER)\s+TABLE/i.test(e)&&await n();let u=[];if(d.results){if(Array.isArray(d.results))if(d.results.length>0&&(null==(s=d.results[0])?void 0:s.columns)&&(null==(l=d.results[0])?void 0:l.values)){let e=d.results[0];u=e.values.map(t=>e.columns.reduce((e,a,s)=>(e[a]=t[s],e),{}))}else u=1===d.results.length&&d.results[0].results&&Array.isArray(d.results[0].results)?d.results[0].results:d.results;else if(d.results.columns&&d.results.values){let e=d.results;u=e.values.map(t=>e.columns.reduce((e,a,s)=>(e[a]=t[s],e),{}))}}else if(d.result&&d.result.results){if(Array.isArray(d.result.results))if(d.result.results.length>0&&(null==(r=d.result.results[0])?void 0:r.columns)&&(null==(i=d.result.results[0])?void 0:i.values)){let e=d.result.results[0];u=e.values.map(t=>e.columns.reduce((e,a,s)=>(e[a]=t[s],e),{}))}else u=1===d.result.results.length&&d.result.results[0].results&&Array.isArray(d.result.results[0].results)?d.result.results[0].results:d.result.results;else if(d.result.results.columns&&d.result.results.values){let e=d.result.results;u=e.values.map(t=>e.columns.reduce((e,a,s)=>(e[a]=t[s],e),{}))}}o({success:!0,results:u,target:(null==d||null==(t=d.execution)?void 0:t.target)||"local"})}catch(e){o({success:!1,error:el(e)||{message:"An unknown error occurred",details:{}}})}finally{c(!1)}};return(0,s.jsxs)("div",{className:"h-full flex flex-col bg-white text-neutral-900 font-sans",children:[(0,s.jsx)("div",{className:"px-4 py-2 bg-neutral-100 border-b flex items-center gap-4",children:(0,s.jsx)(ei,{onSelectQuery:e=>{if(f.current){let t=Object.keys(er).find(t=>t.toLowerCase()===e.toLowerCase());if(t){let e=er[t];f.current.setValue(e),f.current.focus()}else console.warn("Query template not found for: ".concat(e))}}})}),(0,s.jsx)("div",{ref:p,className:"relative pt-2 overflow-hidden",style:{height:"".concat(d,"px"),minHeight:"100px"},children:(0,s.jsx)(X.Ay,{height:"100%",language:"sql",options:{minimap:{enabled:!1},fontSize:14,wordWrap:"on",automaticLayout:!0,lineNumbers:"on",fontFamily:"ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace"},onMount:e=>{f.current=e,e.addCommand(monaco.KeyMod.CtrlCmd|monaco.KeyCode.Enter,()=>{b()})}})}),(0,s.jsx)("div",{className:"relative h-0.5 cursor-row-resize bg-neutral-200 group ".concat(m?"bg-neutral-300":"hover:bg-neutral-300"),onMouseDown:e=>{h(!0),g(e)},onMouseUp:()=>h(!1),children:(0,s.jsx)("div",{className:"absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 bg-neutral-900 rounded-sm px-0.5 \n ".concat(m?"visible":"invisible group-hover:visible"),children:(0,s.jsx)(G.A,{className:"h-3 w-3 text-white"})})}),(0,s.jsx)("div",{className:"px-4 py-3 bg-neutral-100 border-b font-sans",children:(0,s.jsxs)("div",{className:"flex justify-between items-center",children:[(0,s.jsx)("h3",{className:"text-sm font-medium",children:"Results"}),(0,s.jsxs)(L,{size:"sm",onClick:b,disabled:i,className:"flex items-center gap-1 text-sm font-medium",children:["Run",i?(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin"}):(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(Z.A,{className:"h-4 w-4"}),(0,s.jsx)($.A,{className:"h-4 w-4"})]})]})]})}),(0,s.jsx)("div",{className:"flex-1 overflow-auto p-4 bg-white",children:(()=>{var e;if(i)return(0,s.jsxs)("div",{className:"flex items-center text-sm text-neutral-700",children:[(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin mr-2 font-sans"}),"Running your query..."]});if(!r)return(0,s.jsx)("div",{className:"text-sm text-neutral-500 font-sans",children:"Run the query to see the results."});if(!r.success)return console.debug("SQLEditor - Rendering error:",r.error),(0,s.jsx)(et,{error:r.error});let t=r.results,a="remote"===r.target?"Remote":"Local";return t&&0!==t.length?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)("div",{className:"text-xs text-neutral-500 mb-2",children:["Executed on: ",a]}),(0,s.jsx)(ea,{query:(null==(e=f.current)?void 0:e.getValue())||"",results:t})]}):(0,s.jsxs)("div",{className:"text-sm text-neutral-900 font-sans",children:["Success (",a,"). No results to display."]})})()})]})}var ed=a(8162);let eu=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(ed.bL,{ref:t,className:(0,g.cn)("peer h-4 w-4 shrink-0 rounded-sm border border-primary shadow focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground",a),...l,children:(0,s.jsx)(ed.C1,{className:(0,g.cn)("flex items-center justify-center text-current"),children:(0,s.jsx)(S.A,{className:"h-4 w-4"})})})});eu.displayName=ed.bL.displayName;var em=a(7887),ex=a(2614);let eh=em.bL;em.YJ;let ef=em.WT,ep=l.forwardRef((e,t)=>{let{className:a,children:l,...n}=e;return(0,s.jsxs)(em.l9,{ref:t,className:(0,g.cn)("flex h-9 w-full items-center justify-between whitespace-nowrap rounded-md border border-input bg-transparent px-3 py-2 text-sm shadow-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-1 focus:ring-ring disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1",a),...n,children:[l,(0,s.jsx)(em.In,{asChild:!0,children:(0,s.jsx)(c.A,{className:"h-4 w-4 opacity-50"})})]})});ep.displayName=em.l9.displayName;let eg=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(em.PP,{ref:t,className:(0,g.cn)("flex cursor-default items-center justify-center py-1",a),...l,children:(0,s.jsx)(ex.A,{className:"h-4 w-4"})})});eg.displayName=em.PP.displayName;let eb=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(em.wn,{ref:t,className:(0,g.cn)("flex cursor-default items-center justify-center py-1",a),...l,children:(0,s.jsx)(c.A,{className:"h-4 w-4"})})});eb.displayName=em.wn.displayName;let ej=l.forwardRef((e,t)=>{let{className:a,children:l,position:n="popper",...r}=e;return(0,s.jsx)(em.ZL,{children:(0,s.jsxs)(em.UC,{ref:t,className:(0,g.cn)("relative z-50 max-h-96 min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2","popper"===n&&"data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",a),position:n,...r,children:[(0,s.jsx)(eg,{}),(0,s.jsx)(em.LM,{className:(0,g.cn)("p-1","popper"===n&&"h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]"),children:l}),(0,s.jsx)(eb,{})]})})});ej.displayName=em.UC.displayName,l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(em.JU,{ref:t,className:(0,g.cn)("px-2 py-1.5 text-sm font-semibold",a),...l})}).displayName=em.JU.displayName;let ev=l.forwardRef((e,t)=>{let{className:a,children:l,...n}=e;return(0,s.jsxs)(em.q7,{ref:t,className:(0,g.cn)("relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-2 pr-8 text-sm outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",a),...n,children:[(0,s.jsx)("span",{className:"absolute right-2 flex h-3.5 w-3.5 items-center justify-center",children:(0,s.jsx)(em.VF,{children:(0,s.jsx)(S.A,{className:"h-4 w-4"})})}),(0,s.jsx)(em.p4,{children:l})]})});ev.displayName=em.q7.displayName,l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(em.wv,{ref:t,className:(0,g.cn)("-mx-1 my-1 h-px bg-muted",a),...l})}).displayName=em.wv.displayName;var eN=a(5468),ew=a(8772),ey=a(6204),eC=a(3173),eS=a(7520);let ek=eS.Kq,eR=eS.bL,eE=eS.l9,eT=l.forwardRef((e,t)=>{let{className:a,sideOffset:l=4,...n}=e;return(0,s.jsx)(eS.ZL,{children:(0,s.jsx)(eS.UC,{ref:t,sideOffset:l,className:(0,g.cn)("z-50 overflow-hidden rounded-md bg-primary px-3 py-1.5 text-xs text-primary-foreground animate-in fade-in-0 zoom-in-95 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",a),...n})})});eT.displayName=eS.UC.displayName;var eA=a(3381);let e_=(0,l.memo)(e=>{let{onRefresh:t,disabled:a}=e,[n,r]=(0,l.useState)(!1),o=async()=>{if(!n&&!a){r(!0);try{await t()}catch(e){console.error("Refresh failed:",e)}finally{setTimeout(()=>{r(!1)},500)}}};return(0,s.jsx)(ek,{children:(0,s.jsxs)(eR,{children:[(0,s.jsx)(eE,{asChild:!0,children:(0,s.jsx)(L,{variant:"outline",size:"icon",onClick:o,disabled:n||a,className:"h-8",children:n?(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin"}):(0,s.jsx)(eA.A,{className:"h-4 w-4"})})}),(0,s.jsx)(eT,{children:(0,s.jsx)("p",{children:"Refresh table data"})})]})})});e_.displayName="RefreshButton";var eO=a(1715),eI=a(1374);let eL=(0,l.memo)(e=>{let{tableName:t,onSync:a}=e,[n,r]=(0,l.useState)(!1),{toast:o}=(0,eI.dj)();if("d1_migrations"!==t)return null;let i=async()=>{try{r(!0);let e=await fetch("/api/migrations/sync-files",{method:"POST"}),t=await e.json();if(!e.ok)throw Error(t.error||"Failed to sync migrations");let{added:s=[],removed:l=[]}=t.details||{};o({title:"Migrations Synced",description:"Added: ".concat(s.length,", Removed: ").concat(l.length)}),a&&await a()}catch(e){console.error("Error syncing migrations:",e),o({variant:"destructive",title:"Sync Failed",description:e.message})}finally{r(!1)}};return(0,s.jsx)(ek,{children:(0,s.jsxs)(eR,{children:[(0,s.jsx)(eE,{asChild:!0,children:(0,s.jsx)(L,{variant:"outline",size:"icon",onClick:i,disabled:n,className:"h-8",children:n?(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin"}):(0,s.jsx)(eO.A,{className:"h-4 w-4"})})}),(0,s.jsx)(eT,{children:(0,s.jsx)("p",{children:"Sync migration files with d1_migrations table"})})]})})});eL.displayName="SyncMigrationsButton";var eU=a(3489),eM=a(9680),eD=a(5584),eF=a(9963);let ez=(0,l.memo)(e=>{let{tableName:t,onDownloadStart:a,onDownloadComplete:n,disabled:r}=e,[o,i]=(0,l.useState)(!1),{toast:c}=(0,eI.dj)(),d=(e,t)=>{try{let a=new Blob([e],{type:"application/octet-stream"}),s=window.URL.createObjectURL(a),l=document.createElement("a");l.href=s,l.download=t,document.body.appendChild(l),l.click(),document.body.removeChild(l),window.URL.revokeObjectURL(s)}catch(e){throw console.error("Error during file download:",e),e}},u=async e=>{if(!r)try{i(!0),null==a||a();let s=await fetch("/api/tables/".concat(t,"?export=true&page=1&limit=999999")),l=await s.json();if(!s.ok)throw Error(l.error||"Failed to fetch data");let{data:r,schema:o}=l;if(!(null==r?void 0:r.length))return void c({title:"No Data",description:"The table is empty.",variant:"destructive"});let u=new Date().toISOString().split("T")[0],m="".concat(t,"_").concat(u,".").concat(e);switch(e){case"json":{let e=JSON.stringify(r,null,2);d(e,m);break}case"csv":{let e=Object.keys(r[0]),t=[e.join(","),...r.map(t=>e.map(e=>{let a=t[e];if(null==a)return"";if("string"==typeof a){let e=a.replace(/"/g,'""');return'"'.concat(e,'"')}return a}).join(","))].join("\n");d(t,m);break}case"sql":{let e="CREATE TABLE IF NOT EXISTS ".concat(t," (\n")+o.map(e=>{let t=[];return e.pk&&t.push("PRIMARY KEY"),e.notnull||t.push("NOT NULL")," ".concat(e.name," ").concat(e.type).concat(t.length?" ".concat(t.join(" ")):"")}).join(",\n")+"\n);\n\n",a=r.map(e=>{let a=Object.keys(e),s=a.map(t=>{let a=e[t];return null===a?"NULL":"string"==typeof a?"'".concat(a.replace(/'/g,"''"),"'"):a});return"INSERT INTO ".concat(t," (").concat(a.join(", "),")\nVALUES (").concat(s.join(", "),");")}).join("\n");d(e+a,m)}}c({title:"Download Complete",description:"".concat(r.length," rows exported as ").concat(e.toUpperCase(),".")}),null==n||n()}catch(e){console.error("Download error:",e),c({variant:"destructive",title:"Download Failed",description:e.message})}finally{i(!1)}};return(0,s.jsxs)(R,{children:[(0,s.jsx)(ek,{children:(0,s.jsxs)(eR,{children:[(0,s.jsx)(eE,{asChild:!0,children:(0,s.jsx)(E,{asChild:!0,children:(0,s.jsx)(L,{variant:"outline",size:"icon",className:"h-8",disabled:o||r,children:o?(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin"}):(0,s.jsx)(eU.A,{className:"h-4 w-4"})})})}),(0,s.jsx)(eT,{children:(0,s.jsx)("p",{children:"Download table data"})})]})}),(0,s.jsxs)(T,{align:"end",children:[(0,s.jsxs)(A,{onClick:()=>u("json"),disabled:r,children:[(0,s.jsx)(eM.A,{className:"mr-2 h-4 w-4"}),(0,s.jsx)("span",{children:"Download JSON"})]}),(0,s.jsxs)(A,{onClick:()=>u("csv"),disabled:r,children:[(0,s.jsx)(eD.A,{className:"mr-2 h-4 w-4"}),(0,s.jsx)("span",{children:"Download CSV"})]}),(0,s.jsxs)(A,{onClick:()=>u("sql"),disabled:r,children:[(0,s.jsx)(eF.A,{className:"mr-2 h-4 w-4"}),(0,s.jsx)("span",{children:"Download SQL"})]})]})]})});ez.displayName="DownloadButton";var eP=a(3527);let eB=function(e){let{schema:t=[],visibleColumns:a=[],onColumnToggle:n}=e,[r,o]=(0,l.useState)("");(0,l.useEffect)(()=>{if(!t||0===t.length)return;let e=t.map(e=>e.name);0===a.length&&e.forEach(e=>n(e))},[t,a.length,n]);let i=t.filter(e=>e.name.toLowerCase().includes(r.toLowerCase())),c=i.map(e=>e.name),d=c.length>0&&c.every(e=>a.includes(e));return(0,s.jsxs)(R,{children:[(0,s.jsx)(E,{asChild:!0,children:(0,s.jsxs)(L,{size:"sm",variant:"outline",className:"font-semibold",children:[(0,s.jsx)(eP.A,{className:"h-4 w-4"}),"Columns (",a.length,")"]})}),(0,s.jsxs)(T,{align:"end",className:"w-64 font-sans",children:[(0,s.jsx)("div",{className:"p-2",children:(0,s.jsx)(b,{placeholder:"Search columns...",value:r,onChange:e=>o(e.target.value),className:"h-8"})}),(0,s.jsxs)("div",{className:"flex items-center space-x-2 px-2 py-1.5 hover:bg-accent rounded-md",children:[(0,s.jsx)(eu,{id:"select-all-columns",checked:d,onCheckedChange:e=>{e?i.forEach(e=>{a.includes(e.name)||n(e.name)}):i.forEach(e=>{a.includes(e.name)&&n(e.name)})},className:"shrink-0"}),(0,s.jsxs)("label",{htmlFor:"select-all-columns",className:"flex-1 cursor-pointer text-sm font-medium",children:["Select All ",r&&"(Filtered)"]})]}),(0,s.jsx)("div",{className:"max-h-[300px] overflow-y-auto",children:i.map(e=>(0,s.jsxs)("div",{className:"flex items-start space-x-2 px-2 py-1.5 hover:bg-accent rounded-md",children:[(0,s.jsx)(eu,{id:"column-".concat(e.name),checked:a.includes(e.name),onCheckedChange:()=>{n(e.name)},className:"mt-1"}),(0,s.jsxs)("label",{htmlFor:"column-".concat(e.name),className:"flex-1 cursor-pointer",children:[(0,s.jsx)("div",{className:"text-sm font-medium",children:e.name}),(0,s.jsx)("div",{className:"text-xs text-muted-foreground",children:e.type.toLowerCase()})]})]},e.name))})]})]})},eW=(0,l.memo)(e=>{let{onAdd:t,onDelete:a,onImport:l,selectedCount:n,disabled:r,readOnly:o,newRecordsCount:i,schema:c,visibleColumns:d,onColumnToggle:u,showFilters:m,onToggleFilters:x,activeFilterCount:h}=e;return(0,s.jsxs)("div",{className:"flex items-center space-x-2",children:[(0,s.jsxs)(L,{size:"sm",variant:m?"default":"outline",className:"font-semibold gap-1",onClick:x,children:[(0,s.jsx)(eN.A,{className:"h-4 w-4"}),"Filters",h>0&&(0,s.jsx)("span",{className:"ml-0.5 text-[10px] font-bold bg-white text-neutral-800 px-1 py-0 rounded-full",children:h})]}),(0,s.jsx)(eB,{schema:c,visibleColumns:d,onColumnToggle:u}),(0,s.jsx)("div",{className:"h-4 border-r mx-2"}),(0,s.jsx)(L,{size:"sm",variant:"default",className:"font-semibold",onClick:t,disabled:o,children:"Add record"}),(0,s.jsx)(ek,{children:(0,s.jsxs)(eR,{children:[(0,s.jsx)(eE,{asChild:!0,children:(0,s.jsxs)(L,{size:"sm",variant:"outline",className:"font-semibold",onClick:l,disabled:r||o,children:[(0,s.jsx)(ew.A,{className:"h-4 w-4"}),"Import"]})}),(0,s.jsx)(eT,{children:(0,s.jsx)("p",{children:"Import CSV or JSON"})})]})}),n>0&&(0,s.jsxs)(L,{size:"sm",className:"font-semibold",onClick:a,variant:"destructive",disabled:r||i>0||o,children:["Delete ",n," ",1===n?"record":"records"]})]})}),eY=(0,l.memo)(e=>{let{totalRows:t}=e;return(0,s.jsxs)("span",{className:"text-sm text-neutral-600",children:[t," ",1===t?"row":"rows"]})}),eq=(0,l.memo)(e=>{let{currentPage:t,totalPages:a,onPageChange:l,disabled:n,newRecordsCount:r}=e;return(0,s.jsxs)("div",{className:"flex items-center space-x-2",children:[(0,s.jsx)(L,{variant:"ghost",size:"icon",onClick:()=>{console.log("Pagination: navigating to previous page",t-1),l(t-1)},disabled:1===t||n||r>0,className:"h-8",children:(0,s.jsx)(ey.A,{className:"h-4 w-4"})}),(0,s.jsxs)("span",{className:"text-sm",children:["Page ",t," of ",a]}),(0,s.jsx)(L,{variant:"ghost",size:"icon",onClick:()=>{console.log("Pagination: navigating to next page",t+1),l(t+1)},disabled:t===a||n||r>0,className:"h-8",children:(0,s.jsx)(eC.A,{className:"h-4 w-4"})})]})}),eH=(0,l.memo)(e=>{let{value:t,onValueChange:a,disabled:l,newRecordsCount:n}=e;return(0,s.jsxs)(eh,{value:t.toString(),onValueChange:a,disabled:l||n>0,children:[(0,s.jsx)(ep,{className:"w-20 h-8",children:(0,s.jsx)(ef,{})}),(0,s.jsxs)(ej,{children:[(0,s.jsx)(ev,{value:"50",children:"50"}),(0,s.jsx)(ev,{value:"100",children:"100"}),(0,s.jsx)(ev,{value:"250",children:"250"})]})]})}),eK=(0,l.memo)(e=>{let{onSave:t,onDiscard:a,totalChanges:l,isSaving:n}=e;return(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)(L,{size:"sm",variant:"ghost",className:"font-semibold",onClick:a,disabled:n,children:"Discard changes"}),(0,s.jsx)(L,{size:"sm",className:"font-semibold bg-green-600 hover:bg-green-700",variant:"default",onClick:t,disabled:n,children:n?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin"}),"Saving..."]}):"Save ".concat(l," ").concat(1===l?"change":"changes")})]})}),eV=e=>{let{selectedCount:t=0,onAdd:a,onDelete:l,onImport:n,onRefresh:r,totalRows:o=0,currentPage:i=1,totalPages:c=1,onPageChange:d,rowsPerPage:u=50,onRowsPerPageChange:m,tableName:x,onDownloadStart:h,onDownloadComplete:f,disabled:p=!1,readOnly:g=!1,editCount:b=0,newRecordsCount:j=0,onSaveChanges:v,onDiscardChanges:N,isSaving:w=!1,schema:y,visibleColumns:C,onColumnToggle:S,showFilters:k=!1,onToggleFilters:R,activeFilterCount:E=0}=e,T=b+j;return(0,s.jsx)("div",{className:"flex items-center justify-between p-3 border-b bg-background font-sans",children:(0,s.jsxs)("div",{className:"flex items-center justify-between w-full",children:[(0,s.jsx)(eW,{onAdd:a,onDelete:l,onImport:n,selectedCount:t,disabled:p||w,readOnly:g,newRecordsCount:j,schema:y,visibleColumns:C,onColumnToggle:S,showFilters:k,onToggleFilters:R,activeFilterCount:E}),(0,s.jsx)("div",{className:"flex items-center gap-4",children:T>0&&!g?(0,s.jsx)(eK,{onSave:v,onDiscard:N,totalChanges:T,isSaving:w}):(0,s.jsxs)("div",{className:"flex items-center space-x-2",children:[(0,s.jsx)(eY,{totalRows:o}),(0,s.jsx)(eq,{currentPage:i,totalPages:c,onPageChange:d,disabled:p,newRecordsCount:j}),(0,s.jsx)(eH,{value:u,onValueChange:e=>{m(parseInt(e,10)),d(1)},disabled:p,newRecordsCount:j}),(0,s.jsx)(e_,{onRefresh:r,disabled:p}),(0,s.jsx)(eL,{tableName:x,onSync:r,disabled:p}),(0,s.jsx)(ez,{tableName:x,onDownloadStart:h,onDownloadComplete:f,disabled:p})]})})]})})};eV.displayName="TableToolbar";let eJ=(0,l.memo)(eV);function eQ(e){let{value:t,row:a,column:n,schema:r,onChange:o,onStartEdit:i,isEditing:c,fkInfo:d,onShowRelation:u}=e,m="string"==typeof t?t.replace(/^'|'$/g,""):t,[x,h]=(0,l.useState)(m),[f,p]=(0,l.useState)("auto"),g=(0,l.useRef)(null),j=(0,l.useRef)(null),v=r.find(e=>e.name===n),N=(0,l.useMemo)(()=>(null==v?void 0:v.check)||null,[v]),w=(0,l.useMemo)(()=>{var e;let t=(null==v||null==(e=v.type)?void 0:e.toLowerCase())||"";return"boolean"===t||"bool"===t||"integer"===t&&n.toLowerCase().startsWith("is_")},[v,n]);(0,l.useEffect)(()=>{h(m)},[m]),(0,l.useEffect)(()=>{if(g.current&&!c){let e=g.current.offsetWidth;p("".concat(e,"px"))}},[c]),(0,l.useEffect)(()=>{c&&j.current&&(j.current.focus(),j.current.select())},[c]),null==v||v.pk;let y=(0,l.useCallback)(e=>{let t=w?"true"===e?1:"false"===e?0:e:e;h(e),null==o||o(t)},[o,w]),C=(0,l.useCallback)(()=>{!c&&i&&i(m)},[c,i,m]),S="border-x border-neutral-200 px-3 py-2.5 text-xs font-mono whitespace-nowrap overflow-hidden bg-white",k=(0,l.useMemo)(()=>null==m?"null":w?1===m||"1"===m||"true"===m?"true":"false":m.toString(),[m,w]);return c?w?(0,s.jsx)(Y,{className:"".concat(S," p-0"),style:{width:f},children:(0,s.jsx)("div",{className:"w-full h-full",children:(0,s.jsxs)(eh,{value:k,onValueChange:y,defaultOpen:c,children:[(0,s.jsx)(ep,{className:"w-full h-9 min-w-0 rounded-none border-0 bg-orange-50 text-orange-900",children:(0,s.jsx)(ef,{placeholder:"Select value"})}),(0,s.jsxs)(ej,{className:"min-w-[8rem]",children:[(0,s.jsx)(ev,{value:"true",children:"true"}),(0,s.jsx)(ev,{value:"false",children:"false"})]})]})})}):(null==N?void 0:N.length)>0?(0,s.jsx)(Y,{className:"".concat(S," p-0"),style:{width:f},children:(0,s.jsx)("div",{className:"w-full h-full",children:(0,s.jsxs)(eh,{value:null!=x?x:N[0],onValueChange:y,defaultOpen:c,children:[(0,s.jsx)(ep,{className:"w-full h-9 min-w-0 rounded-none border-0 bg-orange-50 text-orange-900",children:(0,s.jsx)(ef,{placeholder:"Select ".concat(n)})}),(0,s.jsx)(ej,{className:"min-w-[8rem]",children:N.map(e=>(0,s.jsx)(ev,{value:e,children:e},e))})]})})}):(0,s.jsx)(Y,{className:"".concat(S," p-0"),style:{width:f},children:(0,s.jsx)(b,{ref:j,value:null!=x?x:"",onChange:e=>y(e.target.value),className:"w-full min-w-0 rounded-none border-0 bg-orange-50 text-orange-900"})}):(0,s.jsxs)(Y,{ref:g,className:"".concat(S," relative group cursor-pointer hover:bg-muted/50"),onClick:C,children:[k,d&&(0,s.jsx)("button",{className:"absolute right-0.5 top-1/2 -translate-y-1/2 opacity-0 group-hover:opacity-100 rounded p-0.5 bg-white border hover:bg-blue-50 transition-opacity z-10",onClick:e=>{e.stopPropagation(),null==u||u()},title:"Show related ".concat(d.table),children:(0,s.jsx)(eC.A,{className:"h-3 w-3 text-blue-500"})})]})}var eX=a(3630);function eG(e){return"string"==typeof e?e.replace(/^'|'$/g,""):e}function eZ(e){let{schema:t,onRemove:a,onChange:n,showCheckbox:r=!1,initialData:o={}}=e,[i,c]=(0,l.useState)(()=>t.reduce((e,t)=>{var a;let s=null!=(a=o[t.name])?a:null;if(s=eG(s),1===t.pk&&(null===s||""===s))s="INTEGER"===t.type.toUpperCase()?"[Auto-generated INT]":"[Auto-generated]";else if(null===s){let e=t.dflt_value&&"null"!==t.dflt_value?t.dflt_value:null;if(e){let a=eG(e);s=a.includes("randomblob")||a.includes("lower(hex(")?"[Auto-generated UUID]":"INTEGER"===t.type.toUpperCase()&&1===t.pk&&(a.includes("AUTOINCREMENT")||a.includes("AUTO_INCREMENT")||a.includes("nextval")||""===a||"0"===a)?"[Auto-generated INT]":a}else"INTEGER"===t.type.toUpperCase()?s="":"TEXT"===t.type.toUpperCase()&&(s="")}return e[t.name]=s,e},{})),d=(0,l.useRef)(!1);(0,l.useEffect)(()=>{d.current||(d.current=!0,null==n||n(i))},[]);let u=(e,t)=>{let a={...i,[e]:t};c(a),null==n||n(a)};return(0,s.jsxs)(B,{className:"bg-orange-50/50",children:[(0,s.jsx)(Y,{className:"w-10 p-0",children:(0,s.jsx)("div",{className:"pl-1 flex items-center",children:r?(0,s.jsx)(eu,{disabled:!0}):(0,s.jsx)(L,{variant:"ghost",size:"icon",className:"h-8 w-8 text-orange-700 hover:text-orange-800",onClick:a,children:(0,s.jsx)(eX.A,{className:"h-4 w-4"})})})}),t.map(e=>{var t,a;let l=1===e.pk,n=e.check||null;if((e=>{var t;let a=(null==(t=e.type)?void 0:t.toLowerCase())||"";return"boolean"===a||"bool"===a||"integer"===a&&e.name.toLowerCase().startsWith("is_")})(e)){let t="false";if(null!==e.dflt_value&&void 0!==e.dflt_value){let a=e.dflt_value.toString().toLowerCase();t="1"===a||"true"===a||"'true'"===a||'"true"'===a?"true":"false"}let a=null!==i[e.name]&&void 0!==i[e.name]?1===i[e.name]||!0===i[e.name]||"true"===i[e.name]?"true":"false":t;return(0,s.jsx)(Y,{className:"border-x border-neutral-200 p-0",children:(0,s.jsx)("div",{className:"w-full",children:(0,s.jsxs)(eh,{value:a,onValueChange:t=>u(e.name,+("true"===t)),children:[(0,s.jsx)(ep,{className:"w-full h-9 min-w-0 rounded-none border-0 bg-orange-50 text-orange-900",title:"Default: ".concat(t),children:(0,s.jsx)(ef,{placeholder:"Select ".concat(e.name)})}),(0,s.jsxs)(ej,{className:"min-w-[8rem]",children:[(0,s.jsx)(ev,{value:"true",children:"true"}),(0,s.jsx)(ev,{value:"false",children:"false"})]})]})})},e.name)}if((null==n?void 0:n.length)>0)return(0,s.jsx)(Y,{className:"border-x border-neutral-200 p-0",children:(0,s.jsx)("div",{className:"w-full",children:(0,s.jsxs)(eh,{value:null!=(t=i[e.name])?t:n[0],onValueChange:t=>u(e.name,t),children:[(0,s.jsx)(ep,{className:"w-full h-9 min-w-0 rounded-none border-0 bg-orange-50 text-orange-900",children:(0,s.jsx)(ef,{placeholder:"Select ".concat(e.name)})}),(0,s.jsx)(ej,{className:"min-w-[8rem]",children:n.map(e=>(0,s.jsx)(ev,{value:e,children:e},e))})]})})},e.name);let r=l&&("[Auto-generated INT]"===i[e.name]||"[Auto-generated UUID]"===i[e.name]||"[Auto-generated]"===i[e.name]);return(0,s.jsx)(Y,{className:"border-x border-neutral-200 p-0",children:(0,s.jsx)(b,{value:r?"":null!=(a=i[e.name])?a:"",onChange:t=>u(e.name,t.target.value),disabled:r,className:"w-full min-w-0 rounded-none border-0 px-2 bg-orange-50 text-orange-900 ".concat(r?"opacity-50":""),placeholder:r?"Auto-generated":"Enter ".concat(e.name)})},e.name)})]})}var e$=a(9511);let e0=e$.bL;e$.l9;let e1=e$.ZL;e$.bm;let e2=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(e$.hJ,{ref:t,className:(0,g.cn)("fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",a),...l})});e2.displayName=e$.hJ.displayName;let e5=l.forwardRef((e,t)=>{let{className:a,children:l,...n}=e;return(0,s.jsxs)(e1,{children:[(0,s.jsx)(e2,{}),(0,s.jsxs)(e$.UC,{ref:t,className:(0,g.cn)("fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg",a),...n,children:[l,(0,s.jsxs)(e$.bm,{className:"absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground",children:[(0,s.jsx)(q.A,{className:"h-4 w-4"}),(0,s.jsx)("span",{className:"sr-only",children:"Close"})]})]})]})});e5.displayName=e$.UC.displayName;let e4=e=>{let{className:t,...a}=e;return(0,s.jsx)("div",{className:(0,g.cn)("flex flex-col space-y-1.5 text-center sm:text-left",t),...a})};e4.displayName="DialogHeader";let e3=e=>{let{className:t,...a}=e;return(0,s.jsx)("div",{className:(0,g.cn)("flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",t),...a})};e3.displayName="DialogFooter";let e6=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(e$.hE,{ref:t,className:(0,g.cn)("text-lg font-semibold leading-none tracking-tight",a),...l})});e6.displayName=e$.hE.displayName;let e8=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(e$.VY,{ref:t,className:(0,g.cn)("text-sm text-muted-foreground",a),...l})});e8.displayName=e$.VY.displayName;let e9=function(e){let{isOpen:t,onClose:a,onConfirm:l,selectedCount:n,tableName:r,isDeleting:o,error:c}=e;return(0,s.jsx)(e0,{open:t,onOpenChange:a,children:(0,s.jsxs)(e5,{className:"sm:max-w-md font-sans",children:[(0,s.jsxs)(e4,{children:[(0,s.jsxs)(e6,{className:"mb-2",children:["Delete ",n," ",1===n?"Record":"Records","?"]}),(0,s.jsxs)(e8,{className:"text-neutral-600 font-medium",children:["You're about to delete ",n," ",1===n?"record":"records"," from ",(0,s.jsx)("span",{className:"font-sans font-bold text-neutral-900",children:r}),"."]})]}),c&&(0,s.jsxs)(N,{variant:"destructive",children:[(0,s.jsx)(i.A,{className:"h-4 w-4"}),(0,s.jsx)(w,{children:c})]}),(0,s.jsxs)(e3,{className:"flex gap-1 mt-2 sm:justify-end font-sans",children:[(0,s.jsx)(L,{variant:"outline",onClick:a,disabled:o,children:"Cancel"}),(0,s.jsx)(L,{variant:"destructive",onClick:l,disabled:o,children:o?"Deleting...":"Delete"})]})]})})};function e7(e){var t;let{isOpen:a,onClose:l,error:n}=e;if(!n||!a)return null;let{message:r,icon:o}=n?{message:n.message,icon:i.A}:{message:"An unknown error occurred.",icon:i.A};return(0,s.jsx)(e0,{open:a,onOpenChange:l,children:(0,s.jsxs)(e5,{className:"sm:max-w-md font-sans",children:[(0,s.jsx)(e4,{children:(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)(o,{className:"h-4 w-4 text-red-500"}),(0,s.jsx)(e6,{children:"Error"})]})}),(0,s.jsxs)("div",{className:"space-y-4",children:[(0,s.jsx)("p",{className:"bg-neutral-100 p-2 rounded-md text-neutral-900",children:r}),(null==(t=n.details)?void 0:t.suggestions)&&(0,s.jsxs)("div",{className:"text-sm space-y-1",children:[(0,s.jsx)("p",{className:"font-medium",children:"Suggestions:"}),(0,s.jsx)("ul",{className:"list-disc pl-4 space-y-1",children:n.details.suggestions.map((e,t)=>(0,s.jsx)("li",{children:e},t))})]})]}),(0,s.jsx)("div",{className:"flex justify-end gap-2 mt-4",children:(0,s.jsx)(L,{variant:"default",onClick:l,children:"Got it"})})]})})}var te=a(984);function tt(e){var t;let{open:a,onClose:n,tableName:r,schema:o,onImportComplete:c}=e,{currentDatabase:d}=(0,_.t)(),{toast:u}=(0,eI.dj)(),m=(0,l.useRef)(null),[x,h]=(0,l.useState)(null),[f,p]=(0,l.useState)(""),[g,b]=(0,l.useState)(null),[j,v]=(0,l.useState)(!1),[y,C]=(0,l.useState)(null),S=o.map(e=>e.name),k=(0,l.useCallback)(()=>{h(null),p(""),b(null),C(null),m.current&&(m.current.value="")},[]),R=(0,l.useCallback)(()=>{k(),n()},[k,n]),E=(0,l.useCallback)(async e=>{var t;let a=null==(t=e.target.files)?void 0:t[0];if(a){b(null),C(null),p(a.name);try{let e,t=await a.text();if(a.name.endsWith(".json")){let a=JSON.parse(t);e=Array.isArray(a)?a:[a]}else if(a.name.endsWith(".csv"))e=function(e){let t=e.trim().split("\n");if(t.length<2)return[];let a=t[0].split(",").map(e=>e.trim().replace(/^"|"$/g,""));return t.slice(1).map(e=>{let t=[],s="",l=!1;for(let a=0;a<e.length;a++){let n=e[a];'"'===n?l&&'"'===e[a+1]?(s+='"',a++):l=!l:","!==n||l?s+=n:(t.push(s.trim()),s="")}t.push(s.trim());let n={};return a.forEach((e,a)=>{var s;n[e]=null!=(s=t[a])?s:""}),n})}(t);else throw Error("Unsupported file type. Use .json or .csv");if(0===e.length)throw Error("File contains no data rows");h(e)}catch(e){b(e.message),h(null)}}},[]),T=(0,l.useCallback)(async()=>{if(null==x?void 0:x.length){v(!0);try{let e=null==d?void 0:d.database_name,t=await fetch("/api/tables/".concat(r,"/import"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({rows:x,...e&&{db:e}})}),a=await t.json();if(!t.ok||!a.success)throw Error(a.error||"Import failed");C(a),u({title:"Import Complete",description:"".concat(a.imported," row").concat(1!==a.imported?"s":""," imported.")}),null==c||c()}catch(e){b(e.message)}finally{v(!1)}}},[x,r,d,u,c]),A=x?Object.keys(x[0]):[],O=A.filter(e=>S.includes(e)),I=A.filter(e=>!S.includes(e)),U=null!=(t=null==x?void 0:x.slice(0,5))?t:[];return(0,s.jsx)(e0,{open:a,onOpenChange:e=>!e&&R(),children:(0,s.jsxs)(e5,{className:"max-w-3xl max-h-[85vh] flex flex-col",children:[(0,s.jsx)(e4,{children:(0,s.jsxs)(e6,{children:["Import data into ",(0,s.jsx)("span",{className:"font-mono",children:r})]})}),(0,s.jsxs)("div",{className:"flex-1 overflow-y-auto space-y-4 py-2",children:[!x&&!y&&(0,s.jsxs)("div",{className:"border-2 border-dashed rounded-lg p-8 text-center cursor-pointer hover:bg-muted/50 transition-colors",onClick:()=>{var e;return null==(e=m.current)?void 0:e.click()},children:[(0,s.jsx)(ew.A,{className:"h-8 w-8 mx-auto mb-2 text-muted-foreground"}),(0,s.jsx)("p",{className:"text-sm font-medium",children:"Click to select a file"}),(0,s.jsx)("p",{className:"text-xs text-muted-foreground mt-1",children:"Supports .csv and .json"}),(0,s.jsx)("input",{ref:m,type:"file",accept:".csv,.json",className:"hidden",onChange:E})]}),g&&(0,s.jsxs)(N,{variant:"destructive",children:[(0,s.jsx)(i.A,{className:"h-4 w-4"}),(0,s.jsx)(w,{children:g})]}),y&&(0,s.jsxs)(N,{children:[(0,s.jsx)(te.A,{className:"h-4 w-4"}),(0,s.jsxs)(w,{children:["Imported ",y.imported," row",1!==y.imported?"s":"",".",y.failed>0&&" ".concat(y.failed," failed.")]})]}),x&&!y&&(0,s.jsxs)("div",{className:"space-y-3",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2 text-sm",children:[f.endsWith(".json")?(0,s.jsx)(eM.A,{className:"h-4 w-4"}):(0,s.jsx)(eD.A,{className:"h-4 w-4"}),(0,s.jsx)("span",{className:"font-medium",children:f}),(0,s.jsxs)("span",{className:"text-muted-foreground",children:["— ",x.length," rows"]})]}),(0,s.jsx)(L,{variant:"ghost",size:"sm",onClick:k,children:"Change file"})]}),I.length>0&&(0,s.jsxs)(N,{children:[(0,s.jsx)(i.A,{className:"h-4 w-4"}),(0,s.jsxs)(w,{className:"text-xs",children:["Columns not in table schema (will be ignored):"," ",(0,s.jsx)("span",{className:"font-mono",children:I.join(", ")})]})]}),(0,s.jsxs)("div",{children:[(0,s.jsxs)("p",{className:"text-xs text-muted-foreground mb-1",children:["Preview (first ",Math.min(5,x.length)," of ",x.length," rows)"]}),(0,s.jsx)("div",{className:"border rounded overflow-auto max-h-48",children:(0,s.jsxs)(F,{children:[(0,s.jsx)(z,{children:(0,s.jsx)(B,{children:O.map(e=>(0,s.jsx)(W,{className:"text-xs whitespace-nowrap py-1 px-2",children:e},e))})}),(0,s.jsx)(P,{children:U.map((e,t)=>(0,s.jsx)(B,{children:O.map(t=>(0,s.jsx)(Y,{className:"text-xs py-1 px-2 max-w-32 truncate",children:null===e[t]||void 0===e[t]?(0,s.jsx)("span",{className:"text-muted-foreground italic",children:"null"}):String(e[t])},t))},t))})]})})]})]})]}),(0,s.jsxs)(e3,{children:[(0,s.jsx)(L,{variant:"ghost",onClick:R,disabled:j,children:y?"Close":"Cancel"}),x&&!y&&(0,s.jsx)(L,{onClick:T,disabled:j||0===O.length,children:j?"Importing…":"Import ".concat(x.length," row").concat(1!==x.length?"s":"")})]})]})})}let ta=e=>{let{children:t,width:a,onResize:n,className:r=""}=e,o=(0,l.useRef)(null),i=(0,l.useCallback)(e=>{var t,s,l,r;e.preventDefault(),e.stopPropagation();let i=e.clientX,c=null!=(l=null==(t=o.current)?void 0:t.offsetWidth)?l:a||150,d=null==(s=o.current)?void 0:s.closest("table"),u=null!=(r=null==d?void 0:d.offsetWidth)?r:0;d&&(d.style.width="".concat(u,"px"));let m=c,x=e=>{let t=Math.max(c+(e.clientX-i),80),a=t-m;m=t,o.current&&(o.current.style.width="".concat(t,"px")),d&&0!==a&&(d.style.width="".concat(parseFloat(d.style.width)+a,"px"))},h=e=>{document.removeEventListener("mousemove",x),document.removeEventListener("mouseup",h),document.body.style.cursor="",document.body.style.userSelect="";let t=Math.max(c+(e.clientX-i),80);d&&(d.style.width=""),n(t)};document.body.style.cursor="col-resize",document.body.style.userSelect="none",document.addEventListener("mousemove",x),document.addEventListener("mouseup",h)},[a,n]);return(0,s.jsxs)("th",{ref:o,className:"relative overflow-hidden ".concat(r),style:{width:"".concat(a,"px")},children:[t,(0,s.jsx)("div",{className:"absolute top-0 right-0 h-full w-1.5 cursor-col-resize hover:bg-blue-400/40 z-10",onMouseDown:i,onDoubleClick:e=>{e.preventDefault(),e.stopPropagation()}})]})};var ts=a(2240),tl=a(1165),tn=a(43);function tr(e){let{column:t,sortConfig:a,onSort:l,children:n}=e,r=a[t];return(0,s.jsx)("div",{className:"flex items-center w-full",children:(0,s.jsxs)(L,{variant:"ghost",size:"sm",className:"h-8 hover:bg-neutral-100 w-full justify-between",onClick:()=>{l(t,r?"asc"===r?"desc":null:"asc")},children:[(0,s.jsx)("span",{className:"flex items-center gap-1 font-mono",children:n}),(0,s.jsxs)("span",{className:"shrink-0",children:["asc"===r&&(0,s.jsx)(ts.A,{className:"h-4 w-4"}),"desc"===r&&(0,s.jsx)(tl.A,{className:"h-4 w-4"}),!r&&(0,s.jsx)(tn.A,{className:"h-4 w-4"})]})]})})}function to(e){let{relatedTable:t,relatedKey:a,value:n,colSpan:r,onClose:o}=e,{executeQuery:i}=(0,_.t)(),[c,d]=(0,l.useState)(null),[u,m]=(0,l.useState)(!0),[h,f]=(0,l.useState)(!1);(0,l.useEffect)(()=>{if(!t||!a||null==n){m(!1),f(!0);return}let e=!1;m(!0),f(!1),d(null);let s=String(n).replace(/'/g,"''");return i('SELECT * FROM "'.concat(t,'" WHERE "').concat(a,"\" = '").concat(s,"' LIMIT 1")).then(t=>{var a,s;if(e)return;let l=(null==t||null==(s=t.results)||null==(a=s[0])?void 0:a.results)||[];l.length>0?d(l[0]):f(!0)}).catch(()=>{e||f(!0)}).finally(()=>{e||m(!1)}),()=>{e=!0}},[t,a,n,i]);let p=c?Object.keys(c):[];return(0,s.jsx)(B,{className:"bg-neutral-50 hover:bg-neutral-50",children:(0,s.jsx)(Y,{colSpan:r,className:"py-2 px-4 border-b border-neutral-200",children:(0,s.jsxs)("div",{className:"flex items-start justify-between gap-3",children:[(0,s.jsxs)("div",{className:"flex flex-col gap-1.5 flex-1 min-w-0",children:[(0,s.jsx)("span",{className:"text-[11px] font-semibold px-2 py-0.5 self-start rounded-full bg-neutral-200 text-neutral-600 font-mono",children:t}),u&&(0,s.jsx)(x.A,{className:"h-3.5 w-3.5 animate-spin text-neutral-400"}),!u&&h&&(0,s.jsx)("span",{className:"text-xs text-muted-foreground italic",children:"No related record found"}),!u&&c&&(0,s.jsx)("div",{className:"overflow-x-auto [&::-webkit-scrollbar]:hidden",style:{scrollbarWidth:"none"},children:(0,s.jsxs)("table",{className:"text-[11px] font-mono border border-neutral-200 rounded-md overflow-hidden",children:[(0,s.jsx)("thead",{children:(0,s.jsx)("tr",{className:"bg-neutral-100 border-b border-neutral-200",children:p.map(e=>(0,s.jsx)("th",{className:"px-3 py-1 text-left text-muted-foreground font-medium border-r border-neutral-200 last:border-r-0 whitespace-nowrap",children:e},e))})}),(0,s.jsx)("tbody",{children:(0,s.jsx)("tr",{className:"bg-white",children:p.map(e=>(0,s.jsx)("td",{className:"px-3 py-1.5 border-r border-neutral-200 last:border-r-0 whitespace-nowrap max-w-[220px] truncate",children:null==c[e]?(0,s.jsx)("span",{className:"text-neutral-400 italic",children:"null"}):String(c[e])},e))})})]})})]}),(0,s.jsx)("button",{onClick:o,className:"shrink-0 rounded p-0.5 hover:bg-neutral-200 text-muted-foreground hover:text-neutral-700 transition-colors mt-0.5",title:"Close",children:(0,s.jsx)(q.A,{className:"h-3.5 w-3.5"})})]})})})}var ti=a(2265);let tc=[{value:"equals",label:"= equals"},{value:"not_equals",label:"≠ not equals"},{value:"contains",label:"~ contains"},{value:"starts_with",label:"^ starts with"},{value:"ends_with",label:"$ ends with"},{value:"gt",label:"> greater than"},{value:"gte",label:"≥ greater or equal"},{value:"lt",label:"< less than"},{value:"lte",label:"≤ less or equal"},{value:"is_null",label:"is null"},{value:"is_not_null",label:"is not null"}],td=new Set(["is_null","is_not_null"]),tu=1;function tm(e){let t=e.filter(e=>{var t;return!!e.column&&!!e.operator&&(!!td.has(e.operator)||!!String(null!=(t=e.value)?t:"").trim())}).map(e=>{let t='"'.concat(e.column,'"'),a=e=>String(e).replace(/'/g,"''");switch(e.operator){case"equals":return"".concat(t," LIKE '").concat(a(e.value),"'");case"not_equals":return"".concat(t," NOT LIKE '").concat(a(e.value),"'");case"contains":return"".concat(t," LIKE '%").concat(a(e.value),"%'");case"starts_with":return"".concat(t," LIKE '").concat(a(e.value),"%'");case"ends_with":return"".concat(t," LIKE '%").concat(a(e.value),"'");case"gt":return"".concat(t," > '").concat(a(e.value),"'");case"gte":return"".concat(t," >= '").concat(a(e.value),"'");case"lt":return"".concat(t," < '").concat(a(e.value),"'");case"lte":return"".concat(t," <= '").concat(a(e.value),"'");case"is_null":return"".concat(t," IS NULL");case"is_not_null":return"".concat(t," IS NOT NULL");default:return null}}).filter(Boolean);return 0===t.length?null:t.join(" AND ")}function tx(e){let{schema:t,filters:a,onFiltersChange:n,onOpenInSQL:r}=e,o=t.map(e=>e.name),i=(0,l.useCallback)(()=>{n([...a,function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return{id:tu++,column:e,operator:"contains",value:""}}(o[0]||"")])},[a,n,o]),c=(0,l.useCallback)(e=>{n(a.filter(t=>t.id!==e))},[a,n]),d=(0,l.useCallback)((e,t)=>{n(a.map(a=>a.id===e?{...a,...t}:a))},[a,n]),u=(0,l.useCallback)(()=>{n([])},[n]);return(0,s.jsxs)("div",{className:"border-b bg-neutral-50 px-3 py-2 flex flex-col gap-1.5 font-sans",children:[0===a.length&&(0,s.jsx)("div",{className:"text-xs text-muted-foreground py-0.5",children:"No filters applied. Add a filter below."}),a.map(e=>(0,s.jsxs)("div",{className:"flex items-center gap-1.5",children:[(0,s.jsxs)(eh,{value:e.column,onValueChange:t=>d(e.id,{column:t}),children:[(0,s.jsx)(ep,{className:"h-7 text-xs w-36 min-w-0",children:(0,s.jsx)(ef,{placeholder:"Column"})}),(0,s.jsx)(ej,{children:o.map(e=>(0,s.jsx)(ev,{value:e,className:"text-xs font-mono",children:e},e))})]}),(0,s.jsxs)(eh,{value:e.operator,onValueChange:t=>d(e.id,{operator:t}),children:[(0,s.jsx)(ep,{className:"h-7 text-xs w-36 min-w-0",children:(0,s.jsx)(ef,{})}),(0,s.jsx)(ej,{children:tc.map(e=>(0,s.jsx)(ev,{value:e.value,className:"text-xs",children:e.label},e.value))})]}),!td.has(e.operator)&&(0,s.jsx)(b,{className:"h-7 text-xs w-44 min-w-0 font-mono",placeholder:"value",value:e.value,onChange:t=>d(e.id,{value:t.target.value})}),(0,s.jsx)("button",{className:"shrink-0 rounded p-0.5 hover:bg-neutral-200 text-muted-foreground transition-colors",onClick:()=>c(e.id),title:"Remove filter",children:(0,s.jsx)(q.A,{className:"h-3.5 w-3.5"})})]},e.id)),(0,s.jsxs)("div",{className:"flex items-center gap-2 pt-0.5",children:[(0,s.jsxs)(L,{size:"sm",variant:"ghost",className:"h-6 text-xs px-2 gap-1",onClick:i,children:[(0,s.jsx)(ti.A,{className:"h-3 w-3"}),"Add filter"]}),a.length>0&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)(L,{size:"sm",variant:"ghost",className:"h-6 text-xs px-2 gap-1",onClick:r,children:[(0,s.jsx)(h.A,{className:"h-3 w-3"}),"Open in SQL"]}),(0,s.jsx)(L,{size:"sm",variant:"ghost",className:"h-6 text-xs px-2 text-red-500 hover:text-red-600 hover:bg-red-50",onClick:u,children:"Clear filters"})]})]})]})}function th(e,t){try{let a=localStorage.getItem("table-columns-".concat(e));if(a){let e=JSON.parse(a).filter(e=>t.includes(e));if(e.length>0)return e}}catch(e){}return localStorage.setItem("table-columns-".concat(e),JSON.stringify(t)),t}function tf(e){let{tableName:t,onOpenInSQL:a}=e,{executeQuery:n,getCachedTable:r,updateTableCache:o,invalidateTable:c,switchTable:d,activeTable:u,updateTableData:m,setOperationStatus:h,sourceMode:f}=(0,_.t)(),{toast:p}=(0,eI.dj)(),[g,b]=(0,l.useState)(()=>[]),[j,v]=(0,l.useState)([]),[y,C]=(0,l.useState)(0),[S,k]=(0,l.useState)(!0),[R,E]=(0,l.useState)(!1),[T,A]=(0,l.useState)(null),[O,I]=(0,l.useState)(new Map),[L,U]=(0,l.useState)(new Set),[q,H]=(0,l.useState)([]),[K,V]=(0,l.useState)(1),[J,Q]=(0,l.useState)(50),[X,G]=(0,l.useState)(1),[Z,$]=(0,l.useState)(new Set),ee=(0,l.useRef)(null),[et,ea]=(0,l.useState)(!1),[es,en]=(0,l.useState)(!1),[er,eo]=(0,l.useState)(null),[ei,ec]=(0,l.useState)(!1),[ed,em]=(0,l.useState)(!1),[ex,eh]=(0,l.useState)(null),[ef,ep]=(0,l.useState)({}),[eg,eb]=(0,l.useState)([]),[ej,ev]=(0,l.useState)(!1),[eN,ew]=(0,l.useState)(null),[ey,eC]=(0,l.useState)(0),[eS,ek]=(0,l.useState)({}),[eR,eE]=(0,l.useState)(null),eT=(0,l.useRef)(null),[eA,e_]=(0,l.useState)({}),[eO,eL]=(0,l.useState)([]),eU=ej&&null!==eN,eM=eU?eN:g,eD=eU?ey:y,eF=eU?Math.max(1,Math.ceil(ey/J)):X,ez=j.filter(e=>eO.includes(e.name)),eP=ez.reduce((e,t)=>e+(eA[t.name]||150),40),eB=O.size>0||q.length>0,eW="remote"===f;(0,l.useEffect)(()=>{if(t&&1){let e=localStorage.getItem("table-widths-".concat(t));if(e)try{e_(JSON.parse(e))}catch(e){}}},[t]);let eY=(0,l.useCallback)(async e=>{try{var t,a;let s=await n('PRAGMA foreign_key_list("'.concat(e,'")')),l=(null==s||null==(a=s.results)||null==(t=a[0])?void 0:t.results)||[];if(l.length>0){let e={};l.forEach(t=>{e[t.from]={table:t.table,to:t.to}}),ek(e)}else ek({})}catch(e){ek({})}},[n]),eq=(0,l.useCallback)(async function(){let e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];if(!t)return;let a=r(t,K,J);if(a&&!e){b(a.data),v(a.schema),C(a.meta.total),G(a.meta.totalPages);return}k(!0),A(null),$(new Set);try{let a=await d(t,{page:K,limit:J,forceFetch:e});a.meta.page=K;let s=a.schema||[];return b(Array.isArray(a.data)?a.data:[]),v(s),C(a.meta.total),G(a.meta.totalPages),eL(th(t,s.map(e=>e.name))),eE(null),eY(t),!0}catch(e){throw console.error("Error loading table data:",e),A(e.message||String(e)),p({variant:"destructive",title:"Error",description:"Failed to load table: ".concat(e.message)}),e}finally{k(!1)}},[t,K,J,r,d,p,eY]),eH=(0,l.useCallback)(e=>{eL(a=>{let s=a.includes(e)?a.filter(t=>t!==e):[...a,e];return localStorage.setItem("table-columns-".concat(t),JSON.stringify(s)),s})},[t]),eK=(0,l.useCallback)((e,a)=>{e_(s=>{let l={...s,[e]:a};return localStorage.setItem("table-widths-".concat(t),JSON.stringify(l)),l})},[t]),eV=(0,l.useCallback)(async()=>{try{$(new Set),c(t),await eq(!0),p({title:"Success",description:"Table data refreshed"})}catch(e){console.error("Refresh failed:",e),p({variant:"destructive",title:"Refresh Failed",description:e.message})}},[eq,c,t,p]),eX=(0,l.useCallback)((e,a)=>{ep(t=>({...t,[e]:a}));let s=a&&g.length?[...g].sort((t,s)=>{let l=t[e],n=s[e];if(null==l&&null==n)return 0;if(null==l)return 1;if(null==n)return -1;if("number"==typeof l&&"number"==typeof n)return"asc"===a?l-n:n-l;let r=String(l).toLowerCase(),o=String(n).toLowerCase();return"asc"===a?r.localeCompare(o):o.localeCompare(r)}):g;b(s);let l=r(t);l&&o(t,s,l.schema,{...l.meta,currentPage:K,limit:J},K,J)},[g,t,r,o]),eG=(0,l.useCallback)((e,t,a)=>{let s="".concat(e,"::").concat(t);I(e=>new Map(e).set(s,{value:a,originalValue:a})),U(t=>new Set(t).add(e))},[]),e$=(0,l.useCallback)((e,t,a)=>{var s,l;let n="".concat(e,"::").concat(t),r=g[e],o=null!=(l=null==(s=O.get(n))?void 0:s.originalValue)?l:r[t];if(a===o){let t=new Map(O);if(t.delete(n),I(t),!Array.from(t.keys()).some(t=>t.startsWith("".concat(e,"::")))){let t=new Set(L);t.delete(e),U(t)}}else I(e=>new Map(e).set(n,{value:a,originalValue:o})),U(t=>new Set(t).add(e))},[g,O,L]),e0=(0,l.useCallback)(()=>{if(eW)return void p({variant:"destructive",title:"Remote is read-only",description:"Switch source to Local to add records."});H(e=>[...e,{}])},[eW,p]),e1=(0,l.useCallback)(e=>{H(t=>t.filter((t,a)=>a!==e))},[]),e2=(0,l.useCallback)((e,t)=>{H(a=>{let s=[...a];return s[e]=t,s})},[]),e5=async function(){let e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];E(!0);try{if(q.length>0){let e=0,a=j.filter(e=>"datetime"===e.type.toLowerCase()).map(e=>e.name);for(let s of q){let l=Object.keys(s).filter(e=>{let t=s[e];if(null===t||""===t||"null"===t){let t=j.find(t=>t.name===e);return!!((null==t?void 0:t.dflt_value)&&(t.dflt_value.includes("datetime(")||t.dflt_value.includes("randomblob")||t.dflt_value.includes("lower(hex(")))}if((null==t?void 0:t.toString())==="[Auto-generated INT]"){let t=j.find(t=>t.name===e);if(t&&1===t.pk&&"INTEGER"===t.type.toUpperCase())return!1}return null==t||t.toString(),!0});if(a.forEach(e=>{l.includes(e)||l.push(e)}),0===l.length)continue;let r=e=>(null==e?void 0:e.dflt_value)&&"null"!==e.dflt_value&&null!==e.dflt_value,o=l.map(e=>{if(a.includes(e))return"CURRENT_TIMESTAMP";let t=s[e];if(null===t||""===t||"null"===t){let t=j.find(t=>t.name===e);return r(t)?t.dflt_value:"NULL"}let l=t.toString();if("[Auto-generated UUID]"===l){let t=j.find(t=>t.name===e);if(r(t))return t.dflt_value}if("[Auto-generated]"===l){let t=j.find(t=>t.name===e);return r(t)?t.dflt_value:"lower(hex(randomblob(8)))"}if("[Auto-generated INT]"===l){let t=j.find(t=>t.name===e);if(t&&1===t.pk&&"INTEGER"===t.type.toUpperCase())return null}return l.includes("randomblob")||l.includes("CURRENT_TIMESTAMP")||l.includes("lower(hex(")||l.includes("datetime(")||l.startsWith("(")&&l.endsWith(")")?l:"'".concat(l.replace(/'/g,"''"),"'")}),i="INSERT INTO ".concat(t," (").concat(l.join(", "),") VALUES (").concat(o.join(", "),");");await n(i),e++}if(e>0){let a=await m(t);b(a.data),C(a.meta.total),G(Math.ceil(a.meta.total/J)),h("success","Added ".concat(e," ").concat(1===e?"record":"records"," \xb7 ").concat(t)),p({title:"Success",description:"Added ".concat(e," ").concat(1===e?"record":"records")})}H([])}if(O.size>0&&(e||!ex)){var a;let e=j.find(e=>1===e.pk),s=null==e?void 0:e.name;if(!s)throw Error("No primary key found in table schema");let l=(null==e||null==(a=e.type)?void 0:a.toUpperCase())==="INTEGER",r=new Map;O.forEach((e,t)=>{let a=t.indexOf("::"),l=t.substring(0,a),n=t.substring(a+2),o=g[l];r.has(o[s])||r.set(o[s],new Map),r.get(o[s]).set(n,e.value)});let o=0;for(let[e,a]of r){let r=Array.from(a.entries()).map(e=>{let[t,a]=e;return null===a||""===a||"null"===a?"".concat(t," = NULL"):"CURRENT_TIMESTAMP"===a?"".concat(t," = CURRENT_TIMESTAMP"):"".concat(t," = '").concat(a.toString().replace(/'/g,"''"),"'")}).join(", "),i=l?e:"'".concat(String(e).replace(/'/g,"''"),"'"),c="\n UPDATE ".concat(t,"\n SET ").concat(r,"\n WHERE ").concat(s," = ").concat(i,";\n ");await n(c),o++}if(o>0){let e=await m(t);b(e.data),C(e.meta.total),G(Math.ceil(e.meta.total/J)),h("success","Updated ".concat(o," ").concat(1===o?"record":"records"," \xb7 ").concat(t)),p({title:"Success",description:"Updated ".concat(o," ").concat(1===o?"record":"records")})}}I(new Map),U(new Set),eh(null)}catch(s){let e=el(s),a={...e,stderr:s.stderr,stdout:s.stdout};h("error",e.message||"Save failed","Error saving changes to ".concat(t)),eh(a),em(!0)}finally{E(!1)}},e4=(0,l.useCallback)(async()=>{if(!eW&&t){en(!0),eo(null);try{var e;let a,s=null==(e=j.find(e=>1===e.pk))?void 0:e.name;if(!s)throw Error("No primary key found in table schema");let l=Array.from(Z).map(e=>g[e][s]);if(0===l.length)return;let r=e=>null==e||"null"===e,o=l.filter(r),i=l.filter(e=>!r(e)),c=i.map(e=>"string"==typeof e&&isNaN(e)?"'".concat(e.replace(/'/g,"''"),"'"):e);a=o.length>0&&i.length>0?"DELETE FROM ".concat(t," WHERE ").concat(s," IS NULL OR ").concat(s," IN (").concat(c.join(","),");"):o.length>0?"DELETE FROM ".concat(t," WHERE ").concat(s," IS NULL;"):"DELETE FROM ".concat(t," WHERE ").concat(s," IN (").concat(c.join(","),");"),await n(a);let d=new Set(l),u=g.filter(e=>!d.has(e[s]));b(u),C(e=>e-l.length),G(Math.ceil((y-l.length)/J)),K>1&&g.length===l.length&&V(e=>e-1),ea(!1),$(new Set),h("success","Deleted ".concat(l.length," ").concat(1===l.length?"record":"records"," \xb7 ").concat(t)),p({title:"Success",description:"Deleted ".concat(l.length," ").concat(1===l.length?"record":"records")})}catch(e){console.error("Delete operation failed:",e),h("error",e.message||"Delete failed","Error deleting from ".concat(t)),eo(e.message||String(e))}finally{en(!1)}}},[t,Z,g,j,n,m,eq,K,J,y,p,r,o,h,eW]);(0,l.useEffect)(()=>{if(!t)return;I(new Map),U(new Set),H([]),A(null),$(new Set),V(1);let e=r(t,1,J);e?(b(e.data),v(e.schema),C(e.meta.total),G(e.meta.totalPages),eL(th(t,e.schema.map(e=>e.name))),eE(null),eY(t)):eq(!1)},[t,J,f]),(0,l.useEffect)(()=>{if(u===t){let e=r(t);e&&(b(e.data),v(e.schema),C(e.meta.total),G(e.meta.totalPages))}},[u,t,r]),(0,l.useEffect)(()=>{if(t&&!S){var e;let a=r(t,K,J);a&&K===(null==(e=a.meta)?void 0:e.currentPage)||eq(!1)}},[t,K,J,S,eq,r]),(0,l.useEffect)(()=>{ee.current&&(ee.current.indeterminate=Z.size>0&&Z.size<eM.length)},[Z.size,eM.length]);let e3=(0,l.useCallback)(e=>{$(e?new Set(eM.map((e,t)=>t)):new Set)},[eM]),e6=(0,l.useCallback)((e,t)=>{$(a=>{let s=new Set(a);return t?s.add(e):s.delete(e),s})},[]);(0,l.useCallback)(e=>{e_(e),localStorage.setItem("table-widths-".concat(t),JSON.stringify(e))},[t]);let e8=(0,l.useCallback)(async e=>{let a=tm(e);if(!a){ew(null),eC(0);return}try{var s,l,r,o,i,c,d,u,m;let e=(null==(s=j.find(e=>1===e.pk))?void 0:s.name)||(null==(l=j[0])?void 0:l.name)||"rowid",x=await n('SELECT COUNT(*) as total FROM "'.concat(t,'" WHERE ').concat(a)),h=null!=(m=null==x||null==(c=x.results)||null==(i=c[0])||null==(o=i.results)||null==(r=o[0])?void 0:r.total)?m:0,f=await n('SELECT * FROM "'.concat(t,'" WHERE ').concat(a,' ORDER BY "').concat(e,'" DESC LIMIT ').concat(J," OFFSET ").concat((K-1)*J)),p=(null==f||null==(u=f.results)||null==(d=u[0])?void 0:d.results)||[];eC(h),ew(p)}catch(e){ew([]),eC(0)}},[t,j,n,J,K]);return((0,l.useEffect)(()=>(eT.current&&clearTimeout(eT.current),ej&&eg.length>0?eg.some(e=>{var t;return e.column&&e.operator&&(td.has(e.operator)||String(null!=(t=e.value)?t:"").trim())})&&(eT.current=setTimeout(()=>e8(eg),400)):(ew(null),eC(0)),()=>{eT.current&&clearTimeout(eT.current)}),[ej,eg,e8]),(0,l.useEffect)(()=>{eb([]),ew(null),eC(0)},[t]),T)?(0,s.jsx)("div",{className:"p-4",children:(0,s.jsxs)(N,{variant:"destructive",children:[(0,s.jsx)(i.A,{className:"h-4 w-4"}),(0,s.jsxs)(w,{children:["Failed to load table: ",T]})]})}):(0,s.jsxs)("div",{className:"h-full flex flex-col",children:[(0,s.jsx)(eJ,{selectedCount:Z.size,totalRows:eD,currentPage:K,totalPages:eF,rowsPerPage:J,onPageChange:V,onRowsPerPageChange:Q,onAdd:e0,onDelete:()=>ea(!0),onImport:()=>{if(eW)return void p({variant:"destructive",title:"Remote is read-only",description:"Switch source to Local to import records."});ec(!0)},onRefresh:eV,tableName:t,disabled:eB,readOnly:eW,editCount:O.size,newRecordsCount:q.length,isSaving:R,onSaveChanges:e5,onDiscardChanges:()=>{I(new Map),U(new Set),H([]),eh(null),p({description:"Changes discarded"})},schema:j,visibleColumns:eO,onColumnToggle:eH,showFilters:ej,onToggleFilters:()=>ev(e=>!e),activeFilterCount:eg.filter(e=>e.column&&e.operator).length}),ej&&(0,s.jsx)(tx,{schema:j,filters:eg,onFiltersChange:eb,onOpenInSQL:()=>{let e=tm(eg);if(e&&a){var s,l;let n=(null==(s=j.find(e=>1===e.pk))?void 0:s.name)||(null==(l=j[0])?void 0:l.name)||"rowid";a('SELECT * FROM "'.concat(t,'" WHERE ').concat(e,' ORDER BY "').concat(n,'" DESC LIMIT ').concat(J,";"))}}}),(0,s.jsx)("div",{className:"flex-1 overflow-hidden bg-background font-mono",children:(0,s.jsxs)(M,{className:"w-full h-full",children:[(0,s.jsx)("div",{className:"w-max min-w-full pb-4",children:(0,s.jsxs)(F,{style:{tableLayout:"fixed",width:"".concat(eP,"px")},children:[!S&&(0,s.jsx)(z,{className:"bg-neutral-100 sticky top-0 z-30",children:(0,s.jsxs)(B,{children:[(0,s.jsx)(W,{className:"p-0 border-r border-neutral-200",style:{width:"40px",minWidth:"40px"},children:(0,s.jsx)("div",{className:"py-0.5 px-3 flex items-center",children:(0,s.jsx)(eu,{ref:ee,checked:Z.size===eM.length&&eM.length>0,onCheckedChange:e3,disabled:eB})})}),ez.map(e=>{var t;return(0,s.jsx)(ta,{width:eA[e.name]||150,onResize:t=>eK(e.name,t),className:"py-0.5 whitespace-nowrap border-r border-neutral-200",children:(0,s.jsxs)(tr,{column:e.name,sortConfig:ef,onSort:eX,children:[(0,s.jsx)("span",{className:"text-xs font-bold text-foreground",children:e.name}),(0,s.jsx)("span",{className:"text-xs text-muted-foreground",children:null==(t=e.type)?void 0:t.toLowerCase()})]})},e.name)})]})}),(0,s.jsxs)(P,{children:[S&&(0,s.jsx)(B,{className:"hover:bg-transparent",children:(0,s.jsx)(Y,{colSpan:ez.length+1,className:"h-64",children:(0,s.jsx)("div",{className:"h-full w-full flex items-center justify-center",children:(0,s.jsx)(x.A,{className:"h-6 w-6 animate-spin text-primary"})})})}),!S&&(0,s.jsxs)(s.Fragment,{children:[q.map((e,t)=>(0,s.jsx)(eZ,{schema:ez,onRemove:()=>e1(t),onChange:e=>e2(t,e)},"new-".concat(t))),0===eM.length&&0===q.length?(0,s.jsx)(B,{className:"hover:bg-transparent",children:(0,s.jsx)(Y,{colSpan:ez.length+1,className:"h-64 text-center text-muted-foreground",children:eU?"No rows match the current filters":"Table is empty"})}):eM.map((e,t)=>(0,s.jsxs)(l.Fragment,{children:[(0,s.jsxs)(B,{className:Z.has(t)?"bg-blue-50":"bg-white",children:[(0,s.jsx)(Y,{className:"p-0",children:(0,s.jsx)("div",{className:"px-3 py-2.5 flex items-center",children:(0,s.jsx)(eu,{checked:Z.has(t),onCheckedChange:e=>e6(t,e),disabled:eB||eW})})}),ez.map(a=>{let l="".concat(t,"::").concat(a.name),n=O.get(l);return(0,s.jsx)(eQ,{value:n?n.value:e[a.name],row:e,column:a.name,schema:j,onChange:e=>e$(t,a.name,e),onStartEdit:eU||eW?void 0:e=>eG(t,a.name,e),isEditing:!eU&&O.has(l),style:{width:"".concat(eA[a.name]||150,"px")},fkInfo:eS[a.name],onShowRelation:eS[a.name]?()=>eE({rowIndex:t,relatedTable:eS[a.name].table,relatedKey:eS[a.name].to,value:e[a.name]}):void 0},a.name)})]}),(null==eR?void 0:eR.rowIndex)===t&&(0,s.jsx)(to,{relatedTable:eR.relatedTable,relatedKey:eR.relatedKey,value:eR.value,colSpan:ez.length+1,onClose:()=>eE(null)})]},t))]})]})]})}),(0,s.jsx)(D,{orientation:"horizontal"})]})}),(0,s.jsx)(e9,{isOpen:et,onClose:()=>{ea(!1),eo(null)},onConfirm:e4,selectedCount:Z.size,tableName:t,isDeleting:es,error:er}),(0,s.jsx)(e7,{isOpen:ed,onClose:()=>em(!1),error:ex,onOpenInSQL:()=>{em(!1)},onUpdateAnyway:async()=>{em(!1),await e5(!0)},showSQLOption:(null==ex?void 0:ex.type)==="NULLABLE_PK"}),(0,s.jsx)(tt,{open:ei,onClose:()=>ec(!1),tableName:t,schema:j,onImportComplete:()=>{ec(!1),eV()}})]})}function tp(e){let{migration:t}=e;return t.orphaned?(0,s.jsxs)("span",{className:"inline-flex items-center gap-1 text-xs font-medium text-amber-600",children:[(0,s.jsx)(i.A,{className:"h-3 w-3"})," Orphaned"]}):t.applied?(0,s.jsxs)("span",{className:"inline-flex items-center gap-1 text-xs font-medium text-green-600",children:[(0,s.jsx)(te.A,{className:"h-3 w-3"})," Applied"]}):(0,s.jsxs)("span",{className:"inline-flex items-center gap-1 text-xs font-medium text-amber-600",children:[(0,s.jsx)(p.A,{className:"h-3 w-3"})," Pending"]})}function tg(){let{currentDatabase:e,executeQuery:t,refreshTables:a}=(0,_.t)(),{toast:n}=(0,eI.dj)(),[r,o]=(0,l.useState)([]),[c,d]=(0,l.useState)(""),[u,m]=(0,l.useState)(!0),[h,f]=(0,l.useState)(!1),[p,g]=(0,l.useState)(null),b=(0,l.useCallback)(async()=>{m(!0),g(null);try{let t=null==e?void 0:e.database_name,a=t?"?db=".concat(encodeURIComponent(t)):"",s=await fetch("/api/migrations/list".concat(a)),l=await s.json();if(!s.ok||!l.success)throw Error(l.error||"Failed to load migrations");o(l.migrations),d(l.migrationsDir)}catch(e){g(e.message)}finally{m(!1)}},[e]);(0,l.useEffect)(()=>{b()},[b]);let j=r.filter(e=>!e.applied&&!e.orphaned),v=(0,l.useCallback)(async()=>{if(0===j.length)return;f(!0);let t=0;try{for(let a of j){await fetch("/api/migrations/list?file=".concat(encodeURIComponent(a.name)).concat((null==e?void 0:e.database_name)?"&db=".concat(encodeURIComponent(e.database_name)):""));let s=await fetch("/api/migrations/sync-files",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({migrationName:a.name,db:null==e?void 0:e.database_name})}),l=await s.json();if(!s.ok||!l.success)throw Error("Failed to apply ".concat(a.name,": ").concat(l.error||"Unknown error"));t++}n({title:"Migrations Applied",description:"".concat(t," migration").concat(1!==t?"s":""," applied successfully.")}),await a(),await b()}catch(e){n({variant:"destructive",title:"Apply Failed",description:e.message})}finally{f(!1)}},[j,e,n,a,b]);return(0,s.jsxs)("div",{className:"h-full flex flex-col",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between p-4 border-b",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("h2",{className:"text-sm font-semibold",children:"Migrations"}),c&&(0,s.jsx)("p",{className:"text-xs text-muted-foreground mt-0.5 font-mono",children:c})]}),(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[j.length>0&&(0,s.jsx)(L,{size:"sm",onClick:v,disabled:h,className:"font-semibold",children:h?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin"})," Applying…"]}):"Apply ".concat(j.length," pending")}),(0,s.jsx)(L,{variant:"outline",size:"icon",className:"h-8",onClick:b,disabled:u,children:(0,s.jsx)(eA.A,{className:"h-4 w-4 ".concat(u?"animate-spin":"")})})]})]}),(0,s.jsxs)("div",{className:"flex-1 overflow-auto",children:[p&&(0,s.jsx)("div",{className:"p-4",children:(0,s.jsxs)(N,{variant:"destructive",children:[(0,s.jsx)(i.A,{className:"h-4 w-4"}),(0,s.jsx)(w,{children:p})]})}),!u&&!p&&0===r.length&&(0,s.jsxs)("div",{className:"flex flex-col items-center justify-center h-64 text-muted-foreground gap-2",children:[(0,s.jsx)(eF.A,{className:"h-8 w-8"}),(0,s.jsx)("p",{className:"text-sm",children:"No migration files found"}),(0,s.jsx)("p",{className:"text-xs",children:"Migration files appear here when you run DDL statements in the SQL editor"})]}),!p&&r.length>0&&(0,s.jsxs)(F,{children:[(0,s.jsx)(z,{className:"bg-neutral-100 sticky top-0 z-10",children:(0,s.jsxs)(B,{children:[(0,s.jsx)(W,{className:"text-xs font-bold",children:"Migration file"}),(0,s.jsx)(W,{className:"text-xs font-bold w-28",children:"Status"}),(0,s.jsx)(W,{className:"text-xs font-bold w-44",children:"Applied at"}),(0,s.jsx)(W,{className:"text-xs font-bold w-20",children:"Checksum"})]})}),(0,s.jsx)(P,{children:r.map(e=>(0,s.jsxs)(B,{className:e.orphaned?"opacity-60":"",children:[(0,s.jsx)(Y,{className:"font-mono text-xs",children:e.name}),(0,s.jsx)(Y,{children:(0,s.jsx)(tp,{migration:e})}),(0,s.jsx)(Y,{className:"text-xs text-muted-foreground",children:e.applied_at?new Date(e.applied_at).toLocaleString():"—"}),(0,s.jsx)(Y,{className:"font-mono text-xs text-muted-foreground",children:e.checksum?e.checksum.slice(0,8):"—"})]},e.name))})]})]})]})}let tb=(0,a(7909).default)(()=>Promise.all([a.e(714),a.e(862),a.e(95),a.e(950)]).then(a.bind(a,7950)).then(e=>({default:e.SchemaERD})),{loadableGenerated:{webpack:()=>[7950]},ssr:!1,loading:()=>(0,s.jsx)("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center",background:"#0d1117",color:"#8b949e"},children:(0,s.jsx)("span",{style:{fontSize:14},children:"Loading…"})})});function tj(){let{currentDatabase:e,sourceMode:t}=(0,_.t)();return(0,s.jsx)(tb,{db:null==e?void 0:e.database_name,source:t})}let tv={bg:"#ffffff",bg2:"#f9f8f6",border:"#e8e5e0",border2:"#d4d0c8",text:"#1a1714",text2:"#5a5650",text3:"#9a9590",orange:"#d4581e",orangeBg:"#fff4ee",orangeBorder:"#fddcc8"},tN=["January","February","March","April","May","June","July","August","September","October","November","December"],tw={2:[3,7,10,14,16],1:[5,12,18,22]};function ty(e){return String(e).padStart(2,"0")}function tC(e){let{year:t,month:a,selDay:l,today:n,earliest:r,onSelect:o,onPrev:i,onNext:c}=e,d=new Date(t,a,1).getDay(),u=new Date(t,a+1,0).getDate(),m=tw[a]||[],x=n.getUTCFullYear(),h=n.getUTCMonth(),f=n.getUTCDate(),p=[];for(let e=0;e<d;e++)p.push((0,s.jsx)("div",{},"b".concat(e)));for(let e=1;e<=u;e++){let n=t>x||t===x&&a>h||t===x&&a===h&&e>f,i=new Date(t,a,e).getTime(),c=!!r&&i<new Date(r.getFullYear(),r.getMonth(),r.getDate()).getTime(),d=t===x&&a===h&&e===f,u=e===l,g=m.includes(e),b=n||c;p.push((0,s.jsxs)("div",{onClick:b?void 0:()=>o(e),style:{fontSize:11,textAlign:"center",padding:"4px 0",borderRadius:4,cursor:b?"default":"pointer",color:b?tv.border2:u?tv.bg:d?tv.text:tv.text2,background:u?tv.text:"transparent",fontWeight:d?600:void 0,pointerEvents:b?"none":void 0,position:"relative",userSelect:"none"},className:b||u?"":"hover:bg-[#f2f0ec]",children:[e,g&&!n&&(0,s.jsx)("span",{style:{position:"absolute",bottom:1,left:"50%",transform:"translateX(-50%)",width:3,height:3,borderRadius:"50%",background:u?tv.bg:tv.orange,display:"block"}})]},e))}return(0,s.jsxs)("div",{style:{padding:"14px 16px",borderBottom:"1px solid ".concat(tv.border)},children:[(0,s.jsxs)("div",{style:{display:"flex",alignItems:"center",justifyContent:"space-between",marginBottom:10},children:[(0,s.jsxs)("span",{style:{fontSize:13,fontWeight:500,color:tv.text},children:[tN[a]," ",t]}),(0,s.jsx)("div",{style:{display:"flex",gap:3},children:[["‹",i],["›",c]].map(e=>{let[t,a]=e;return(0,s.jsx)("button",{onClick:a,style:{width:24,height:24,border:"1px solid ".concat(tv.border),borderRadius:6,background:"transparent",cursor:"pointer",fontSize:14,color:tv.text2,display:"flex",alignItems:"center",justifyContent:"center",lineHeight:1},className:"hover:bg-[#f9f8f6]",children:t},t)})})]}),(0,s.jsxs)("div",{style:{display:"grid",gridTemplateColumns:"repeat(7, 1fr)",gap:1},children:[["Su","Mo","Tu","We","Th","Fr","Sa"].map(e=>(0,s.jsx)("div",{style:{fontSize:10,fontWeight:500,color:tv.text3,textAlign:"center",padding:"3px 0"},children:e},e)),p]})]})}function tS(e){let{kind:t,text:a}=e,l={a:{bg:"#f0fdf4",color:"#15803d",markBg:"#f0fdf4",markColor:"#16a34a",mark:"+"},r:{bg:"#fef2f2",color:"#b91c1c",markBg:"#fef2f2",markColor:"#dc2626",mark:"−"},c:{bg:"#f9fafb",color:tv.text3,markBg:"#f9fafb",markColor:tv.text3,mark:"~"}}[t];return(0,s.jsxs)("div",{style:{display:"flex",alignItems:"stretch",fontSize:11,fontFamily:"ui-monospace,SF Mono,monospace",borderRadius:5,overflow:"hidden",marginBottom:2},children:[(0,s.jsx)("div",{style:{width:22,minWidth:22,display:"flex",alignItems:"center",justifyContent:"center",fontWeight:600,fontSize:12,background:l.markBg,color:l.markColor},children:l.mark}),(0,s.jsx)("div",{style:{flex:1,padding:"4px 8px",whiteSpace:"nowrap",overflow:"hidden",textOverflow:"ellipsis",background:l.bg,color:l.color},children:a})]})}function tk(e){let{type:t,children:a}=e;return(0,s.jsx)("span",{style:{fontSize:10,padding:"1px 6px",borderRadius:3,fontFamily:"ui-monospace,SF Mono,monospace",...{rm:{background:"#fef2f2",color:"#dc2626",border:"1px solid #fecaca"},add:{background:"#f0fdf4",color:"#16a34a",border:"1px solid #bbf7d0"},ch:{background:"#fffbeb",color:"#d97706",border:"1px solid #fde68a"}}[t]},children:a})}function tR(e){let{label:t,bookmark:a,onCancel:l,onConfirm:n}=e;return(0,s.jsx)("div",{style:{position:"absolute",inset:0,background:"rgba(0,0,0,0.2)",display:"flex",alignItems:"center",justifyContent:"center",zIndex:30,borderRadius:12},children:(0,s.jsxs)("div",{style:{background:tv.bg,border:"1px solid ".concat(tv.border),borderRadius:12,padding:24,boxShadow:"0 8px 40px rgba(0,0,0,0.12)",width:340},children:[(0,s.jsx)("div",{style:{width:38,height:38,borderRadius:10,display:"flex",alignItems:"center",justifyContent:"center",background:"#fef2f2",border:"1px solid #fecaca",marginBottom:14},children:(0,s.jsxs)("svg",{width:"18",height:"18",viewBox:"0 0 18 18",fill:"none",stroke:"#dc2626",strokeWidth:"1.5",children:[(0,s.jsx)("path",{d:"M9 2L2 15h14L9 2z"}),(0,s.jsx)("path",{d:"M9 7v4M9 12.5v.5"})]})}),(0,s.jsx)("div",{style:{fontSize:14,fontWeight:600,color:tv.text,marginBottom:5},children:"Restore database?"}),(0,s.jsx)("div",{style:{fontSize:12,color:tv.text2,lineHeight:1.6,marginBottom:10},children:"This will overwrite your remote D1 database in place. All in-flight queries and transactions will be cancelled."}),(0,s.jsxs)("div",{style:{fontFamily:"ui-monospace,SF Mono,monospace",fontSize:12,padding:"8px 10px",background:tv.bg2,borderRadius:6,color:tv.text,marginBottom:10,lineHeight:1.7},children:[t,a&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("br",{}),(0,s.jsxs)("span",{style:{fontSize:10,color:tv.text3},children:["bookmark: ",a]})]})]}),(0,s.jsx)("div",{style:{fontSize:11,color:"#c2410c",background:tv.orangeBg,border:"1px solid ".concat(tv.orangeBorder),borderRadius:6,padding:"8px 10px",marginBottom:16,lineHeight:1.55},children:"⚠ A pre-restore bookmark will be saved automatically so you can undo this restore if needed."}),(0,s.jsxs)("div",{style:{display:"flex",gap:8},children:[(0,s.jsx)("button",{onClick:l,style:{flex:1,padding:8,border:"1px solid ".concat(tv.border),borderRadius:7,background:"transparent",color:tv.text2,fontSize:12,cursor:"pointer",fontFamily:"system-ui,sans-serif"},className:"hover:text-[#1a1714]",children:"Cancel"}),(0,s.jsx)("button",{onClick:n,style:{flex:1,padding:8,background:"#dc2626",color:"white",border:"none",borderRadius:7,fontSize:12,fontWeight:500,cursor:"pointer",fontFamily:"system-ui,sans-serif"},className:"hover:bg-[#b91c1c]",children:"Yes, restore database"})]})]})})}function tE(e){let{label:t,undoBookmark:a,onDone:l}=e;return(0,s.jsx)("div",{style:{position:"absolute",inset:0,background:"rgba(0,0,0,0.2)",display:"flex",alignItems:"center",justifyContent:"center",zIndex:30,borderRadius:12},children:(0,s.jsxs)("div",{style:{background:tv.bg,border:"1px solid ".concat(tv.border),borderRadius:12,padding:24,boxShadow:"0 8px 40px rgba(0,0,0,0.12)",width:320,textAlign:"center"},children:[(0,s.jsx)("div",{style:{width:38,height:38,borderRadius:"50%",display:"flex",alignItems:"center",justifyContent:"center",background:"#f0fdf4",border:"1px solid #bbf7d0",margin:"0 auto 14px"},children:(0,s.jsx)("svg",{width:"18",height:"18",viewBox:"0 0 20 20",fill:"none",stroke:"#16a34a",strokeWidth:"2",children:(0,s.jsx)("path",{d:"M4 10l4 4 8-8"})})}),(0,s.jsx)("div",{style:{fontSize:14,fontWeight:600,color:tv.text,marginBottom:5},children:"Database restored"}),(0,s.jsxs)("div",{style:{fontSize:12,color:tv.text2,lineHeight:1.6,marginBottom:10},children:["Database has been restored to ",t," successfully."]}),a&&(0,s.jsxs)("div",{style:{fontSize:11,color:tv.text2,background:tv.bg2,border:"1px solid ".concat(tv.border),borderRadius:6,padding:"7px 10px",marginBottom:14,fontFamily:"ui-monospace,SF Mono,monospace",textAlign:"left"},children:["undo bookmark: ",a,(0,s.jsx)("br",{}),(0,s.jsx)("span",{style:{fontSize:10,color:tv.text3},children:"Use this to revert if something looks wrong"})]}),(0,s.jsx)("button",{onClick:l,style:{width:"100%",padding:9,background:tv.text,color:tv.bg,border:"none",borderRadius:7,fontSize:12,fontWeight:500,cursor:"pointer",fontFamily:"system-ui,sans-serif"},children:"Done"})]})})}function tT(){let{currentDatabase:e}=(0,_.t)(),t=(null==e?void 0:e.database_name)||"database",a=new Date,[n,r]=(0,l.useState)(a.getUTCFullYear()),[o,i]=(0,l.useState)(a.getUTCMonth()),[c,d]=(0,l.useState)(a.getUTCDate()),[u,m]=(0,l.useState)("".concat(ty(a.getUTCHours()),":").concat(ty(a.getUTCMinutes()),":00")),[x,h]=(0,l.useState)(null),[f,p]=(0,l.useState)(null),[g,b]=(0,l.useState)(!0),[j,v]=(0,l.useState)(null),[N,w]=(0,l.useState)(null),y=new Date(a.getTime()-2592e6),C=N||y,[S,k]=(0,l.useState)(null),[R,E]=(0,l.useState)(!1),[T,A]=(0,l.useState)(null),[O,I]=(0,l.useState)(!1),[L,U]=(0,l.useState)(!1),[M,D]=(0,l.useState)(null),[F,z]=(0,l.useState)(!1),[P,B]=(0,l.useState)(null),W="".concat(tN[o].slice(0,3)," ").concat(ty(c),", ").concat(n," \xb7 ").concat(u||"00:00:00"," UTC"),Y="".concat(tN[o].slice(0,3)," ").concat(ty(c),", ").concat(n," at ").concat(u||"00:00:00"," UTC"),q=(0,l.useCallback)(()=>{i(e=>0===e?(r(e=>e-1),11):e-1),d(1),k(null)},[]),H=(0,l.useCallback)(()=>{let e=11===o?0:o+1,t=11===o?n+1:n;t>a.getUTCFullYear()||t===a.getUTCFullYear()&&e>a.getUTCMonth()||(i(e),r(t),d(1),k(null))},[o,n,a]),K=(0,l.useCallback)(e=>{d(e),h(null),k(null)},[]),V=(0,l.useCallback)((e,t,a,s,l)=>{r(t),i(a),d(s),m(l),h(e),k(null)},[]),J=(0,l.useCallback)(()=>{let e=new Date;r(e.getUTCFullYear()),i(e.getUTCMonth()),d(e.getUTCDate()),m("".concat(ty(e.getUTCHours()),":").concat(ty(e.getUTCMinutes()),":00")),h(null),k(null)},[]),Q=(0,l.useCallback)(async()=>{E(!0),A(null),k(null);try{let t=(null==e?void 0:e.database_name)?"?db=".concat(encodeURIComponent(e.database_name)):"",a=await fetch("/api/time-travel/preview".concat(t)),s=await a.json();if(!a.ok||!s.success)throw Error(s.error||"Failed to refresh remote state");p(s.currentBookmark),k({bookmark:s.currentBookmark})}catch(e){A(e.message)}finally{E(!1)}},[e]),X=(0,l.useCallback)(async()=>{I(!1),U(!0),D(null);try{var t;let a=new Date("".concat(n,"-").concat(ty(o+1),"-").concat(ty(c),"T").concat(u,"Z")).toISOString(),s=await fetch("/api/time-travel/restore",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({db:null==e?void 0:e.database_name,bookmark:null!=(t=null==S?void 0:S.bookmark)?t:null,timestamp:a})}),l=await s.json();if(!s.ok||!l.success)throw Error(l.error||"Restore failed");B(l.undoBookmark),z(!0)}catch(e){D(e.message)}finally{U(!1)}},[n,o,c,u,e,S]);(0,l.useEffect)(()=>{let t=!1;return k(null),A(null),async function(){b(!0),v(null);try{let a=(null==e?void 0:e.database_name)?"?db=".concat(encodeURIComponent(e.database_name)):"",s=await fetch("/api/time-travel/preview".concat(a)),l=await s.json();if(t)return;l.success?p(l.currentBookmark):v(l.error)}catch(e){t||v(e.message)}finally{t||b(!1)}}(),()=>{t=!0}},[null==e?void 0:e.database_name]);let G=new Date(a-18e5),Z=new Date(a);Z.setHours(12,0,0,0);let $=new Date(a-864e5);$.setHours(18,0,0,0);let ee=new Date(a-1728e5);ee.setHours(9,0,0,0);let et=new Date(a-6048e5);et.setHours(0,0,0,0);let ea=[{id:"q30",label:"30 minutes ago",time:"".concat(ty(G.getUTCHours()),":").concat(ty(G.getUTCMinutes())),date:G,y:G.getUTCFullYear(),m:G.getUTCMonth(),d:G.getUTCDate(),t:"".concat(ty(G.getUTCHours()),":").concat(ty(G.getUTCMinutes()),":00"),icon:"clock"},{id:"qtoday",label:"Earlier today",time:"12:00",date:Z,y:Z.getUTCFullYear(),m:Z.getUTCMonth(),d:Z.getUTCDate(),t:"12:00:00",icon:"clock"},{id:"qyest",label:"Yesterday evening",time:"".concat(tN[$.getUTCMonth()].slice(0,3)," ").concat($.getUTCDate()),date:$,y:$.getUTCFullYear(),m:$.getUTCMonth(),d:$.getUTCDate(),t:"18:00:00",icon:"clock"},{id:"qmig",label:"Before last migration",time:"".concat(tN[ee.getUTCMonth()].slice(0,3)," ").concat(ee.getUTCDate()),date:ee,y:ee.getUTCFullYear(),m:ee.getUTCMonth(),d:ee.getUTCDate(),t:"09:00:00",icon:"shield"},{id:"qweek",label:"1 week ago",time:"".concat(tN[et.getUTCMonth()].slice(0,3)," ").concat(et.getUTCDate()),date:et,y:et.getUTCFullYear(),m:et.getUTCMonth(),d:et.getUTCDate(),t:"00:00:00",icon:"clock"}];return(0,s.jsxs)("div",{style:{display:"flex",flexDirection:"column",height:"100%",position:"relative",overflow:"hidden",background:tv.bg},children:[(0,s.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:12,padding:"12px 18px",borderBottom:"1px solid ".concat(tv.border),flexShrink:0},children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("div",{style:{fontSize:13,fontWeight:600,color:tv.text},children:"Time travel"}),(0,s.jsxs)("div",{style:{fontSize:11,color:tv.text3,marginTop:1},children:[t," \xb7 remote \xb7"," ",g?"loading available window…":j?"could not load window info":f?"latest snapshot: ".concat(f.slice(0,18),"…"):"restore to any point in the available window"]})]}),(0,s.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:6,padding:"5px 11px",background:tv.orangeBg,border:"1px solid ".concat(tv.orangeBorder),borderRadius:20,fontSize:11,color:tv.orange,marginLeft:"auto",flexShrink:0},children:[(0,s.jsxs)("svg",{width:"12",height:"12",viewBox:"0 0 13 13",fill:"none",stroke:"currentColor",strokeWidth:"1.3",children:[(0,s.jsx)("path",{d:"M6.5 1L1 11h11z"}),(0,s.jsx)("path",{d:"M6.5 5v3M6.5 9.5v.5"})]}),"Restore overwrites remote DB in place"]})]}),(0,s.jsxs)("div",{style:{flex:1,display:"flex",overflow:"hidden"},children:[(0,s.jsxs)("div",{style:{width:290,minWidth:290,borderRight:"1px solid ".concat(tv.border),display:"flex",flexDirection:"column",overflow:"hidden"},children:[(0,s.jsx)(tC,{year:n,month:o,selDay:c,today:a,earliest:C,onSelect:K,onPrev:q,onNext:H}),(0,s.jsxs)("div",{style:{padding:"12px 16px",borderBottom:"1px solid ".concat(tv.border),flexShrink:0},children:[(0,s.jsx)("div",{style:{fontSize:11,fontWeight:500,color:tv.text3,marginBottom:7},children:"Time (UTC)"}),(0,s.jsxs)("div",{style:{display:"flex",gap:7,alignItems:"center"},children:[(0,s.jsx)("input",{value:u,onChange:e=>{m(e.target.value),k(null)},style:{flex:1,padding:"7px 10px",border:"1px solid ".concat(tv.border),borderRadius:6,fontFamily:"ui-monospace,SF Mono,monospace",fontSize:13,color:tv.text,background:tv.bg,outline:"none"},onFocus:e=>e.target.style.borderColor=tv.border2,onBlur:e=>e.target.style.borderColor=tv.border,placeholder:"HH:MM:SS"}),(0,s.jsx)("button",{onClick:J,style:{padding:"7px 11px",border:"1px solid ".concat(tv.border),borderRadius:6,fontSize:11,color:tv.text2,background:tv.bg2,cursor:"pointer",fontFamily:"system-ui,sans-serif",whiteSpace:"nowrap"},className:"hover:text-[#1a1714]",children:"Now"})]})]}),(0,s.jsxs)("div",{style:{padding:"10px 16px",flex:1,overflowY:"auto"},children:[(0,s.jsx)("div",{style:{fontSize:11,fontWeight:500,color:tv.text3,marginBottom:9},children:"Quick points"}),ea.map(e=>{let t=x===e.id,a=e.date<new Date(C.getFullYear(),C.getMonth(),C.getDate());return(0,s.jsxs)("button",{onClick:a?void 0:()=>V(e.id,e.y,e.m,e.d,e.t),disabled:a,title:a?"Outside available Time Travel window":void 0,style:{display:"flex",alignItems:"center",gap:8,width:"100%",padding:"7px 10px",border:"1px solid ".concat(t?tv.text:tv.border),borderRadius:7,background:t?tv.bg2:tv.bg,cursor:a?"not-allowed":"pointer",fontFamily:"system-ui,sans-serif",fontSize:12,color:a?tv.border2:t?tv.text:tv.text2,marginBottom:5,textAlign:"left",transition:"border-color 0.1s",opacity:a?.5:1},className:a?"":"hover:border-[#d4d0c8] hover:text-[#1a1714]",children:["clock"===e.icon?(0,s.jsxs)("svg",{width:"13",height:"13",viewBox:"0 0 13 13",fill:"none",stroke:"currentColor",strokeWidth:"1.2",style:{opacity:t?.8:.4,flexShrink:0},children:[(0,s.jsx)("circle",{cx:"6.5",cy:"6.5",r:"5"}),(0,s.jsx)("path",{d:"M6.5 3.5v3l2 1.5"})]}):(0,s.jsx)("svg",{width:"13",height:"13",viewBox:"0 0 13 13",fill:"none",stroke:"currentColor",strokeWidth:"1.2",style:{opacity:t?.8:.4,flexShrink:0},children:(0,s.jsx)("path",{d:"M6.5 1L1 4v3c0 3 2.4 5.5 5.5 6.5 3.1-1 5.5-3.5 5.5-6.5V4z"})}),e.label,(0,s.jsx)("span",{style:{marginLeft:"auto",fontFamily:"ui-monospace,SF Mono,monospace",fontSize:10,color:tv.text3},children:e.time})]},e.id)})]})]}),(0,s.jsxs)("div",{style:{flex:1,display:"flex",flexDirection:"column",overflow:"hidden"},children:[(0,s.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:10,padding:"11px 16px",borderBottom:"1px solid ".concat(tv.border),flexShrink:0},children:[(0,s.jsxs)("span",{style:{fontSize:12,fontWeight:500,color:tv.text,flex:1},children:["Snapshot — ",Y]}),(null==S?void 0:S.bookmark)&&(0,s.jsx)("span",{style:{fontFamily:"ui-monospace,SF Mono,monospace",fontSize:10,color:tv.text3,padding:"2px 7px",background:tv.bg2,border:"1px solid ".concat(tv.border),borderRadius:3},children:S.bookmark.length>22?S.bookmark.slice(0,20)+"…":S.bookmark})]}),(0,s.jsxs)("div",{style:{flex:1,overflowY:"auto",padding:"14px 16px"},children:[T&&(0,s.jsx)("div",{style:{fontSize:12,color:"#dc2626",background:"#fef2f2",border:"1px solid #fecaca",borderRadius:7,padding:"10px 12px",marginBottom:12},children:T}),M&&(0,s.jsxs)("div",{style:{fontSize:12,color:"#dc2626",background:"#fef2f2",border:"1px solid #fecaca",borderRadius:7,padding:"10px 12px",marginBottom:12},children:["Restore failed: ",M]}),R&&(0,s.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:8,fontSize:12,color:tv.text3,padding:"20px 0"},children:[(0,s.jsxs)("svg",{width:"14",height:"14",viewBox:"0 0 14 14",fill:"none",stroke:"currentColor",strokeWidth:"1.5",style:{animation:"spin 1s linear infinite"},children:[(0,s.jsx)("circle",{cx:"7",cy:"7",r:"5",strokeOpacity:"0.3"}),(0,s.jsx)("path",{d:"M7 2a5 5 0 0 1 5 5"})]}),"Loading snapshot info…",(0,s.jsx)("style",{children:"@keyframes spin { to { transform: rotate(360deg); } }"})]}),!R&&!S&&!T&&(0,s.jsxs)("div",{style:{padding:"20px 0",color:tv.text3,fontSize:12},children:[f&&(0,s.jsxs)("div",{style:{background:tv.bg2,border:"1px solid ".concat(tv.border),borderRadius:8,padding:"12px 14px",marginBottom:14},children:[(0,s.jsx)("div",{style:{fontSize:11,fontWeight:500,color:tv.text3,marginBottom:6},children:"Current snapshot (latest)"}),(0,s.jsx)("div",{style:{fontFamily:"ui-monospace,SF Mono,monospace",fontSize:11,color:tv.text,wordBreak:"break-all"},children:f})]}),(0,s.jsxs)("div",{style:{textAlign:"center",paddingTop:f?4:24},children:[(0,s.jsxs)("svg",{width:"32",height:"32",viewBox:"0 0 13 13",fill:"none",stroke:"currentColor",strokeWidth:"1",style:{margin:"0 auto 10px",display:"block",opacity:.3},children:[(0,s.jsx)("circle",{cx:"6.5",cy:"6.5",r:"5"}),(0,s.jsx)("path",{d:"M6.5 3.5v3l2 1.5"})]}),"Select a date & time, then click ",(0,s.jsx)("strong",{children:"Refresh preview"})," to load snapshot info."]})]}),!R&&S&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)("div",{style:{marginBottom:18},children:[(0,s.jsxs)("div",{style:{fontSize:11,fontWeight:600,color:tv.text,marginBottom:7,display:"flex",alignItems:"center",gap:6},children:["Remote state",(0,s.jsx)(tk,{type:"add",children:"refreshed"})]}),(0,s.jsx)(tS,{kind:"c",text:"Restore target: ".concat(Y)}),S.bookmark?(0,s.jsx)(tS,{kind:"a",text:"current bookmark: ".concat(S.bookmark)}):(0,s.jsx)(tS,{kind:"c",text:"current bookmark: (not returned by wrangler)"}),(0,s.jsx)(tS,{kind:"c",text:"Restore will roll back all changes made after the selected time"})]}),(0,s.jsx)("div",{style:{fontSize:11,color:tv.text3,background:tv.bg2,border:"1px solid ".concat(tv.border),borderRadius:7,padding:"10px 12px",lineHeight:1.6},children:"Cloudflare Time Travel does not provide a row-level diff before restore. After confirming, the database will be rolled back atomically to the selected point in time. An undo bookmark will be shown after restore."})]})]}),(0,s.jsxs)("div",{style:{padding:"12px 16px",borderTop:"1px solid ".concat(tv.border),display:"flex",alignItems:"center",gap:10,background:tv.bg,flexShrink:0},children:[(0,s.jsxs)("div",{style:{flex:1},children:[(0,s.jsxs)("div",{style:{fontSize:12,fontWeight:500,color:tv.text},children:["Restore to ",W]}),(null==S?void 0:S.bookmark)&&(0,s.jsxs)("div",{style:{fontSize:11,color:tv.text3,fontFamily:"ui-monospace,SF Mono,monospace",marginTop:1},children:["bookmark: ",S.bookmark]})]}),(0,s.jsxs)("button",{onClick:Q,disabled:R,style:{display:"flex",alignItems:"center",gap:5,padding:"8px 14px",borderRadius:7,border:"1px solid ".concat(tv.border),background:tv.bg,color:tv.text,fontSize:12,fontWeight:500,cursor:R?"default":"pointer",fontFamily:"system-ui,sans-serif",whiteSpace:"nowrap",opacity:R?.6:1},className:"hover:bg-[#f9f8f6]",children:[(0,s.jsxs)("svg",{width:"12",height:"12",viewBox:"0 0 12 12",fill:"none",stroke:"currentColor",strokeWidth:"1.3",children:[(0,s.jsx)("path",{d:"M1 6A5 5 0 0 1 10 3M11 6A5 5 0 0 1 2 9"}),(0,s.jsx)("path",{d:"M9 1.5v2h2M1 8.5v2h2"})]}),"Refresh preview"]}),(0,s.jsxs)("button",{onClick:()=>I(!0),disabled:L,style:{display:"flex",alignItems:"center",gap:5,padding:"8px 14px",borderRadius:7,border:"none",background:L?"#f87171":"#dc2626",color:"white",fontSize:12,fontWeight:500,cursor:L?"default":"pointer",fontFamily:"system-ui,sans-serif",whiteSpace:"nowrap"},className:"hover:bg-[#b91c1c]",children:[(0,s.jsxs)("svg",{width:"12",height:"12",viewBox:"0 0 12 12",fill:"none",stroke:"currentColor",strokeWidth:"1.3",children:[(0,s.jsx)("path",{d:"M1 6A5 5 0 0 1 10 3M11 6A5 5 0 0 1 2 9"}),(0,s.jsx)("path",{d:"M9 1.5v2h2M1 8.5v2h2"})]}),L?"Restoring…":"Restore database"]})]})]})]}),O&&(0,s.jsx)(tR,{label:W,bookmark:null==S?void 0:S.bookmark,onCancel:()=>I(!1),onConfirm:X}),F&&(0,s.jsx)(tE,{label:W,undoBookmark:P,onDone:()=>{z(!1),k(null)}})]})}function tA(e){let{open:t,onClose:a,onSubmit:n}=e,[r,o]=(0,l.useState)(""),[i,c]=(0,l.useState)(""),[d,u]=(0,l.useState)(""),[m,h]=(0,l.useState)(!1),f=async e=>{e.preventDefault(),u(""),h(!0);try{await n({accountId:r,apiToken:i}),localStorage.setItem("cf_credentials",JSON.stringify({accountId:r,apiToken:i})),a()}catch(e){u(e.message)}finally{h(!1)}};return(0,s.jsx)(e0,{open:t,onOpenChange:a,children:(0,s.jsxs)(e5,{className:"sm:max-w-md",children:[(0,s.jsxs)(e4,{children:[(0,s.jsx)(e6,{children:"Connect to Cloudflare"}),(0,s.jsx)(e8,{children:"Enter your Cloudflare credentials to push your database to production."})]}),(0,s.jsxs)("form",{onSubmit:f,className:"space-y-4 mt-4",children:[d&&(0,s.jsx)(N,{variant:"destructive",children:(0,s.jsx)(w,{children:d})}),(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsx)("label",{className:"text-sm font-medium",children:"Account ID"}),(0,s.jsx)(b,{placeholder:"Your Cloudflare Account ID",value:r,onChange:e=>o(e.target.value),required:!0})]}),(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsx)("label",{className:"text-sm font-medium",children:"API Token"}),(0,s.jsx)(b,{type:"password",placeholder:"Your D1 Database API Token",value:i,onChange:e=>c(e.target.value),required:!0})]}),(0,s.jsxs)("div",{className:"flex justify-end gap-3",children:[(0,s.jsx)(L,{type:"button",variant:"outline",onClick:a,children:"Cancel"}),(0,s.jsxs)(L,{type:"submit",disabled:m,children:[m&&(0,s.jsx)(x.A,{className:"mr-2 h-4 w-4 animate-spin"}),"Connect"]})]})]})]})})}function t_(){let{refreshTables:e}=(0,_.t)(),[t,a]=(0,l.useState)(!1),[n,r]=(0,l.useState)(null),[o,c]=(0,l.useState)(null),{isConnecting:d,isPushing:u,showPreview:m,connectToCloudflare:h,pushToRemote:f,pullToRemotePreview:p,validateStoredCredentials:g,PreviewUI:b}=V(),j=async()=>{c(null),r("push");try{if(!await g())return void a(!0);await f(),c({type:"success",message:"Push to remote initiated. Check the preview for details."})}catch(e){c({type:"error",message:e.message})}},v=async()=>{c(null),r("pull");try{if(!await g())return void a(!0);await p()}catch(e){c({type:"error",message:e.message})}},y=d||u;return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)("div",{className:"h-full flex flex-col",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2 p-4 border-b",children:[(0,s.jsx)(eA.A,{className:"h-4 w-4"}),(0,s.jsx)("h2",{className:"text-sm font-semibold",children:"Remote Sync"})]}),(0,s.jsx)("div",{className:"flex-1 overflow-auto p-6",children:(0,s.jsxs)("div",{className:"max-w-lg flex flex-col gap-6",children:[(0,s.jsx)("p",{className:"text-sm text-muted-foreground",children:"Sync your local D1 database with Cloudflare. Pushing uploads your local data to production; pulling downloads the remote state to your local environment."}),o&&(0,s.jsxs)(N,{variant:"error"===o.type?"destructive":"default",children:["error"===o.type?(0,s.jsx)(i.A,{className:"h-4 w-4"}):(0,s.jsx)(te.A,{className:"h-4 w-4"}),(0,s.jsx)(w,{children:o.message})]}),(0,s.jsxs)("div",{className:"grid grid-cols-2 gap-4",children:[(0,s.jsxs)("div",{className:"border rounded-md p-4 bg-white flex flex-col gap-3",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)(ew.A,{className:"h-4 w-4 text-neutral-600"}),(0,s.jsx)("span",{className:"text-sm font-semibold",children:"Push to remote"})]}),(0,s.jsx)("p",{className:"text-xs text-muted-foreground",children:"Upload local database state to your Cloudflare D1 database."}),(0,s.jsx)(L,{size:"sm",onClick:j,disabled:y,className:"w-full",children:u&&"push"===n?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin mr-2"})," Pushing…"]}):(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(ew.A,{className:"h-4 w-4 mr-2"})," Push"]})})]}),(0,s.jsxs)("div",{className:"border rounded-md p-4 bg-white flex flex-col gap-3",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)(eU.A,{className:"h-4 w-4 text-neutral-600"}),(0,s.jsx)("span",{className:"text-sm font-semibold",children:"Pull from remote"})]}),(0,s.jsx)("p",{className:"text-xs text-muted-foreground",children:"Download Cloudflare D1 data into your local environment."}),(0,s.jsx)(L,{size:"sm",variant:"outline",onClick:v,disabled:y,className:"w-full",children:d&&"pull"===n?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin mr-2"})," Pulling…"]}):(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(eU.A,{className:"h-4 w-4 mr-2"})," Pull"]})})]})]}),(0,s.jsx)("p",{className:"text-xs text-muted-foreground",children:"Cloudflare API credentials are required. You'll be prompted to enter them if not already saved."})]})})]}),(0,s.jsx)(tA,{open:t,onClose:()=>a(!1),onSubmit:h}),b]})}let tO={loading:"text-blue-500 border-blue-500/40 bg-blue-500/5",success:"text-green-600 border-green-500/40 bg-green-500/5",error:"text-red-500 border-red-500/40 bg-red-500/5",info:"text-muted-foreground border-border bg-transparent"},tI={loading:null,success:"✓",error:"✗",info:"─"};function tL(){var e,t,a,n,r;let{lastStatus:o}=(0,_.t)(),i=function(e){let[t,a]=(0,l.useState)("");return(0,l.useEffect)(()=>{if(!e)return void a("");let t=()=>{let t=Math.floor((Date.now()-e.getTime())/1e3);t<5?a("just now"):t<60?a("".concat(t,"s ago")):t<3600?a("".concat(Math.floor(t/60),"m ago")):a(e.toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"}))};t();let s=setInterval(t,5e3);return()=>clearInterval(s)},[e]),t}(null!=(e=null==o?void 0:o.ts)?e:null),c=null!=(t=null==o?void 0:o.type)?t:"info",d=null!=(a=null==o?void 0:o.message)?a:"Ready",u=null!=(n=null==o?void 0:o.detail)?n:null,m=null!=(r=tO[c])?r:tO.info,h=tI[c];return(0,s.jsxs)("div",{className:"flex items-center justify-between h-8 px-4 border-t font-mono text-xs select-none shrink-0 ".concat(m),role:"status","aria-live":"polite",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2 min-w-0",children:[(0,s.jsx)("span",{className:"shrink-0 w-4 flex items-center justify-center",children:"loading"===c?(0,s.jsx)(x.A,{className:"h-3 w-3 animate-spin"}):(0,s.jsx)("span",{children:h})}),(0,s.jsx)("span",{className:"truncate",children:d}),u&&(0,s.jsxs)("span",{className:"text-muted-foreground truncate hidden sm:inline",children:["\xb7 ",u]})]}),i&&(0,s.jsx)("span",{className:"text-muted-foreground shrink-0 pl-4",children:i})]})}function tU(){let[e,t]=(0,l.useState)("sql-editor"),[a,n]=(0,l.useState)(null),[r,o]=(0,l.useState)("");return(0,s.jsxs)("div",{className:"flex h-screen bg-background",children:[(0,s.jsx)(Q,{className:"w-64 border-r flex-shrink-0",currentView:e,onNavigate:t,onTableSelect:e=>{n(e),t("table-view")}}),(0,s.jsxs)("div",{className:"flex-1 flex flex-col overflow-hidden",children:[(0,s.jsxs)("main",{className:"flex-1 overflow-hidden",children:["sql-editor"===e&&(0,s.jsx)(ec,{initialQuery:r}),"schema"===e&&(0,s.jsx)(tj,{}),"migrations"===e&&(0,s.jsx)(tg,{}),"time-travel"===e&&(0,s.jsx)(tT,{}),"remote-sync"===e&&(0,s.jsx)(t_,{}),"table-view"===e&&a&&(0,s.jsx)(tf,{tableName:a,onOpenInSQL:e=>{o(e),t("sql-editor")}})]}),(0,s.jsx)(tL,{})]})]})}},4576:(e,t,a)=>{Promise.resolve().then(a.bind(a,930))}},e=>{e.O(0,[421,69,823,441,255,358],()=>e(e.s=4576)),_N_E=e.O()}]);
1
+ (self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[974],{930:(e,t,a)=>{"use strict";a.r(t),a.d(t,{default:()=>tU});var s=a(5155),l=a(2115);let n={rE:"0.3.13"};var r=a(1270),o=a(9940),i=a(8770),c=a(9007),d=a(5582),u=a(7528),m=a(109),x=a(2033),h=a(7513),f=a(8844),p=a(2593),g=a(3537);let b=l.forwardRef((e,t)=>{let{className:a,type:l,...n}=e;return(0,s.jsx)("input",{type:l,className:(0,g.cn)("flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-base shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",a),ref:t,...n})});b.displayName="Input";var j=a(3101);let v=(0,j.F)("relative w-full rounded-lg border px-4 py-3 text-sm [&>svg+div]:translate-y-[-3px] [&>svg]:absolute [&>svg]:left-4 [&>svg]:top-4 [&>svg]:text-foreground [&>svg~*]:pl-7",{variants:{variant:{default:"bg-background text-foreground",destructive:"border-destructive/50 text-destructive dark:border-destructive [&>svg]:text-destructive"}},defaultVariants:{variant:"default"}}),N=l.forwardRef((e,t)=>{let{className:a,variant:l,...n}=e;return(0,s.jsx)("div",{ref:t,role:"alert",className:(0,g.cn)(v({variant:l}),a),...n})});N.displayName="Alert",l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("h5",{ref:t,className:(0,g.cn)("mb-1 font-medium leading-none tracking-tight",a),...l})}).displayName="AlertTitle";let w=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("div",{ref:t,className:(0,g.cn)("text-sm [&_p]:leading-relaxed",a),...l})});w.displayName="AlertDescription";var y=a(5736),C=a(3263),S=a(2251),k=a(5797);let R=y.bL,E=y.l9;y.YJ,y.ZL,y.Pb,y.z6,l.forwardRef((e,t)=>{let{className:a,inset:l,children:n,...r}=e;return(0,s.jsxs)(y.ZP,{ref:t,className:(0,g.cn)("flex cursor-default gap-2 select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none focus:bg-accent data-[state=open]:bg-accent [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",l&&"pl-8",a),...r,children:[n,(0,s.jsx)(C.A,{className:"ml-auto"})]})}).displayName=y.ZP.displayName,l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(y.G5,{ref:t,className:(0,g.cn)("z-50 min-w-[8rem] overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-lg data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",a),...l})}).displayName=y.G5.displayName;let T=l.forwardRef((e,t)=>{let{className:a,sideOffset:l=4,...n}=e;return(0,s.jsx)(y.ZL,{children:(0,s.jsx)(y.UC,{ref:t,sideOffset:l,className:(0,g.cn)("z-50 min-w-[8rem] overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-md","data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",a),...n})})});T.displayName=y.UC.displayName;let A=l.forwardRef((e,t)=>{let{className:a,inset:l,...n}=e;return(0,s.jsx)(y.q7,{ref:t,className:(0,g.cn)("relative flex cursor-default select-none items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-none transition-colors focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 [&>svg]:size-4 [&>svg]:shrink-0",l&&"pl-8",a),...n})});A.displayName=y.q7.displayName,l.forwardRef((e,t)=>{let{className:a,children:l,checked:n,...r}=e;return(0,s.jsxs)(y.H_,{ref:t,className:(0,g.cn)("relative flex cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm outline-none transition-colors focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",a),checked:n,...r,children:[(0,s.jsx)("span",{className:"absolute left-2 flex h-3.5 w-3.5 items-center justify-center",children:(0,s.jsx)(y.VF,{children:(0,s.jsx)(S.A,{className:"h-4 w-4"})})}),l]})}).displayName=y.H_.displayName,l.forwardRef((e,t)=>{let{className:a,children:l,...n}=e;return(0,s.jsxs)(y.hN,{ref:t,className:(0,g.cn)("relative flex cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm outline-none transition-colors focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",a),...n,children:[(0,s.jsx)("span",{className:"absolute left-2 flex h-3.5 w-3.5 items-center justify-center",children:(0,s.jsx)(y.VF,{children:(0,s.jsx)(k.A,{className:"h-2 w-2 fill-current"})})}),l]})}).displayName=y.hN.displayName,l.forwardRef((e,t)=>{let{className:a,inset:l,...n}=e;return(0,s.jsx)(y.JU,{ref:t,className:(0,g.cn)("px-2 py-1.5 text-sm font-semibold",l&&"pl-8",a),...n})}).displayName=y.JU.displayName,l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(y.wv,{ref:t,className:(0,g.cn)("-mx-1 my-1 h-px bg-muted",a),...l})}).displayName=y.wv.displayName;var _=a(3190),O=a(2467);let I=(0,j.F)("inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",{variants:{variant:{default:"bg-primary text-primary-foreground shadow hover:bg-primary/90",destructive:"bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90",outline:"border border-input bg-background shadow-sm hover:bg-accent hover:text-accent-foreground",secondary:"bg-secondary text-secondary-foreground shadow-sm hover:bg-secondary/80",ghost:"hover:bg-accent hover:text-accent-foreground",link:"text-primary underline-offset-4 hover:underline"},size:{default:"h-9 px-4 py-2",sm:"h-8 rounded-md px-3 text-xs",lg:"h-10 rounded-md px-8",icon:"h-9 w-9"}},defaultVariants:{variant:"default",size:"default"}}),L=l.forwardRef((e,t)=>{let{className:a,variant:l,size:n,asChild:r=!1,...o}=e,i=r?O.DX:"button";return(0,s.jsx)(i,{className:(0,g.cn)(I({variant:l,size:n,className:a})),ref:t,...o})});L.displayName="Button";var U=a(9034);let M=l.forwardRef((e,t)=>{let{className:a,children:l,...n}=e;return(0,s.jsxs)(U.bL,{ref:t,className:(0,g.cn)("relative overflow-hidden",a),...n,children:[(0,s.jsx)(U.LM,{className:"h-full w-full rounded-[inherit]",children:l}),(0,s.jsx)(D,{}),(0,s.jsx)(U.OK,{})]})});M.displayName=U.bL.displayName;let D=l.forwardRef((e,t)=>{let{className:a,orientation:l="vertical",...n}=e;return(0,s.jsx)(U.VM,{ref:t,orientation:l,className:(0,g.cn)("flex touch-none select-none transition-colors","vertical"===l&&"h-full w-2.5 border-l border-l-transparent p-[1px]","horizontal"===l&&"h-2.5 flex-col border-t border-t-transparent p-[1px]",a),...n,children:(0,s.jsx)(U.lr,{className:"relative flex-1 rounded-full bg-border"})})});D.displayName=U.VM.displayName;let F=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("div",{className:"relative w-full overflow-auto",children:(0,s.jsx)("table",{ref:t,className:(0,g.cn)("w-full caption-bottom text-sm",a),...l})})});F.displayName="Table";let z=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("thead",{ref:t,className:(0,g.cn)("[&_tr]:border-b",a),...l})});z.displayName="TableHeader";let P=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("tbody",{ref:t,className:(0,g.cn)("[&_tr:last-child]:border-0",a),...l})});P.displayName="TableBody",l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("tfoot",{ref:t,className:(0,g.cn)("border-t bg-muted/50 font-medium [&>tr]:last:border-b-0",a),...l})}).displayName="TableFooter";let B=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("tr",{ref:t,className:(0,g.cn)("border-b transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted",a),...l})});B.displayName="TableRow";let W=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("th",{ref:t,className:(0,g.cn)("h-10 px-2 text-left align-middle font-medium text-muted-foreground [&:has([role=checkbox])]:pr-0 [&>[role=checkbox]]:translate-y-[2px]",a),...l})});W.displayName="TableHead";let Y=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("td",{ref:t,className:(0,g.cn)("p-2 align-middle [&:has([role=checkbox])]:pr-0 [&>[role=checkbox]]:translate-y-[2px]",a),...l})});Y.displayName="TableCell",l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)("caption",{ref:t,className:(0,g.cn)("mt-4 text-sm text-muted-foreground",a),...l})}).displayName="TableCaption";var q=a(6615);let H=function(e){let{preview:t=[],onConfirm:a,onCancel:n,isConfirming:r,error:o,isLoading:i,success:c,databaseName:d,migrationsDir:u}=e,[m,h]=(0,l.useState)(!0);if(i)return(0,s.jsx)("div",{className:"fixed bottom-4 right-4 z-50 font-sans w-[600px]",children:(0,s.jsx)("div",{className:"rounded-lg border p-4 shadow-lg bg-background",children:(0,s.jsxs)("div",{className:"flex items-start gap-3",children:[(0,s.jsx)(x.A,{className:"h-5 w-5 text-neutral-600 shrink-0 mt-0.5 animate-spin"}),(0,s.jsx)("div",{className:"flex-1 space-y-3",children:(0,s.jsxs)("div",{className:"space-y-1",children:[(0,s.jsxs)("p",{className:"font-medium text-neutral-800",children:["Preparing Push Migration",d?" \xb7 ".concat(d):""]}),(0,s.jsx)("p",{className:"text-sm text-neutral-600",children:"Checking local migrations..."})]})})]})})});if(!(null==t?void 0:t.length))return(0,s.jsx)("div",{className:"fixed bottom-4 right-4 z-50 w-[600px]",children:(0,s.jsx)("div",{className:"rounded-lg border p-4",children:(0,s.jsx)("p",{className:"text-sm font-medium",children:"No migrations to preview"})})});let f=t.filter(e=>"will be applied"===e.action).length,p=t.filter(e=>"skipped"===e.action).length;return t.filter(e=>"applied"===e.action).length,(0,s.jsx)("div",{className:"fixed bottom-4 right-4 z-50 font-sans w-[600px]",children:(0,s.jsx)("div",{className:(0,g.cn)("rounded-lg border p-4 shadow-lg",o?"bg-destructive/15":"bg-background"),children:(0,s.jsxs)("div",{className:"flex flex-col gap-3",children:[(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between",children:[(0,s.jsxs)("h5",{className:"font-medium text-neutral-900",children:["Push Migration Preview",d&&(0,s.jsx)("span",{className:"ml-2 text-xs font-normal text-neutral-500 font-mono",children:d})]}),(0,s.jsx)("button",{onClick:n,className:"text-neutral-500 hover:text-neutral-900 p-1 rounded-md hover:bg-neutral-100",children:(0,s.jsx)(q.A,{className:"h-4 w-4"})})]}),(0,s.jsxs)("p",{className:"text-sm text-neutral-700",children:[f," migration(s) \uD83D\uDD52️ pending, ",p," to ⏭️ skip"]}),(0,s.jsx)("p",{className:"text-xs text-neutral-400 font-mono",children:u?(0,s.jsxs)(s.Fragment,{children:["\uD83D\uDCC1 ",u]}):(0,s.jsx)("span",{className:"text-amber-600",children:'⚠ migrations_dir not set — using default "migrations/"'})}),o?(0,s.jsx)("p",{className:"text-sm text-destructive font-medium mt-1",children:o.message}):c&&(0,s.jsx)("p",{className:"text-sm text-green-700 font-medium mt-1",children:c})]}),(0,s.jsxs)("div",{children:[(0,s.jsx)("button",{onClick:()=>h(!m),className:"text-sm font-medium hover:underline",children:m?"Hide Details":"Show Details"}),m&&(0,s.jsx)(M,{className:"h-[480px] mt-2 rounded border border-neutral-200",children:(0,s.jsxs)(F,{children:[(0,s.jsx)(z,{children:(0,s.jsxs)(B,{children:[(0,s.jsx)(W,{children:"Migration"}),(0,s.jsx)(W,{className:"text-center w-24",children:"Status"})]})}),(0,s.jsx)(P,{children:t.map((e,t)=>(0,s.jsxs)(B,{children:[(0,s.jsx)(Y,{className:"font-mono text-xs",children:e.name}),(0,s.jsx)(Y,{className:"text-center align-middle",children:(e=>{switch(e){case"will be applied":return"\uD83D\uDD52️";case"applied":return"✅";case"skipped":return"⏭️";case"failed":return"❌";default:return""}})(e.action)})]},t))})]})})]}),(0,s.jsxs)("p",{className:"text-xs text-neutral-400",children:["Seed files (",(0,s.jsx)("span",{className:"font-mono",children:"*seed*.sql"}),") are local-only and excluded from remote sync."]}),(0,s.jsxs)("div",{className:"flex gap-2 pt-1",children:[(0,s.jsx)(L,{onClick:a,disabled:r||0===f,className:"flex-1",children:r?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(x.A,{className:"mr-2 h-4 w-4 animate-spin"}),"Confirming..."]}):"Confirm Push"}),(0,s.jsx)(L,{variant:"outline",onClick:n,disabled:r,className:"flex-1",children:"Cancel"})]})]})})})},K=function(e){let{preview:t=[],onConfirm:a,onCancel:n,onDataPull:r,isConfirming:o,isDataPulling:i,error:c,isLoading:d,success:u,databaseName:m,migrationsDir:h}=e;console.log("PullPreviewToast rendered with:",{previewLength:t.length,isConfirming:o,error:c,isLoading:d}),(0,l.useEffect)(()=>(console.log("PullPreviewToast mounted/updated"),()=>console.log("PullPreviewToast unmounted")),[]);let[f,p]=(0,l.useState)(!1);if(d)return console.log("Rendering loading state"),(0,s.jsx)("div",{className:"fixed bottom-4 right-4 z-50 font-sans w-[600px]",children:(0,s.jsx)("div",{className:"rounded-lg border p-4 shadow-lg bg-background",children:(0,s.jsxs)("div",{className:"flex items-start gap-3",children:[(0,s.jsx)(x.A,{className:"h-5 w-5 text-neutral-600 shrink-0 mt-0.5 animate-spin"}),(0,s.jsx)("div",{className:"flex-1 space-y-3",children:(0,s.jsxs)("div",{className:"space-y-1",children:[(0,s.jsxs)("p",{className:"font-medium text-neutral-800",children:["Preparing Pull Migration",m?" \xb7 ".concat(m):""]}),(0,s.jsx)("p",{className:"text-sm text-neutral-600",children:"Checking remote migrations..."})]})})]})})});if(!(null==t?void 0:t.length))return console.log("No preview data available"),(0,s.jsx)("div",{className:"fixed bottom-4 right-4 z-50 w-[600px]",children:(0,s.jsx)("div",{className:"rounded-lg border bg-destructive/15 p-4",children:(0,s.jsx)("div",{className:"flex items-center gap-2",children:(0,s.jsx)("p",{className:"text-sm font-medium text-destructive",children:"No preview data available"})})})});let b=t.filter(e=>(e.migration||e.name||"").endsWith(".sql")),j=b.length;console.log("Migration stats:",{totalMigrations:j});let v=u&&!i&&!c;return(0,s.jsx)("div",{className:"fixed bottom-4 right-4 z-50 font-sans w-[600px]",children:(0,s.jsx)("div",{className:(0,g.cn)("rounded-lg border p-4 shadow-lg",c?"bg-destructive/15":"bg-background"),children:(0,s.jsx)("div",{className:"flex items-start gap-x-1",children:(0,s.jsxs)("div",{className:"flex-1 space-y-3",children:[(0,s.jsxs)("div",{className:"space-y-1",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between",children:[(0,s.jsxs)("h5",{className:(0,g.cn)("font-medium",c?"text-destructive":"text-neutral-900"),children:[c?"Pull Migration Error":"Pull Migration Preview",m&&(0,s.jsx)("span",{className:"ml-2 text-xs font-normal text-neutral-500 font-mono",children:m})]}),(0,s.jsx)("button",{onClick:n,className:"text-neutral-500 hover:text-neutral-900 p-1 rounded-md hover:bg-neutral-100",children:(0,s.jsx)(q.A,{className:"h-4 w-4"})})]}),(0,s.jsx)("p",{className:(0,g.cn)("text-sm",c?"text-destructive/80":"text-neutral-700"),children:"".concat(j," migration(s) will be applied")}),(0,s.jsx)("p",{className:"text-xs text-neutral-400 font-mono",children:h?(0,s.jsxs)(s.Fragment,{children:["\uD83D\uDCC1 ",h]}):(0,s.jsx)("span",{className:"text-amber-600",children:'⚠ migrations_dir not set — using default "migrations/"'})}),c&&(0,s.jsx)("p",{className:"text-sm text-destructive font-medium mt-1",children:c.message}),u&&(0,s.jsx)("p",{className:"text-sm text-green-700 font-medium mt-1",children:u})]}),(0,s.jsxs)("div",{children:[(0,s.jsx)("button",{onClick:()=>p(!f),className:"text-sm font-medium hover:underline",children:f?"Hide Details":"Show Details"}),f&&(0,s.jsx)(M,{className:"h-[480px] mt-2 rounded border border-neutral-200",children:(0,s.jsxs)("div",{className:"w-full",children:[(0,s.jsx)("div",{className:"bg-neutral-50 border-b px-4 py-2",children:(0,s.jsx)("div",{className:"text-sm font-medium text-neutral-600",children:"Migration"})}),(0,s.jsx)("div",{children:b.map((e,t)=>(0,s.jsx)("div",{className:"px-4 py-2 border-b last:border-0",children:(0,s.jsx)("div",{className:"font-mono text-xs",children:e.migration||e.name})},t))})]})})]}),(0,s.jsxs)("p",{className:"text-xs text-neutral-400",children:["Seed files (",(0,s.jsx)("span",{className:"font-mono",children:"*seed*.sql"}),") are local-only and excluded from remote sync."]}),(0,s.jsx)("div",{className:"flex flex-col gap-2 pt-1",children:(0,s.jsxs)("div",{className:"flex gap-2",children:[(0,s.jsx)(L,{onClick:a,disabled:o,className:"flex-1 ".concat(o?"cursor-not-allowed opacity-50":""),children:o?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin"}),"Confirming..."]}):"Confirm Pull"}),(0,s.jsxs)("div",{className:"flex-1 flex flex-col gap-2",children:[(0,s.jsx)(L,{onClick:r,disabled:!v,variant:"outline",className:"w-full ".concat(v?"":"cursor-not-allowed opacity-50"),children:i?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin"}),"Syncing remote data..."]}):"Sync remote data"}),(0,s.jsx)("p",{className:"text-xs text-neutral-500 text-center",children:"75 records per table"})]})]})})]})})})})};function V(){var e,t;let[a]=(0,l.useState)(!1),[n,r]=(0,l.useState)(!1),[o,i]=(0,l.useState)(!1),[c,d]=(0,l.useState)(null),[u,m]=(0,l.useState)(null),[x,h]=(0,l.useState)(null),[f,p]=(0,l.useState)(null),{refreshTables:g,setOperationStatus:b,setLastSyncTime:j,currentDatabase:v}=(0,_.t)(),N=()=>{i(!1),d(null),m(null),h(null),p(null)},w=async()=>{try{h("push"),i(!0),r(!0),m(null),p(null),d({preview:[]});let t=await fetch("/api/push-remote/preview",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({db:null==v?void 0:v.database_name})}),a=await t.json();if(!a.success){var e;throw Error((null==(e=a.error)?void 0:e.message)||"Failed to generate preview")}d(a.details.details)}catch(e){console.error("Push to remote preview failed:",e),m(e),b("error",e.message||"Failed to generate push preview")}finally{r(!1)}},y=async()=>{try{r(!0),m(null),p(null);let t=await fetch("/api/push-remote/confirm",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({db:null==v?void 0:v.database_name})}),a=await t.json();if(!a.success){var e;throw Error((null==(e=a.error)?void 0:e.message)||"Failed to confirm push")}d(e=>({...e,preview:e.preview.map(e=>({...e,action:"will be applied"===e.action?"applied":e.action}))})),p("Applied ".concat(a.details.applied," migration(s), skipped ").concat(a.details.skipped,".")),j(),b("success","Push complete — ".concat(a.details.applied," migration(s) applied"))}catch(e){console.error("Push to remote confirm failed:",e),m(e)}finally{r(!1)}},C=async()=>{try{h("pull"),i(!0),r(!0),m(null),p(null),d({preview:[]});let t=await fetch("/api/pull-remote/preview",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({db:null==v?void 0:v.database_name})}),a=await t.json();if(!a.success){var e;throw Error((null==(e=a.error)?void 0:e.message)||"Failed to generate pull preview")}d(a.details)}catch(e){console.error("Pull to remote preview failed:",e),m(e),b("error",e.message||"Failed to generate pull preview")}finally{r(!1)}},S=async()=>{try{r(!0),m(null),p(null);let t=await fetch("/api/pull-remote/confirm",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({db:null==v?void 0:v.database_name})}),a=await t.json();if(!a.success){var e;throw Error((null==(e=a.error)?void 0:e.message)||"Failed to confirm pull")}p("Pull migrations applied successfully."),j(),b("success","Pull complete"),d(e=>({...e,preview:e.preview.map(e=>({...e,action:"will be applied"===e.action?"applied":e.action}))})),await g()}catch(e){console.error("Pull to remote confirm failed:",e),m(e)}finally{r(!1)}},k=o&&("pull"===x?(0,s.jsx)(K,{preview:(null==c?void 0:c.preview)||[],onConfirm:S,onCancel:N,isConfirming:n,error:u,success:f,isLoading:n&&!(null==c||null==(e=c.preview)?void 0:e.length),databaseName:null==v?void 0:v.database_name,migrationsDir:null==v?void 0:v.migrations_dir}):(0,s.jsx)(H,{preview:(null==c?void 0:c.preview)||[],onConfirm:y,onCancel:N,isConfirming:n,error:u,success:f,isLoading:n&&!(null==c||null==(t=c.preview)?void 0:t.length),databaseName:null==v?void 0:v.database_name,migrationsDir:null==v?void 0:v.migrations_dir}));return{isConnecting:a,isPushing:n,showPreview:o,pushToRemote:w,pullToRemotePreview:C,PreviewUI:k}}function J(e){let{icon:t,label:a,view:l,currentView:n,onNavigate:o,disabled:i,pro:c,locked:d}=e,u=n===l;return(0,s.jsxs)("button",{className:"w-full flex items-center gap-x-1.5 border rounded-md text-sm font-medium transition-colors px-2 py-2\n ".concat(u?"bg-neutral-900 text-white":"bg-white hover:bg-neutral-100"),onClick:()=>o(l),disabled:i,title:d?"Pro license required — run: d1s --license YOUR-KEY":void 0,children:[(0,s.jsx)(t,{className:"h-[16px] w-[16px] flex-shrink-0"}),(0,s.jsx)("span",{className:"flex-1 text-left",children:a}),c&&d&&(0,s.jsx)(r.A,{className:"h-3 w-3 opacity-40 flex-shrink-0"})]})}function Q(e){let{className:t,currentView:a,onNavigate:g,onTableSelect:j}=e,{tables:v,databases:y,currentDatabase:C,sourceMode:S,setSourceMode:k,switchDatabase:O,isLoading:I,error:L,lastSyncTime:U}=(0,_.t)(),[M,D]=(0,l.useState)(""),[F,z]=(0,l.useState)(null),[P,B]=(0,l.useState)(!1),[W,Y]=(0,l.useState)(!1);(0,l.useEffect)(()=>{fetch("/api/pro").then(e=>e.json()).then(e=>Y(!!e.isPro)).catch(()=>{})},[]);let{isConnecting:q,isPushing:H,pushToRemote:K,pullToRemotePreview:Q,PreviewUI:X}=V(),G=(0,l.useMemo)(()=>{let e=[...(null==v?void 0:v.filter(e=>e.toLowerCase().includes(M.toLowerCase())))||[]].sort((e,t)=>e.localeCompare(t)),t=e.indexOf("d1_migrations");return -1!==t&&(e.splice(t,1),e.unshift("d1_migrations")),e},[v,M]),Z=async e=>{try{B(!0),z(e),j(e)}catch(e){console.error("Error selecting table:",e)}finally{B(!1)}};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("aside",{className:"".concat(t," top-[var(--banner-height,0px)] left-0 h-[calc(100vh-var(--banner-height,0px))] bg-neutral-50 p-4"),children:(0,s.jsxs)("div",{className:"flex flex-col h-full font-sans",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between gap-2 mb-3",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)(o.A,{className:"h-5 w-5"}),(0,s.jsx)("span",{className:"font-mono font-bold text-sm",children:"d1-studio"})]}),(0,s.jsxs)("div",{className:"text-xs font-medium text-muted-foreground",children:["v",n.rE]})]}),L&&(0,s.jsxs)(N,{variant:"destructive",className:"mb-4",children:[(0,s.jsx)(i.A,{className:"h-4 w-4"}),(0,s.jsx)(w,{className:"text-sm ml-2",children:L})]}),(0,s.jsxs)("div",{className:"mb-1.5",children:[(0,s.jsx)("div",{className:"text-xs text-muted-foreground mb-1",children:"Database"}),(0,s.jsxs)(R,{children:[(0,s.jsx)(E,{asChild:!0,children:(0,s.jsxs)("button",{className:"flex items-center gap-1.5 text-sm font-semibold hover:text-foreground/80 transition-colors max-w-full w-full text-left",children:[(0,s.jsx)("span",{className:"truncate",children:(null==C?void 0:C.database_name)||"Select database"}),C&&(0,s.jsx)("span",{className:"shrink-0 text-[10px] font-medium px-1.5 py-0.5 rounded ".concat("remote"===S?"bg-sky-100 text-sky-700":"bg-green-100 text-green-700"),children:"remote"===S?"Remote":"Local".concat(C.hasLocal?" ✓":"")}),(0,s.jsx)(c.A,{className:"h-3 w-3 shrink-0 ml-auto opacity-50"})]})}),(0,s.jsx)(T,{align:"start",className:"w-56",children:y.map(e=>(0,s.jsxs)(A,{onClick:()=>O(e.database_name),className:"flex items-center justify-between gap-2 ".concat((null==C?void 0:C.database_name)===e.database_name?"font-semibold bg-accent":""),children:[(0,s.jsx)("span",{className:"truncate",children:e.database_name}),(0,s.jsx)("span",{className:"shrink-0 text-[10px] font-medium px-1.5 py-0.5 rounded ".concat("remote"===S?"bg-sky-100 text-sky-700":"bg-green-100 text-green-700"),children:"remote"===S?"Remote":"Local".concat(e.hasLocal?" ✓":"")})]},e.database_name))})]})]}),(0,s.jsxs)("div",{className:"mb-2",children:[(0,s.jsx)("div",{className:"text-xs text-muted-foreground mb-1",children:"Source"}),(0,s.jsxs)("div",{className:"flex items-center gap-1 rounded-md border bg-white p-1",children:[(0,s.jsx)("button",{onClick:()=>{k("local").catch(()=>{})},className:"flex-1 rounded px-2 py-1.5 text-xs font-semibold transition-colors ".concat("local"===S?"bg-neutral-900 text-white":"text-neutral-700 hover:bg-neutral-100"),children:"Local"}),(0,s.jsx)("button",{onClick:()=>{k("remote").catch(()=>{})},disabled:!W,title:W?"Browse/query remote database":"Pro license required — run: d1s --license YOUR-KEY",className:"flex-1 rounded px-2 py-1.5 text-xs font-semibold transition-colors disabled:opacity-50 disabled:cursor-not-allowed ".concat("remote"===S?"bg-neutral-900 text-white":"text-neutral-700 hover:bg-neutral-100"),children:W||"remote"===S?"Remote":(0,s.jsxs)("span",{className:"inline-flex items-center gap-1",children:[(0,s.jsx)(r.A,{className:"h-3.5 w-3.5 opacity-60"}),"Remote"]})})]}),(0,s.jsx)("div",{className:"text-[11px] text-muted-foreground px-0.5 mt-1",children:"remote"===S?"Remote is read-only. Switch to Local for sync/edit actions.":"Local mode enables Pull/Push and row editing."})]}),(0,s.jsxs)("div",{className:"mb-2",children:[(0,s.jsxs)("div",{className:"flex gap-1.5 mb-1",children:[(0,s.jsxs)("button",{onClick:Q,disabled:!W||"remote"===S||q||H,title:W?"remote"===S?"Switch source to Local to use Pull":void 0:"Pro license required — run: d1s --license YOUR-KEY",className:"flex-1 flex items-center justify-center gap-1 text-xs font-medium border rounded-md px-2 py-1.5 bg-white hover:bg-neutral-100 disabled:opacity-50 disabled:cursor-not-allowed transition-colors",children:[W?(0,s.jsx)(d.A,{className:"h-3.5 w-3.5"}):(0,s.jsx)(r.A,{className:"h-3.5 w-3.5 opacity-40"}),"Pull"]}),(0,s.jsxs)("button",{onClick:K,disabled:!W||"remote"===S||q||H,title:W?"remote"===S?"Switch source to Local to use Push":void 0:"Pro license required — run: d1s --license YOUR-KEY",className:"flex-1 flex items-center justify-center gap-1 text-xs font-medium border rounded-md px-2 py-1.5 bg-white hover:bg-neutral-100 disabled:opacity-50 disabled:cursor-not-allowed transition-colors",children:[W?(0,s.jsx)(u.A,{className:"h-3.5 w-3.5"}):(0,s.jsx)(r.A,{className:"h-3.5 w-3.5 opacity-40"}),"Push"]})]}),(0,s.jsx)("div",{className:"text-[11px] text-muted-foreground px-0.5",children:U?"Remote sync \xb7 ".concat(function(e){if(!e)return null;let t=Math.floor((Date.now()-new Date(e).getTime())/1e3);return t<60?"just now":t<3600?"".concat(Math.floor(t/60)," min ago"):t<86400?"".concat(Math.floor(t/3600),"h ago"):"".concat(Math.floor(t/86400),"d ago")}(U)):"Never synced"})]}),(0,s.jsxs)("div",{className:"relative mt-3 mb-2 bg-white",children:[(0,s.jsx)(m.A,{className:"absolute left-2 top-2.5 h-4 w-4 text-neutral-500"}),(0,s.jsx)(b,{placeholder:"Search tables",value:M,onChange:e=>D(e.target.value),className:"pl-7"})]}),(0,s.jsxs)("div",{className:"flex-1 min-h-0 flex flex-col",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between mb-1 flex-none",children:[(0,s.jsxs)("h3",{className:"text-sm font-medium text-muted-foreground",children:["Tables ",(null==v?void 0:v.length)?"(".concat(v.length,")"):""]}),I&&(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin text-muted-foreground"})]}),(0,s.jsx)("div",{className:"overflow-y-auto flex-1",children:(0,s.jsxs)("div",{className:"pr-2 pb-2",children:[!I&&0===G.length&&(0,s.jsx)("div",{className:"text-sm font-medium text-muted-foreground px-2 py-2",children:M?"No matching tables found":L?"Error loading tables":"No tables available"}),G.map(e=>(0,s.jsx)("button",{className:"w-full text-left text-sm font-medium rounded-md transition-colors px-2 py-2\n ".concat("table-view"===a&&F===e?"bg-[#F0F0F0] text-neutral-900 font-semibold":"text-neutral-700 hover:bg-[#F2F2F2]","\n ").concat(P&&"opacity-50 cursor-not-allowed"),onClick:()=>Z(e),disabled:P,children:e},e))]})})]}),(0,s.jsxs)("div",{className:"pt-2 flex flex-col gap-1",children:[(0,s.jsx)(J,{icon:h.A,label:"SQL editor",view:"sql-editor",currentView:a,onNavigate:g,disabled:P}),(0,s.jsx)(J,{icon:f.A,label:"Schema",view:"schema",currentView:a,onNavigate:g,disabled:P}),(0,s.jsx)(J,{icon:p.A,label:"Time travel",view:"time-travel",currentView:a,onNavigate:g,disabled:P||!W,pro:!0,locked:!W})]})]})}),X]})}var X=a(5778),G=a(2300),Z=a(4338),$=a(4903);function ee(e){let{results:t}=e;return t&&Array.isArray(t)?(0,s.jsxs)("div",{className:"text-xs font-medium text-neutral-900 flex items-center gap-2 mb-2",children:[t.length," ",1===t.length?"row":"rows"," returned"]}):null}function et(e){var t;let{error:a}=e;console.debug("SQLErrorDisplay - Received error prop:",a);let l=a&&"string"==typeof a.message?a:{message:"An unknown error occurred",details:{}};return(0,s.jsxs)("div",{children:[(0,s.jsx)("div",{className:"text-neutral-700 text-sm",children:l.message}),(null==(t=l.details)?void 0:t.stderr)&&(0,s.jsx)("pre",{className:"mt-2 p-2 text-xs text-red-500 whitespace-pre-wrap",children:l.details.stderr})]})}function ea(e){let{query:t,results:a}=e;if(!(null==a?void 0:a.length))return(0,s.jsx)("div",{className:"text-sm text-neutral-500",children:"No results to display."});let l=null==t?void 0:t.trim().toLowerCase().startsWith("pragma table_info"),n=l?["Column Name","Type","Not Null","Default Value","Primary Key"]:Object.keys(a[0]),r=l?a.map(e=>({"Column Name":e.name,Type:e.type,"Not Null":1===e.notnull?"YES":"NO","Default Value":e.dflt_value||"(null)","Primary Key":1===e.pk?"YES":"NO"})):a;return(0,s.jsxs)("div",{className:"flex flex-col h-full",children:[(0,s.jsx)(ee,{results:r}),(0,s.jsx)("div",{className:"flex-1 overflow-auto border rounded-lg",children:(0,s.jsx)("div",{className:"min-w-full",children:(0,s.jsxs)(F,{children:[(0,s.jsx)(z,{children:(0,s.jsx)(B,{className:"bg-muted/50",children:n.map(e=>(0,s.jsx)(W,{className:"font-mono text-xs font-medium text-muted-foreground",children:e},e))})}),(0,s.jsx)(P,{children:r.map((e,t)=>(0,s.jsx)(B,{className:"hover:bg-muted/50",children:n.map(a=>{let l=e[a],n=null===l?"(null)":String(l);return(0,s.jsx)(Y,{className:"font-monospace text-xs whitespace-nowrap",children:n},"".concat(t,"-").concat(a))})},t))})]})})})]})}let es={SYNTAX_ERROR:"SYNTAX_ERROR",EXECUTION_ERROR:"EXECUTION_ERROR",TABLE_NOT_FOUND:"TABLE_NOT_FOUND",TABLE_EXISTS:"TABLE_EXISTS",FOREIGN_KEY_ERROR:"FOREIGN_KEY_ERROR",UNIQUE_CONSTRAINT_ERROR:"UNIQUE_CONSTRAINT_ERROR",NOT_NULL_CONSTRAINT_ERROR:"NOT_NULL_CONSTRAINT_ERROR",CHECK_CONSTRAINT_ERROR:"CHECK_CONSTRAINT_ERROR",UNKNOWN_ERROR:"UNKNOWN_ERROR"};function el(e){if(!e)return{type:es.UNKNOWN_ERROR,message:"An unknown error occurred",details:{}};let t=null,a=e.cmd||e.details&&e.details.command;if(a){let e=a.match(RegExp('--command="(.+?)"\\s*--json',"s"));e&&(t=e[1].replace(/\\n/g,"\n").replace(/\\"/g,'"'))}let s=e.stdout||e.details&&e.details.stdout;if(s)try{var l;let e=JSON.parse(s.trim()),n=(null==e||null==(l=e.error)?void 0:l.text)||"";if(n)return en(n,t,a)}catch(e){}let n=e.stderr||e.details&&e.details.stderr;if(n)return en(n.replace(/\x1B\[[0-9;]*[a-zA-Z]/g,""),t,a);let r=e.message||e.details&&e.details.originalError||"";return r?en(r,t,a):{type:es.UNKNOWN_ERROR,message:e.message||"An unknown error occurred",details:{}}}function en(e,t,a){if(e.includes("env: node: No such file or directory"))return{type:es.EXECUTION_ERROR,message:"Node.js not found — ensure Node.js is installed and in PATH",details:{originalQuery:t}};let s=e.match(/near "([^"]+)": syntax error(?:\s+at offset (\d+))?/);if(s){let[,e,a]=s;return{type:es.SYNTAX_ERROR,message:'Syntax error near "'.concat(e,'"').concat(a?" at position ".concat(a):""),details:{originalQuery:t,errorLocation:{near:e,offset:a?parseInt(a):null}}}}if(e.includes("UNIQUE constraint failed")){let[,a]=e.match(/UNIQUE constraint failed: ([\w.]+)/)||[];return{type:es.UNIQUE_CONSTRAINT_ERROR,message:"UNIQUE constraint failed: ".concat(a||"unknown column"),details:{originalQuery:t}}}if(e.includes("NOT NULL constraint failed")){let[,a]=e.match(/NOT NULL constraint failed: ([\w.]+)/)||[];return{type:es.NOT_NULL_CONSTRAINT_ERROR,message:"NOT NULL constraint failed: ".concat(a||"unknown column"),details:{originalQuery:t}}}if(e.includes("CHECK constraint failed")){let[,a]=e.match(/CHECK constraint failed: (\S+)/)||[];return{type:es.CHECK_CONSTRAINT_ERROR,message:"CHECK constraint failed: ".concat(a||"unknown constraint"),details:{originalQuery:t}}}if(e.includes("FOREIGN KEY constraint failed"))return{type:es.FOREIGN_KEY_ERROR,message:"FOREIGN KEY constraint failed",details:{originalQuery:t}};if(e.includes("no such table")){let[,a]=e.match(/no such table: (\S+)/)||[];return{type:es.TABLE_NOT_FOUND,message:"Table not found: ".concat(a||"unknown"),details:{originalQuery:t}}}if(e.includes("already exists")){let[,a]=e.match(/table (\w+) already exists/)||[];return{type:es.TABLE_EXISTS,message:"Table already exists: ".concat(a||"unknown"),details:{originalQuery:t}}}return{type:es.EXECUTION_ERROR,message:e.replace(/:\s*SQLITE_\w+$/,"").trim()||"Failed to execute SQL command",details:{originalQuery:t}}}let er={"Show Tables":"SELECT name\n FROM sqlite_master \n WHERE type='table' \n AND name NOT LIKE 'sqlite_%';","Table Schema":"-- Table Schema\n-- Replace table_name with your table name\nPRAGMA table_info(table_name);","Table Indexes":"-- Table Indexes\n-- Replace table_name with your table name\nSELECT \n m.name as table_name,\n i.name as index_name,\n i.sql as index_definition\nFROM sqlite_master m\nJOIN sqlite_master i ON m.name = i.tbl_name\nWHERE m.type = 'table'\nAND i.type = 'index'\nAND m.name = 'table_name';","Foreign Keys":"-- Foreign Key Information\n-- Replace table_name with your table name\nPRAGMA foreign_key_list(table_name);","Table Data":"-- Table Data\n-- Replace table_name with your table name\nSELECT * FROM table_name LIMIT 100;","Row Count":"-- Row Count\n-- Replace table_name with your table name\nSELECT COUNT(*) as total_rows FROM table_name;","Column Stats":"-- Column Statistics\n-- Replace table_name and column_name with your values\nSELECT \n COUNT(*) as total_rows,\n COUNT(column_name) as non_null_count,\n COUNT(DISTINCT column_name) as unique_values,\n MIN(column_name) as min_value,\n MAX(column_name) as max_value\nFROM table_name;","Create Table":"CREATE TABLE IF NOT EXISTS new_table (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n name TEXT NOT NULL,\n email TEXT UNIQUE,\n status TEXT CHECK(status IN ('active', 'inactive')) DEFAULT 'active',\n created_at DATETIME DEFAULT CURRENT_TIMESTAMP\n);\n\n-- Create indexes\nCREATE INDEX idx_new_table_name ON new_table(name);\nCREATE UNIQUE INDEX idx_new_table_email ON new_table(email);","Create View":"-- Create a view\nCREATE VIEW view_name AS\nSELECT column1, column2\nFROM table_name\nWHERE condition;"},eo={"Schema Information":["Show Tables","Table Schema","Table Indexes","Foreign Keys"],"Data Queries":["Table Data","Row Count","Column Stats"],"Schema Modification":["Create Table","Create View"]};function ei(e){let{onSelectQuery:t}=e;return(0,s.jsxs)(R,{children:[(0,s.jsx)(E,{asChild:!0,children:(0,s.jsxs)(L,{variant:"outline",size:"sm",children:["Query Templates",(0,s.jsx)(c.A,{className:"ml-2 h-4 w-4"})]})}),(0,s.jsx)(T,{align:"start",className:"w-72",children:Object.entries(eo).map(e=>{let[a,n]=e;return(0,s.jsxs)(l.Fragment,{children:[(0,s.jsx)("div",{className:"px-2 py-1.5 text-sm font-semibold text-neutral-900 font-sans",children:a}),n.map(e=>(0,s.jsx)(A,{onClick:()=>t(e),className:"font-sans text-neutral-800",children:e},e))]},a)})})]})}function ec(e){let{initialQuery:t=""}=e,{executeQuery:a,refreshTables:n}=(0,_.t)(),[r,o]=(0,l.useState)(null),[i,c]=(0,l.useState)(!1),[d,u]=(0,l.useState)(300),[m,h]=(0,l.useState)(!1),f=(0,l.useRef)(null),p=(0,l.useRef)(null);(0,l.useEffect)(()=>{t&&f.current&&(f.current.setValue(t),f.current.focus())},[t]),(0,l.useEffect)(()=>{p.current&&(p.current.style.height="".concat(d,"px"))},[d]);let g=(0,l.useCallback)(e=>{e.preventDefault();let t=e.pageY,a=e=>{u(Math.min(Math.max(d+(e.pageY-t),100),.8*window.innerHeight))},s=()=>{document.removeEventListener("mousemove",a),document.removeEventListener("mouseup",s)};document.addEventListener("mousemove",a),document.addEventListener("mouseup",s)},[d]),b=async()=>{if(!f.current)return;let e=f.current.getValue().trim().replace(/;+$/,"");if(!e)return void o({success:!1,error:{message:"Query cannot be empty."}});try{var t,s,l,r,i;c(!0),o(null);let d=await a(e);/^(CREATE|DROP|ALTER)\s+TABLE/i.test(e)&&await n();let u=[];if(d.results){if(Array.isArray(d.results))if(d.results.length>0&&(null==(s=d.results[0])?void 0:s.columns)&&(null==(l=d.results[0])?void 0:l.values)){let e=d.results[0];u=e.values.map(t=>e.columns.reduce((e,a,s)=>(e[a]=t[s],e),{}))}else u=1===d.results.length&&d.results[0].results&&Array.isArray(d.results[0].results)?d.results[0].results:d.results;else if(d.results.columns&&d.results.values){let e=d.results;u=e.values.map(t=>e.columns.reduce((e,a,s)=>(e[a]=t[s],e),{}))}}else if(d.result&&d.result.results){if(Array.isArray(d.result.results))if(d.result.results.length>0&&(null==(r=d.result.results[0])?void 0:r.columns)&&(null==(i=d.result.results[0])?void 0:i.values)){let e=d.result.results[0];u=e.values.map(t=>e.columns.reduce((e,a,s)=>(e[a]=t[s],e),{}))}else u=1===d.result.results.length&&d.result.results[0].results&&Array.isArray(d.result.results[0].results)?d.result.results[0].results:d.result.results;else if(d.result.results.columns&&d.result.results.values){let e=d.result.results;u=e.values.map(t=>e.columns.reduce((e,a,s)=>(e[a]=t[s],e),{}))}}o({success:!0,results:u,target:(null==d||null==(t=d.execution)?void 0:t.target)||"local"})}catch(e){o({success:!1,error:el(e)||{message:"An unknown error occurred",details:{}}})}finally{c(!1)}};return(0,s.jsxs)("div",{className:"h-full flex flex-col bg-white text-neutral-900 font-sans",children:[(0,s.jsx)("div",{className:"px-4 py-2 bg-neutral-100 border-b flex items-center gap-4",children:(0,s.jsx)(ei,{onSelectQuery:e=>{if(f.current){let t=Object.keys(er).find(t=>t.toLowerCase()===e.toLowerCase());if(t){let e=er[t];f.current.setValue(e),f.current.focus()}else console.warn("Query template not found for: ".concat(e))}}})}),(0,s.jsx)("div",{ref:p,className:"relative pt-2 overflow-hidden",style:{height:"".concat(d,"px"),minHeight:"100px"},children:(0,s.jsx)(X.Ay,{height:"100%",language:"sql",options:{minimap:{enabled:!1},fontSize:14,wordWrap:"on",automaticLayout:!0,lineNumbers:"on",fontFamily:"ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace"},onMount:e=>{f.current=e,e.addCommand(monaco.KeyMod.CtrlCmd|monaco.KeyCode.Enter,()=>{b()})}})}),(0,s.jsx)("div",{className:"relative h-0.5 cursor-row-resize bg-neutral-200 group ".concat(m?"bg-neutral-300":"hover:bg-neutral-300"),onMouseDown:e=>{h(!0),g(e)},onMouseUp:()=>h(!1),children:(0,s.jsx)("div",{className:"absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 bg-neutral-900 rounded-sm px-0.5 \n ".concat(m?"visible":"invisible group-hover:visible"),children:(0,s.jsx)(G.A,{className:"h-3 w-3 text-white"})})}),(0,s.jsx)("div",{className:"px-4 py-3 bg-neutral-100 border-b font-sans",children:(0,s.jsxs)("div",{className:"flex justify-between items-center",children:[(0,s.jsx)("h3",{className:"text-sm font-medium",children:"Results"}),(0,s.jsxs)(L,{size:"sm",onClick:b,disabled:i,className:"flex items-center gap-1 text-sm font-medium",children:["Run",i?(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin"}):(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(Z.A,{className:"h-4 w-4"}),(0,s.jsx)($.A,{className:"h-4 w-4"})]})]})]})}),(0,s.jsx)("div",{className:"flex-1 overflow-auto p-4 bg-white",children:(()=>{var e;if(i)return(0,s.jsxs)("div",{className:"flex items-center text-sm text-neutral-700",children:[(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin mr-2 font-sans"}),"Running your query..."]});if(!r)return(0,s.jsx)("div",{className:"text-sm text-neutral-500 font-sans",children:"Run the query to see the results."});if(!r.success)return console.debug("SQLEditor - Rendering error:",r.error),(0,s.jsx)(et,{error:r.error});let t=r.results,a="remote"===r.target?"Remote":"Local";return t&&0!==t.length?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)("div",{className:"text-xs text-neutral-500 mb-2",children:["Executed on: ",a]}),(0,s.jsx)(ea,{query:(null==(e=f.current)?void 0:e.getValue())||"",results:t})]}):(0,s.jsxs)("div",{className:"text-sm text-neutral-900 font-sans",children:["Success (",a,"). No results to display."]})})()})]})}var ed=a(8162);let eu=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(ed.bL,{ref:t,className:(0,g.cn)("peer h-4 w-4 shrink-0 rounded-sm border border-primary shadow focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground",a),...l,children:(0,s.jsx)(ed.C1,{className:(0,g.cn)("flex items-center justify-center text-current"),children:(0,s.jsx)(S.A,{className:"h-4 w-4"})})})});eu.displayName=ed.bL.displayName;var em=a(7887),ex=a(2614);let eh=em.bL;em.YJ;let ef=em.WT,ep=l.forwardRef((e,t)=>{let{className:a,children:l,...n}=e;return(0,s.jsxs)(em.l9,{ref:t,className:(0,g.cn)("flex h-9 w-full items-center justify-between whitespace-nowrap rounded-md border border-input bg-transparent px-3 py-2 text-sm shadow-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-1 focus:ring-ring disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1",a),...n,children:[l,(0,s.jsx)(em.In,{asChild:!0,children:(0,s.jsx)(c.A,{className:"h-4 w-4 opacity-50"})})]})});ep.displayName=em.l9.displayName;let eg=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(em.PP,{ref:t,className:(0,g.cn)("flex cursor-default items-center justify-center py-1",a),...l,children:(0,s.jsx)(ex.A,{className:"h-4 w-4"})})});eg.displayName=em.PP.displayName;let eb=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(em.wn,{ref:t,className:(0,g.cn)("flex cursor-default items-center justify-center py-1",a),...l,children:(0,s.jsx)(c.A,{className:"h-4 w-4"})})});eb.displayName=em.wn.displayName;let ej=l.forwardRef((e,t)=>{let{className:a,children:l,position:n="popper",...r}=e;return(0,s.jsx)(em.ZL,{children:(0,s.jsxs)(em.UC,{ref:t,className:(0,g.cn)("relative z-50 max-h-96 min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2","popper"===n&&"data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",a),position:n,...r,children:[(0,s.jsx)(eg,{}),(0,s.jsx)(em.LM,{className:(0,g.cn)("p-1","popper"===n&&"h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]"),children:l}),(0,s.jsx)(eb,{})]})})});ej.displayName=em.UC.displayName,l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(em.JU,{ref:t,className:(0,g.cn)("px-2 py-1.5 text-sm font-semibold",a),...l})}).displayName=em.JU.displayName;let ev=l.forwardRef((e,t)=>{let{className:a,children:l,...n}=e;return(0,s.jsxs)(em.q7,{ref:t,className:(0,g.cn)("relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-2 pr-8 text-sm outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",a),...n,children:[(0,s.jsx)("span",{className:"absolute right-2 flex h-3.5 w-3.5 items-center justify-center",children:(0,s.jsx)(em.VF,{children:(0,s.jsx)(S.A,{className:"h-4 w-4"})})}),(0,s.jsx)(em.p4,{children:l})]})});ev.displayName=em.q7.displayName,l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(em.wv,{ref:t,className:(0,g.cn)("-mx-1 my-1 h-px bg-muted",a),...l})}).displayName=em.wv.displayName;var eN=a(5468),ew=a(8772),ey=a(6204),eC=a(3173),eS=a(7520);let ek=eS.Kq,eR=eS.bL,eE=eS.l9,eT=l.forwardRef((e,t)=>{let{className:a,sideOffset:l=4,...n}=e;return(0,s.jsx)(eS.ZL,{children:(0,s.jsx)(eS.UC,{ref:t,sideOffset:l,className:(0,g.cn)("z-50 overflow-hidden rounded-md bg-primary px-3 py-1.5 text-xs text-primary-foreground animate-in fade-in-0 zoom-in-95 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",a),...n})})});eT.displayName=eS.UC.displayName;var eA=a(3381);let e_=(0,l.memo)(e=>{let{onRefresh:t,disabled:a}=e,[n,r]=(0,l.useState)(!1),o=async()=>{if(!n&&!a){r(!0);try{await t()}catch(e){console.error("Refresh failed:",e)}finally{setTimeout(()=>{r(!1)},500)}}};return(0,s.jsx)(ek,{children:(0,s.jsxs)(eR,{children:[(0,s.jsx)(eE,{asChild:!0,children:(0,s.jsx)(L,{variant:"outline",size:"icon",onClick:o,disabled:n||a,className:"h-8",children:n?(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin"}):(0,s.jsx)(eA.A,{className:"h-4 w-4"})})}),(0,s.jsx)(eT,{children:(0,s.jsx)("p",{children:"Refresh table data"})})]})})});e_.displayName="RefreshButton";var eO=a(1715),eI=a(1374);let eL=(0,l.memo)(e=>{let{tableName:t,onSync:a}=e,[n,r]=(0,l.useState)(!1),{toast:o}=(0,eI.dj)();if("d1_migrations"!==t)return null;let i=async()=>{try{r(!0);let e=await fetch("/api/migrations/sync-files",{method:"POST"}),t=await e.json();if(!e.ok)throw Error(t.error||"Failed to sync migrations");let{added:s=[],removed:l=[]}=t.details||{};o({title:"Migrations Synced",description:"Added: ".concat(s.length,", Removed: ").concat(l.length)}),a&&await a()}catch(e){console.error("Error syncing migrations:",e),o({variant:"destructive",title:"Sync Failed",description:e.message})}finally{r(!1)}};return(0,s.jsx)(ek,{children:(0,s.jsxs)(eR,{children:[(0,s.jsx)(eE,{asChild:!0,children:(0,s.jsx)(L,{variant:"outline",size:"icon",onClick:i,disabled:n,className:"h-8",children:n?(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin"}):(0,s.jsx)(eO.A,{className:"h-4 w-4"})})}),(0,s.jsx)(eT,{children:(0,s.jsx)("p",{children:"Sync migration files with d1_migrations table"})})]})})});eL.displayName="SyncMigrationsButton";var eU=a(3489),eM=a(9680),eD=a(5584),eF=a(9963);let ez=(0,l.memo)(e=>{let{tableName:t,onDownloadStart:a,onDownloadComplete:n,disabled:r}=e,[o,i]=(0,l.useState)(!1),{toast:c}=(0,eI.dj)(),d=(e,t)=>{try{let a=new Blob([e],{type:"application/octet-stream"}),s=window.URL.createObjectURL(a),l=document.createElement("a");l.href=s,l.download=t,document.body.appendChild(l),l.click(),document.body.removeChild(l),window.URL.revokeObjectURL(s)}catch(e){throw console.error("Error during file download:",e),e}},u=async e=>{if(!r)try{i(!0),null==a||a();let s=await fetch("/api/tables/".concat(t,"?export=true&page=1&limit=999999")),l=await s.json();if(!s.ok)throw Error(l.error||"Failed to fetch data");let{data:r,schema:o}=l;if(!(null==r?void 0:r.length))return void c({title:"No Data",description:"The table is empty.",variant:"destructive"});let u=new Date().toISOString().split("T")[0],m="".concat(t,"_").concat(u,".").concat(e);switch(e){case"json":{let e=JSON.stringify(r,null,2);d(e,m);break}case"csv":{let e=Object.keys(r[0]),t=[e.join(","),...r.map(t=>e.map(e=>{let a=t[e];if(null==a)return"";if("string"==typeof a){let e=a.replace(/"/g,'""');return'"'.concat(e,'"')}return a}).join(","))].join("\n");d(t,m);break}case"sql":{let e="CREATE TABLE IF NOT EXISTS ".concat(t," (\n")+o.map(e=>{let t=[];return e.pk&&t.push("PRIMARY KEY"),e.notnull||t.push("NOT NULL")," ".concat(e.name," ").concat(e.type).concat(t.length?" ".concat(t.join(" ")):"")}).join(",\n")+"\n);\n\n",a=r.map(e=>{let a=Object.keys(e),s=a.map(t=>{let a=e[t];return null===a?"NULL":"string"==typeof a?"'".concat(a.replace(/'/g,"''"),"'"):a});return"INSERT INTO ".concat(t," (").concat(a.join(", "),")\nVALUES (").concat(s.join(", "),");")}).join("\n");d(e+a,m)}}c({title:"Download Complete",description:"".concat(r.length," rows exported as ").concat(e.toUpperCase(),".")}),null==n||n()}catch(e){console.error("Download error:",e),c({variant:"destructive",title:"Download Failed",description:e.message})}finally{i(!1)}};return(0,s.jsxs)(R,{children:[(0,s.jsx)(ek,{children:(0,s.jsxs)(eR,{children:[(0,s.jsx)(eE,{asChild:!0,children:(0,s.jsx)(E,{asChild:!0,children:(0,s.jsx)(L,{variant:"outline",size:"icon",className:"h-8",disabled:o||r,children:o?(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin"}):(0,s.jsx)(eU.A,{className:"h-4 w-4"})})})}),(0,s.jsx)(eT,{children:(0,s.jsx)("p",{children:"Download table data"})})]})}),(0,s.jsxs)(T,{align:"end",children:[(0,s.jsxs)(A,{onClick:()=>u("json"),disabled:r,children:[(0,s.jsx)(eM.A,{className:"mr-2 h-4 w-4"}),(0,s.jsx)("span",{children:"Download JSON"})]}),(0,s.jsxs)(A,{onClick:()=>u("csv"),disabled:r,children:[(0,s.jsx)(eD.A,{className:"mr-2 h-4 w-4"}),(0,s.jsx)("span",{children:"Download CSV"})]}),(0,s.jsxs)(A,{onClick:()=>u("sql"),disabled:r,children:[(0,s.jsx)(eF.A,{className:"mr-2 h-4 w-4"}),(0,s.jsx)("span",{children:"Download SQL"})]})]})]})});ez.displayName="DownloadButton";var eP=a(3527);let eB=function(e){let{schema:t=[],visibleColumns:a=[],onColumnToggle:n}=e,[r,o]=(0,l.useState)("");(0,l.useEffect)(()=>{if(!t||0===t.length)return;let e=t.map(e=>e.name);0===a.length&&e.forEach(e=>n(e))},[t,a.length,n]);let i=t.filter(e=>e.name.toLowerCase().includes(r.toLowerCase())),c=i.map(e=>e.name),d=c.length>0&&c.every(e=>a.includes(e));return(0,s.jsxs)(R,{children:[(0,s.jsx)(E,{asChild:!0,children:(0,s.jsxs)(L,{size:"sm",variant:"outline",className:"font-semibold",children:[(0,s.jsx)(eP.A,{className:"h-4 w-4"}),"Columns (",a.length,")"]})}),(0,s.jsxs)(T,{align:"end",className:"w-64 font-sans",children:[(0,s.jsx)("div",{className:"p-2",children:(0,s.jsx)(b,{placeholder:"Search columns...",value:r,onChange:e=>o(e.target.value),className:"h-8"})}),(0,s.jsxs)("div",{className:"flex items-center space-x-2 px-2 py-1.5 hover:bg-accent rounded-md",children:[(0,s.jsx)(eu,{id:"select-all-columns",checked:d,onCheckedChange:e=>{e?i.forEach(e=>{a.includes(e.name)||n(e.name)}):i.forEach(e=>{a.includes(e.name)&&n(e.name)})},className:"shrink-0"}),(0,s.jsxs)("label",{htmlFor:"select-all-columns",className:"flex-1 cursor-pointer text-sm font-medium",children:["Select All ",r&&"(Filtered)"]})]}),(0,s.jsx)("div",{className:"max-h-[300px] overflow-y-auto",children:i.map(e=>(0,s.jsxs)("div",{className:"flex items-start space-x-2 px-2 py-1.5 hover:bg-accent rounded-md",children:[(0,s.jsx)(eu,{id:"column-".concat(e.name),checked:a.includes(e.name),onCheckedChange:()=>{n(e.name)},className:"mt-1"}),(0,s.jsxs)("label",{htmlFor:"column-".concat(e.name),className:"flex-1 cursor-pointer",children:[(0,s.jsx)("div",{className:"text-sm font-medium",children:e.name}),(0,s.jsx)("div",{className:"text-xs text-muted-foreground",children:e.type.toLowerCase()})]})]},e.name))})]})]})},eW=(0,l.memo)(e=>{let{onAdd:t,onDelete:a,onImport:l,selectedCount:n,disabled:r,readOnly:o,newRecordsCount:i,schema:c,visibleColumns:d,onColumnToggle:u,showFilters:m,onToggleFilters:x,activeFilterCount:h}=e;return(0,s.jsxs)("div",{className:"flex items-center space-x-2",children:[(0,s.jsxs)(L,{size:"sm",variant:m?"default":"outline",className:"font-semibold gap-1",onClick:x,children:[(0,s.jsx)(eN.A,{className:"h-4 w-4"}),"Filters",h>0&&(0,s.jsx)("span",{className:"ml-0.5 text-[10px] font-bold bg-white text-neutral-800 px-1 py-0 rounded-full",children:h})]}),(0,s.jsx)(eB,{schema:c,visibleColumns:d,onColumnToggle:u}),(0,s.jsx)("div",{className:"h-4 border-r mx-2"}),(0,s.jsx)(L,{size:"sm",variant:"default",className:"font-semibold",onClick:t,disabled:o,children:"Add record"}),(0,s.jsx)(ek,{children:(0,s.jsxs)(eR,{children:[(0,s.jsx)(eE,{asChild:!0,children:(0,s.jsxs)(L,{size:"sm",variant:"outline",className:"font-semibold",onClick:l,disabled:r||o,children:[(0,s.jsx)(ew.A,{className:"h-4 w-4"}),"Import"]})}),(0,s.jsx)(eT,{children:(0,s.jsx)("p",{children:"Import CSV or JSON"})})]})}),n>0&&(0,s.jsxs)(L,{size:"sm",className:"font-semibold",onClick:a,variant:"destructive",disabled:r||i>0||o,children:["Delete ",n," ",1===n?"record":"records"]})]})}),eY=(0,l.memo)(e=>{let{totalRows:t}=e;return(0,s.jsxs)("span",{className:"text-sm text-neutral-600",children:[t," ",1===t?"row":"rows"]})}),eq=(0,l.memo)(e=>{let{currentPage:t,totalPages:a,onPageChange:l,disabled:n,newRecordsCount:r}=e;return(0,s.jsxs)("div",{className:"flex items-center space-x-2",children:[(0,s.jsx)(L,{variant:"ghost",size:"icon",onClick:()=>{console.log("Pagination: navigating to previous page",t-1),l(t-1)},disabled:1===t||n||r>0,className:"h-8",children:(0,s.jsx)(ey.A,{className:"h-4 w-4"})}),(0,s.jsxs)("span",{className:"text-sm",children:["Page ",t," of ",a]}),(0,s.jsx)(L,{variant:"ghost",size:"icon",onClick:()=>{console.log("Pagination: navigating to next page",t+1),l(t+1)},disabled:t===a||n||r>0,className:"h-8",children:(0,s.jsx)(eC.A,{className:"h-4 w-4"})})]})}),eH=(0,l.memo)(e=>{let{value:t,onValueChange:a,disabled:l,newRecordsCount:n}=e;return(0,s.jsxs)(eh,{value:t.toString(),onValueChange:a,disabled:l||n>0,children:[(0,s.jsx)(ep,{className:"w-20 h-8",children:(0,s.jsx)(ef,{})}),(0,s.jsxs)(ej,{children:[(0,s.jsx)(ev,{value:"50",children:"50"}),(0,s.jsx)(ev,{value:"100",children:"100"}),(0,s.jsx)(ev,{value:"250",children:"250"})]})]})}),eK=(0,l.memo)(e=>{let{onSave:t,onDiscard:a,totalChanges:l,isSaving:n}=e;return(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)(L,{size:"sm",variant:"ghost",className:"font-semibold",onClick:a,disabled:n,children:"Discard changes"}),(0,s.jsx)(L,{size:"sm",className:"font-semibold bg-green-600 hover:bg-green-700",variant:"default",onClick:t,disabled:n,children:n?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin"}),"Saving..."]}):"Save ".concat(l," ").concat(1===l?"change":"changes")})]})}),eV=e=>{let{selectedCount:t=0,onAdd:a,onDelete:l,onImport:n,onRefresh:r,totalRows:o=0,currentPage:i=1,totalPages:c=1,onPageChange:d,rowsPerPage:u=50,onRowsPerPageChange:m,tableName:x,onDownloadStart:h,onDownloadComplete:f,disabled:p=!1,readOnly:g=!1,editCount:b=0,newRecordsCount:j=0,onSaveChanges:v,onDiscardChanges:N,isSaving:w=!1,schema:y,visibleColumns:C,onColumnToggle:S,showFilters:k=!1,onToggleFilters:R,activeFilterCount:E=0}=e,T=b+j;return(0,s.jsx)("div",{className:"flex items-center justify-between p-3 border-b bg-background font-sans",children:(0,s.jsxs)("div",{className:"flex items-center justify-between w-full",children:[(0,s.jsx)(eW,{onAdd:a,onDelete:l,onImport:n,selectedCount:t,disabled:p||w,readOnly:g,newRecordsCount:j,schema:y,visibleColumns:C,onColumnToggle:S,showFilters:k,onToggleFilters:R,activeFilterCount:E}),(0,s.jsx)("div",{className:"flex items-center gap-4",children:T>0&&!g?(0,s.jsx)(eK,{onSave:v,onDiscard:N,totalChanges:T,isSaving:w}):(0,s.jsxs)("div",{className:"flex items-center space-x-2",children:[(0,s.jsx)(eY,{totalRows:o}),(0,s.jsx)(eq,{currentPage:i,totalPages:c,onPageChange:d,disabled:p,newRecordsCount:j}),(0,s.jsx)(eH,{value:u,onValueChange:e=>{m(parseInt(e,10)),d(1)},disabled:p,newRecordsCount:j}),(0,s.jsx)(e_,{onRefresh:r,disabled:p}),(0,s.jsx)(eL,{tableName:x,onSync:r,disabled:p}),(0,s.jsx)(ez,{tableName:x,onDownloadStart:h,onDownloadComplete:f,disabled:p})]})})]})})};eV.displayName="TableToolbar";let eJ=(0,l.memo)(eV);function eQ(e){let{value:t,row:a,column:n,schema:r,onChange:o,onStartEdit:i,isEditing:c,fkInfo:d,onShowRelation:u}=e,m="string"==typeof t?t.replace(/^'|'$/g,""):t,[x,h]=(0,l.useState)(m),[f,p]=(0,l.useState)("auto"),g=(0,l.useRef)(null),j=(0,l.useRef)(null),v=r.find(e=>e.name===n),N=(0,l.useMemo)(()=>(null==v?void 0:v.check)||null,[v]),w=(0,l.useMemo)(()=>{var e;let t=(null==v||null==(e=v.type)?void 0:e.toLowerCase())||"";return"boolean"===t||"bool"===t||"integer"===t&&n.toLowerCase().startsWith("is_")},[v,n]);(0,l.useEffect)(()=>{h(m)},[m]),(0,l.useEffect)(()=>{if(g.current&&!c){let e=g.current.offsetWidth;p("".concat(e,"px"))}},[c]),(0,l.useEffect)(()=>{c&&j.current&&(j.current.focus(),j.current.select())},[c]),null==v||v.pk;let y=(0,l.useCallback)(e=>{let t=w?"true"===e?1:"false"===e?0:e:e;h(e),null==o||o(t)},[o,w]),C=(0,l.useCallback)(()=>{!c&&i&&i(m)},[c,i,m]),S="border-x border-neutral-200 px-3 py-2.5 text-xs font-mono whitespace-nowrap overflow-hidden bg-white",k=(0,l.useMemo)(()=>null==m?"null":w?1===m||"1"===m||"true"===m?"true":"false":m.toString(),[m,w]);return c?w?(0,s.jsx)(Y,{className:"".concat(S," p-0"),style:{width:f},children:(0,s.jsx)("div",{className:"w-full h-full",children:(0,s.jsxs)(eh,{value:k,onValueChange:y,defaultOpen:c,children:[(0,s.jsx)(ep,{className:"w-full h-9 min-w-0 rounded-none border-0 bg-orange-50 text-orange-900",children:(0,s.jsx)(ef,{placeholder:"Select value"})}),(0,s.jsxs)(ej,{className:"min-w-[8rem]",children:[(0,s.jsx)(ev,{value:"true",children:"true"}),(0,s.jsx)(ev,{value:"false",children:"false"})]})]})})}):(null==N?void 0:N.length)>0?(0,s.jsx)(Y,{className:"".concat(S," p-0"),style:{width:f},children:(0,s.jsx)("div",{className:"w-full h-full",children:(0,s.jsxs)(eh,{value:null!=x?x:N[0],onValueChange:y,defaultOpen:c,children:[(0,s.jsx)(ep,{className:"w-full h-9 min-w-0 rounded-none border-0 bg-orange-50 text-orange-900",children:(0,s.jsx)(ef,{placeholder:"Select ".concat(n)})}),(0,s.jsx)(ej,{className:"min-w-[8rem]",children:N.map(e=>(0,s.jsx)(ev,{value:e,children:e},e))})]})})}):(0,s.jsx)(Y,{className:"".concat(S," p-0"),style:{width:f},children:(0,s.jsx)(b,{ref:j,value:null!=x?x:"",onChange:e=>y(e.target.value),className:"w-full min-w-0 rounded-none border-0 bg-orange-50 text-orange-900"})}):(0,s.jsxs)(Y,{ref:g,className:"".concat(S," relative group cursor-pointer hover:bg-muted/50"),onClick:C,children:[k,d&&(0,s.jsx)("button",{className:"absolute right-0.5 top-1/2 -translate-y-1/2 opacity-0 group-hover:opacity-100 rounded p-0.5 bg-white border hover:bg-blue-50 transition-opacity z-10",onClick:e=>{e.stopPropagation(),null==u||u()},title:"Show related ".concat(d.table),children:(0,s.jsx)(eC.A,{className:"h-3 w-3 text-blue-500"})})]})}var eX=a(3630);function eG(e){return"string"==typeof e?e.replace(/^'|'$/g,""):e}function eZ(e){let{schema:t,onRemove:a,onChange:n,showCheckbox:r=!1,initialData:o={}}=e,[i,c]=(0,l.useState)(()=>t.reduce((e,t)=>{var a;let s=null!=(a=o[t.name])?a:null;if(s=eG(s),1===t.pk&&(null===s||""===s))s="INTEGER"===t.type.toUpperCase()?"[Auto-generated INT]":"[Auto-generated]";else if(null===s){let e=t.dflt_value&&"null"!==t.dflt_value?t.dflt_value:null;if(e){let a=eG(e);s=a.includes("randomblob")||a.includes("lower(hex(")?"[Auto-generated UUID]":"INTEGER"===t.type.toUpperCase()&&1===t.pk&&(a.includes("AUTOINCREMENT")||a.includes("AUTO_INCREMENT")||a.includes("nextval")||""===a||"0"===a)?"[Auto-generated INT]":a}else"INTEGER"===t.type.toUpperCase()?s="":"TEXT"===t.type.toUpperCase()&&(s="")}return e[t.name]=s,e},{})),d=(0,l.useRef)(!1);(0,l.useEffect)(()=>{d.current||(d.current=!0,null==n||n(i))},[]);let u=(e,t)=>{let a={...i,[e]:t};c(a),null==n||n(a)};return(0,s.jsxs)(B,{className:"bg-orange-50/50",children:[(0,s.jsx)(Y,{className:"w-10 p-0",children:(0,s.jsx)("div",{className:"pl-1 flex items-center",children:r?(0,s.jsx)(eu,{disabled:!0}):(0,s.jsx)(L,{variant:"ghost",size:"icon",className:"h-8 w-8 text-orange-700 hover:text-orange-800",onClick:a,children:(0,s.jsx)(eX.A,{className:"h-4 w-4"})})})}),t.map(e=>{var t,a;let l=1===e.pk,n=e.check||null;if((e=>{var t;let a=(null==(t=e.type)?void 0:t.toLowerCase())||"";return"boolean"===a||"bool"===a||"integer"===a&&e.name.toLowerCase().startsWith("is_")})(e)){let t="false";if(null!==e.dflt_value&&void 0!==e.dflt_value){let a=e.dflt_value.toString().toLowerCase();t="1"===a||"true"===a||"'true'"===a||'"true"'===a?"true":"false"}let a=null!==i[e.name]&&void 0!==i[e.name]?1===i[e.name]||!0===i[e.name]||"true"===i[e.name]?"true":"false":t;return(0,s.jsx)(Y,{className:"border-x border-neutral-200 p-0",children:(0,s.jsx)("div",{className:"w-full",children:(0,s.jsxs)(eh,{value:a,onValueChange:t=>u(e.name,+("true"===t)),children:[(0,s.jsx)(ep,{className:"w-full h-9 min-w-0 rounded-none border-0 bg-orange-50 text-orange-900",title:"Default: ".concat(t),children:(0,s.jsx)(ef,{placeholder:"Select ".concat(e.name)})}),(0,s.jsxs)(ej,{className:"min-w-[8rem]",children:[(0,s.jsx)(ev,{value:"true",children:"true"}),(0,s.jsx)(ev,{value:"false",children:"false"})]})]})})},e.name)}if((null==n?void 0:n.length)>0)return(0,s.jsx)(Y,{className:"border-x border-neutral-200 p-0",children:(0,s.jsx)("div",{className:"w-full",children:(0,s.jsxs)(eh,{value:null!=(t=i[e.name])?t:n[0],onValueChange:t=>u(e.name,t),children:[(0,s.jsx)(ep,{className:"w-full h-9 min-w-0 rounded-none border-0 bg-orange-50 text-orange-900",children:(0,s.jsx)(ef,{placeholder:"Select ".concat(e.name)})}),(0,s.jsx)(ej,{className:"min-w-[8rem]",children:n.map(e=>(0,s.jsx)(ev,{value:e,children:e},e))})]})})},e.name);let r=l&&("[Auto-generated INT]"===i[e.name]||"[Auto-generated UUID]"===i[e.name]||"[Auto-generated]"===i[e.name]);return(0,s.jsx)(Y,{className:"border-x border-neutral-200 p-0",children:(0,s.jsx)(b,{value:r?"":null!=(a=i[e.name])?a:"",onChange:t=>u(e.name,t.target.value),disabled:r,className:"w-full min-w-0 rounded-none border-0 px-2 bg-orange-50 text-orange-900 ".concat(r?"opacity-50":""),placeholder:r?"Auto-generated":"Enter ".concat(e.name)})},e.name)})]})}var e$=a(9511);let e0=e$.bL;e$.l9;let e1=e$.ZL;e$.bm;let e2=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(e$.hJ,{ref:t,className:(0,g.cn)("fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",a),...l})});e2.displayName=e$.hJ.displayName;let e5=l.forwardRef((e,t)=>{let{className:a,children:l,...n}=e;return(0,s.jsxs)(e1,{children:[(0,s.jsx)(e2,{}),(0,s.jsxs)(e$.UC,{ref:t,className:(0,g.cn)("fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg",a),...n,children:[l,(0,s.jsxs)(e$.bm,{className:"absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground",children:[(0,s.jsx)(q.A,{className:"h-4 w-4"}),(0,s.jsx)("span",{className:"sr-only",children:"Close"})]})]})]})});e5.displayName=e$.UC.displayName;let e4=e=>{let{className:t,...a}=e;return(0,s.jsx)("div",{className:(0,g.cn)("flex flex-col space-y-1.5 text-center sm:text-left",t),...a})};e4.displayName="DialogHeader";let e3=e=>{let{className:t,...a}=e;return(0,s.jsx)("div",{className:(0,g.cn)("flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",t),...a})};e3.displayName="DialogFooter";let e6=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(e$.hE,{ref:t,className:(0,g.cn)("text-lg font-semibold leading-none tracking-tight",a),...l})});e6.displayName=e$.hE.displayName;let e8=l.forwardRef((e,t)=>{let{className:a,...l}=e;return(0,s.jsx)(e$.VY,{ref:t,className:(0,g.cn)("text-sm text-muted-foreground",a),...l})});e8.displayName=e$.VY.displayName;let e9=function(e){let{isOpen:t,onClose:a,onConfirm:l,selectedCount:n,tableName:r,isDeleting:o,error:c}=e;return(0,s.jsx)(e0,{open:t,onOpenChange:a,children:(0,s.jsxs)(e5,{className:"sm:max-w-md font-sans",children:[(0,s.jsxs)(e4,{children:[(0,s.jsxs)(e6,{className:"mb-2",children:["Delete ",n," ",1===n?"Record":"Records","?"]}),(0,s.jsxs)(e8,{className:"text-neutral-600 font-medium",children:["You're about to delete ",n," ",1===n?"record":"records"," from ",(0,s.jsx)("span",{className:"font-sans font-bold text-neutral-900",children:r}),"."]})]}),c&&(0,s.jsxs)(N,{variant:"destructive",children:[(0,s.jsx)(i.A,{className:"h-4 w-4"}),(0,s.jsx)(w,{children:c})]}),(0,s.jsxs)(e3,{className:"flex gap-1 mt-2 sm:justify-end font-sans",children:[(0,s.jsx)(L,{variant:"outline",onClick:a,disabled:o,children:"Cancel"}),(0,s.jsx)(L,{variant:"destructive",onClick:l,disabled:o,children:o?"Deleting...":"Delete"})]})]})})};function e7(e){var t;let{isOpen:a,onClose:l,error:n}=e;if(!n||!a)return null;let{message:r,icon:o}=n?{message:n.message,icon:i.A}:{message:"An unknown error occurred.",icon:i.A};return(0,s.jsx)(e0,{open:a,onOpenChange:l,children:(0,s.jsxs)(e5,{className:"sm:max-w-md font-sans",children:[(0,s.jsx)(e4,{children:(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)(o,{className:"h-4 w-4 text-red-500"}),(0,s.jsx)(e6,{children:"Error"})]})}),(0,s.jsxs)("div",{className:"space-y-4",children:[(0,s.jsx)("p",{className:"bg-neutral-100 p-2 rounded-md text-neutral-900",children:r}),(null==(t=n.details)?void 0:t.suggestions)&&(0,s.jsxs)("div",{className:"text-sm space-y-1",children:[(0,s.jsx)("p",{className:"font-medium",children:"Suggestions:"}),(0,s.jsx)("ul",{className:"list-disc pl-4 space-y-1",children:n.details.suggestions.map((e,t)=>(0,s.jsx)("li",{children:e},t))})]})]}),(0,s.jsx)("div",{className:"flex justify-end gap-2 mt-4",children:(0,s.jsx)(L,{variant:"default",onClick:l,children:"Got it"})})]})})}var te=a(984);function tt(e){var t;let{open:a,onClose:n,tableName:r,schema:o,onImportComplete:c}=e,{currentDatabase:d}=(0,_.t)(),{toast:u}=(0,eI.dj)(),m=(0,l.useRef)(null),[x,h]=(0,l.useState)(null),[f,p]=(0,l.useState)(""),[g,b]=(0,l.useState)(null),[j,v]=(0,l.useState)(!1),[y,C]=(0,l.useState)(null),S=o.map(e=>e.name),k=(0,l.useCallback)(()=>{h(null),p(""),b(null),C(null),m.current&&(m.current.value="")},[]),R=(0,l.useCallback)(()=>{k(),n()},[k,n]),E=(0,l.useCallback)(async e=>{var t;let a=null==(t=e.target.files)?void 0:t[0];if(a){b(null),C(null),p(a.name);try{let e,t=await a.text();if(a.name.endsWith(".json")){let a=JSON.parse(t);e=Array.isArray(a)?a:[a]}else if(a.name.endsWith(".csv"))e=function(e){let t=e.trim().split("\n");if(t.length<2)return[];let a=t[0].split(",").map(e=>e.trim().replace(/^"|"$/g,""));return t.slice(1).map(e=>{let t=[],s="",l=!1;for(let a=0;a<e.length;a++){let n=e[a];'"'===n?l&&'"'===e[a+1]?(s+='"',a++):l=!l:","!==n||l?s+=n:(t.push(s.trim()),s="")}t.push(s.trim());let n={};return a.forEach((e,a)=>{var s;n[e]=null!=(s=t[a])?s:""}),n})}(t);else throw Error("Unsupported file type. Use .json or .csv");if(0===e.length)throw Error("File contains no data rows");h(e)}catch(e){b(e.message),h(null)}}},[]),T=(0,l.useCallback)(async()=>{if(null==x?void 0:x.length){v(!0);try{let e=null==d?void 0:d.database_name,t=await fetch("/api/tables/".concat(r,"/import"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({rows:x,...e&&{db:e}})}),a=await t.json();if(!t.ok||!a.success)throw Error(a.error||"Import failed");C(a),u({title:"Import Complete",description:"".concat(a.imported," row").concat(1!==a.imported?"s":""," imported.")}),null==c||c()}catch(e){b(e.message)}finally{v(!1)}}},[x,r,d,u,c]),A=x?Object.keys(x[0]):[],O=A.filter(e=>S.includes(e)),I=A.filter(e=>!S.includes(e)),U=null!=(t=null==x?void 0:x.slice(0,5))?t:[];return(0,s.jsx)(e0,{open:a,onOpenChange:e=>!e&&R(),children:(0,s.jsxs)(e5,{className:"max-w-3xl max-h-[85vh] flex flex-col",children:[(0,s.jsx)(e4,{children:(0,s.jsxs)(e6,{children:["Import data into ",(0,s.jsx)("span",{className:"font-mono",children:r})]})}),(0,s.jsxs)("div",{className:"flex-1 overflow-y-auto space-y-4 py-2",children:[!x&&!y&&(0,s.jsxs)("div",{className:"border-2 border-dashed rounded-lg p-8 text-center cursor-pointer hover:bg-muted/50 transition-colors",onClick:()=>{var e;return null==(e=m.current)?void 0:e.click()},children:[(0,s.jsx)(ew.A,{className:"h-8 w-8 mx-auto mb-2 text-muted-foreground"}),(0,s.jsx)("p",{className:"text-sm font-medium",children:"Click to select a file"}),(0,s.jsx)("p",{className:"text-xs text-muted-foreground mt-1",children:"Supports .csv and .json"}),(0,s.jsx)("input",{ref:m,type:"file",accept:".csv,.json",className:"hidden",onChange:E})]}),g&&(0,s.jsxs)(N,{variant:"destructive",children:[(0,s.jsx)(i.A,{className:"h-4 w-4"}),(0,s.jsx)(w,{children:g})]}),y&&(0,s.jsxs)(N,{children:[(0,s.jsx)(te.A,{className:"h-4 w-4"}),(0,s.jsxs)(w,{children:["Imported ",y.imported," row",1!==y.imported?"s":"",".",y.failed>0&&" ".concat(y.failed," failed.")]})]}),x&&!y&&(0,s.jsxs)("div",{className:"space-y-3",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2 text-sm",children:[f.endsWith(".json")?(0,s.jsx)(eM.A,{className:"h-4 w-4"}):(0,s.jsx)(eD.A,{className:"h-4 w-4"}),(0,s.jsx)("span",{className:"font-medium",children:f}),(0,s.jsxs)("span",{className:"text-muted-foreground",children:["— ",x.length," rows"]})]}),(0,s.jsx)(L,{variant:"ghost",size:"sm",onClick:k,children:"Change file"})]}),I.length>0&&(0,s.jsxs)(N,{children:[(0,s.jsx)(i.A,{className:"h-4 w-4"}),(0,s.jsxs)(w,{className:"text-xs",children:["Columns not in table schema (will be ignored):"," ",(0,s.jsx)("span",{className:"font-mono",children:I.join(", ")})]})]}),(0,s.jsxs)("div",{children:[(0,s.jsxs)("p",{className:"text-xs text-muted-foreground mb-1",children:["Preview (first ",Math.min(5,x.length)," of ",x.length," rows)"]}),(0,s.jsx)("div",{className:"border rounded overflow-auto max-h-48",children:(0,s.jsxs)(F,{children:[(0,s.jsx)(z,{children:(0,s.jsx)(B,{children:O.map(e=>(0,s.jsx)(W,{className:"text-xs whitespace-nowrap py-1 px-2",children:e},e))})}),(0,s.jsx)(P,{children:U.map((e,t)=>(0,s.jsx)(B,{children:O.map(t=>(0,s.jsx)(Y,{className:"text-xs py-1 px-2 max-w-32 truncate",children:null===e[t]||void 0===e[t]?(0,s.jsx)("span",{className:"text-muted-foreground italic",children:"null"}):String(e[t])},t))},t))})]})})]})]})]}),(0,s.jsxs)(e3,{children:[(0,s.jsx)(L,{variant:"ghost",onClick:R,disabled:j,children:y?"Close":"Cancel"}),x&&!y&&(0,s.jsx)(L,{onClick:T,disabled:j||0===O.length,children:j?"Importing…":"Import ".concat(x.length," row").concat(1!==x.length?"s":"")})]})]})})}let ta=e=>{let{children:t,width:a,onResize:n,className:r=""}=e,o=(0,l.useRef)(null),i=(0,l.useCallback)(e=>{var t,s,l,r;e.preventDefault(),e.stopPropagation();let i=e.clientX,c=null!=(l=null==(t=o.current)?void 0:t.offsetWidth)?l:a||150,d=null==(s=o.current)?void 0:s.closest("table"),u=null!=(r=null==d?void 0:d.offsetWidth)?r:0;d&&(d.style.width="".concat(u,"px"));let m=c,x=e=>{let t=Math.max(c+(e.clientX-i),80),a=t-m;m=t,o.current&&(o.current.style.width="".concat(t,"px")),d&&0!==a&&(d.style.width="".concat(parseFloat(d.style.width)+a,"px"))},h=e=>{document.removeEventListener("mousemove",x),document.removeEventListener("mouseup",h),document.body.style.cursor="",document.body.style.userSelect="";let t=Math.max(c+(e.clientX-i),80);d&&(d.style.width=""),n(t)};document.body.style.cursor="col-resize",document.body.style.userSelect="none",document.addEventListener("mousemove",x),document.addEventListener("mouseup",h)},[a,n]);return(0,s.jsxs)("th",{ref:o,className:"relative overflow-hidden ".concat(r),style:{width:"".concat(a,"px")},children:[t,(0,s.jsx)("div",{className:"absolute top-0 right-0 h-full w-1.5 cursor-col-resize hover:bg-blue-400/40 z-10",onMouseDown:i,onDoubleClick:e=>{e.preventDefault(),e.stopPropagation()}})]})};var ts=a(2240),tl=a(1165),tn=a(43);function tr(e){let{column:t,sortConfig:a,onSort:l,children:n}=e,r=a[t];return(0,s.jsx)("div",{className:"flex items-center w-full",children:(0,s.jsxs)(L,{variant:"ghost",size:"sm",className:"h-8 hover:bg-neutral-100 w-full justify-between",onClick:()=>{l(t,r?"asc"===r?"desc":null:"asc")},children:[(0,s.jsx)("span",{className:"flex items-center gap-1 font-mono",children:n}),(0,s.jsxs)("span",{className:"shrink-0",children:["asc"===r&&(0,s.jsx)(ts.A,{className:"h-4 w-4"}),"desc"===r&&(0,s.jsx)(tl.A,{className:"h-4 w-4"}),!r&&(0,s.jsx)(tn.A,{className:"h-4 w-4"})]})]})})}function to(e){let{relatedTable:t,relatedKey:a,value:n,colSpan:r,onClose:o}=e,{executeQuery:i}=(0,_.t)(),[c,d]=(0,l.useState)(null),[u,m]=(0,l.useState)(!0),[h,f]=(0,l.useState)(!1);(0,l.useEffect)(()=>{if(!t||!a||null==n){m(!1),f(!0);return}let e=!1;m(!0),f(!1),d(null);let s=String(n).replace(/'/g,"''");return i('SELECT * FROM "'.concat(t,'" WHERE "').concat(a,"\" = '").concat(s,"' LIMIT 1")).then(t=>{var a,s;if(e)return;let l=(null==t||null==(s=t.results)||null==(a=s[0])?void 0:a.results)||[];l.length>0?d(l[0]):f(!0)}).catch(()=>{e||f(!0)}).finally(()=>{e||m(!1)}),()=>{e=!0}},[t,a,n,i]);let p=c?Object.keys(c):[];return(0,s.jsx)(B,{className:"bg-neutral-50 hover:bg-neutral-50",children:(0,s.jsx)(Y,{colSpan:r,className:"py-2 px-4 border-b border-neutral-200",children:(0,s.jsxs)("div",{className:"flex items-start justify-between gap-3",children:[(0,s.jsxs)("div",{className:"flex flex-col gap-1.5 flex-1 min-w-0",children:[(0,s.jsx)("span",{className:"text-[11px] font-semibold px-2 py-0.5 self-start rounded-full bg-neutral-200 text-neutral-600 font-mono",children:t}),u&&(0,s.jsx)(x.A,{className:"h-3.5 w-3.5 animate-spin text-neutral-400"}),!u&&h&&(0,s.jsx)("span",{className:"text-xs text-muted-foreground italic",children:"No related record found"}),!u&&c&&(0,s.jsx)("div",{className:"overflow-x-auto [&::-webkit-scrollbar]:hidden",style:{scrollbarWidth:"none"},children:(0,s.jsxs)("table",{className:"text-[11px] font-mono border border-neutral-200 rounded-md overflow-hidden",children:[(0,s.jsx)("thead",{children:(0,s.jsx)("tr",{className:"bg-neutral-100 border-b border-neutral-200",children:p.map(e=>(0,s.jsx)("th",{className:"px-3 py-1 text-left text-muted-foreground font-medium border-r border-neutral-200 last:border-r-0 whitespace-nowrap",children:e},e))})}),(0,s.jsx)("tbody",{children:(0,s.jsx)("tr",{className:"bg-white",children:p.map(e=>(0,s.jsx)("td",{className:"px-3 py-1.5 border-r border-neutral-200 last:border-r-0 whitespace-nowrap max-w-[220px] truncate",children:null==c[e]?(0,s.jsx)("span",{className:"text-neutral-400 italic",children:"null"}):String(c[e])},e))})})]})})]}),(0,s.jsx)("button",{onClick:o,className:"shrink-0 rounded p-0.5 hover:bg-neutral-200 text-muted-foreground hover:text-neutral-700 transition-colors mt-0.5",title:"Close",children:(0,s.jsx)(q.A,{className:"h-3.5 w-3.5"})})]})})})}var ti=a(2265);let tc=[{value:"equals",label:"= equals"},{value:"not_equals",label:"≠ not equals"},{value:"contains",label:"~ contains"},{value:"starts_with",label:"^ starts with"},{value:"ends_with",label:"$ ends with"},{value:"gt",label:"> greater than"},{value:"gte",label:"≥ greater or equal"},{value:"lt",label:"< less than"},{value:"lte",label:"≤ less or equal"},{value:"is_null",label:"is null"},{value:"is_not_null",label:"is not null"}],td=new Set(["is_null","is_not_null"]),tu=1;function tm(e){let t=e.filter(e=>{var t;return!!e.column&&!!e.operator&&(!!td.has(e.operator)||!!String(null!=(t=e.value)?t:"").trim())}).map(e=>{let t='"'.concat(e.column,'"'),a=e=>String(e).replace(/'/g,"''");switch(e.operator){case"equals":return"".concat(t," LIKE '").concat(a(e.value),"'");case"not_equals":return"".concat(t," NOT LIKE '").concat(a(e.value),"'");case"contains":return"".concat(t," LIKE '%").concat(a(e.value),"%'");case"starts_with":return"".concat(t," LIKE '").concat(a(e.value),"%'");case"ends_with":return"".concat(t," LIKE '%").concat(a(e.value),"'");case"gt":return"".concat(t," > '").concat(a(e.value),"'");case"gte":return"".concat(t," >= '").concat(a(e.value),"'");case"lt":return"".concat(t," < '").concat(a(e.value),"'");case"lte":return"".concat(t," <= '").concat(a(e.value),"'");case"is_null":return"".concat(t," IS NULL");case"is_not_null":return"".concat(t," IS NOT NULL");default:return null}}).filter(Boolean);return 0===t.length?null:t.join(" AND ")}function tx(e){let{schema:t,filters:a,onFiltersChange:n,onOpenInSQL:r}=e,o=t.map(e=>e.name),i=(0,l.useCallback)(()=>{n([...a,function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return{id:tu++,column:e,operator:"contains",value:""}}(o[0]||"")])},[a,n,o]),c=(0,l.useCallback)(e=>{n(a.filter(t=>t.id!==e))},[a,n]),d=(0,l.useCallback)((e,t)=>{n(a.map(a=>a.id===e?{...a,...t}:a))},[a,n]),u=(0,l.useCallback)(()=>{n([])},[n]);return(0,s.jsxs)("div",{className:"border-b bg-neutral-50 px-3 py-2 flex flex-col gap-1.5 font-sans",children:[0===a.length&&(0,s.jsx)("div",{className:"text-xs text-muted-foreground py-0.5",children:"No filters applied. Add a filter below."}),a.map(e=>(0,s.jsxs)("div",{className:"flex items-center gap-1.5",children:[(0,s.jsxs)(eh,{value:e.column,onValueChange:t=>d(e.id,{column:t}),children:[(0,s.jsx)(ep,{className:"h-7 text-xs w-36 min-w-0",children:(0,s.jsx)(ef,{placeholder:"Column"})}),(0,s.jsx)(ej,{children:o.map(e=>(0,s.jsx)(ev,{value:e,className:"text-xs font-mono",children:e},e))})]}),(0,s.jsxs)(eh,{value:e.operator,onValueChange:t=>d(e.id,{operator:t}),children:[(0,s.jsx)(ep,{className:"h-7 text-xs w-36 min-w-0",children:(0,s.jsx)(ef,{})}),(0,s.jsx)(ej,{children:tc.map(e=>(0,s.jsx)(ev,{value:e.value,className:"text-xs",children:e.label},e.value))})]}),!td.has(e.operator)&&(0,s.jsx)(b,{className:"h-7 text-xs w-44 min-w-0 font-mono",placeholder:"value",value:e.value,onChange:t=>d(e.id,{value:t.target.value})}),(0,s.jsx)("button",{className:"shrink-0 rounded p-0.5 hover:bg-neutral-200 text-muted-foreground transition-colors",onClick:()=>c(e.id),title:"Remove filter",children:(0,s.jsx)(q.A,{className:"h-3.5 w-3.5"})})]},e.id)),(0,s.jsxs)("div",{className:"flex items-center gap-2 pt-0.5",children:[(0,s.jsxs)(L,{size:"sm",variant:"ghost",className:"h-6 text-xs px-2 gap-1",onClick:i,children:[(0,s.jsx)(ti.A,{className:"h-3 w-3"}),"Add filter"]}),a.length>0&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)(L,{size:"sm",variant:"ghost",className:"h-6 text-xs px-2 gap-1",onClick:r,children:[(0,s.jsx)(h.A,{className:"h-3 w-3"}),"Open in SQL"]}),(0,s.jsx)(L,{size:"sm",variant:"ghost",className:"h-6 text-xs px-2 text-red-500 hover:text-red-600 hover:bg-red-50",onClick:u,children:"Clear filters"})]})]})]})}function th(e,t){try{let a=localStorage.getItem("table-columns-".concat(e));if(a){let e=JSON.parse(a).filter(e=>t.includes(e));if(e.length>0)return e}}catch(e){}return localStorage.setItem("table-columns-".concat(e),JSON.stringify(t)),t}function tf(e){let{tableName:t,onOpenInSQL:a}=e,{executeQuery:n,getCachedTable:r,updateTableCache:o,invalidateTable:c,switchTable:d,activeTable:u,updateTableData:m,setOperationStatus:h,sourceMode:f}=(0,_.t)(),{toast:p}=(0,eI.dj)(),[g,b]=(0,l.useState)(()=>[]),[j,v]=(0,l.useState)([]),[y,C]=(0,l.useState)(0),[S,k]=(0,l.useState)(!0),[R,E]=(0,l.useState)(!1),[T,A]=(0,l.useState)(null),[O,I]=(0,l.useState)(new Map),[L,U]=(0,l.useState)(new Set),[q,H]=(0,l.useState)([]),[K,V]=(0,l.useState)(1),[J,Q]=(0,l.useState)(50),[X,G]=(0,l.useState)(1),[Z,$]=(0,l.useState)(new Set),ee=(0,l.useRef)(null),[et,ea]=(0,l.useState)(!1),[es,en]=(0,l.useState)(!1),[er,eo]=(0,l.useState)(null),[ei,ec]=(0,l.useState)(!1),[ed,em]=(0,l.useState)(!1),[ex,eh]=(0,l.useState)(null),[ef,ep]=(0,l.useState)({}),[eg,eb]=(0,l.useState)([]),[ej,ev]=(0,l.useState)(!1),[eN,ew]=(0,l.useState)(null),[ey,eC]=(0,l.useState)(0),[eS,ek]=(0,l.useState)({}),[eR,eE]=(0,l.useState)(null),eT=(0,l.useRef)(null),[eA,e_]=(0,l.useState)({}),[eO,eL]=(0,l.useState)([]),eU=ej&&null!==eN,eM=eU?eN:g,eD=eU?ey:y,eF=eU?Math.max(1,Math.ceil(ey/J)):X,ez=j.filter(e=>eO.includes(e.name)),eP=ez.reduce((e,t)=>e+(eA[t.name]||150),40),eB=O.size>0||q.length>0,eW="remote"===f;(0,l.useEffect)(()=>{if(t&&1){let e=localStorage.getItem("table-widths-".concat(t));if(e)try{e_(JSON.parse(e))}catch(e){}}},[t]);let eY=(0,l.useCallback)(async e=>{try{var t,a;let s=await n('PRAGMA foreign_key_list("'.concat(e,'")')),l=(null==s||null==(a=s.results)||null==(t=a[0])?void 0:t.results)||[];if(l.length>0){let e={};l.forEach(t=>{e[t.from]={table:t.table,to:t.to}}),ek(e)}else ek({})}catch(e){ek({})}},[n]),eq=(0,l.useCallback)(async function(){let e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];if(!t)return;let a=r(t,K,J);if(a&&!e){b(a.data),v(a.schema),C(a.meta.total),G(a.meta.totalPages);return}k(!0),A(null),$(new Set);try{let a=await d(t,{page:K,limit:J,forceFetch:e});a.meta.page=K;let s=a.schema||[];return b(Array.isArray(a.data)?a.data:[]),v(s),C(a.meta.total),G(a.meta.totalPages),eL(th(t,s.map(e=>e.name))),eE(null),eY(t),!0}catch(e){throw console.error("Error loading table data:",e),A(e.message||String(e)),p({variant:"destructive",title:"Error",description:"Failed to load table: ".concat(e.message)}),e}finally{k(!1)}},[t,K,J,r,d,p,eY]),eH=(0,l.useCallback)(e=>{eL(a=>{let s=a.includes(e)?a.filter(t=>t!==e):[...a,e];return localStorage.setItem("table-columns-".concat(t),JSON.stringify(s)),s})},[t]),eK=(0,l.useCallback)((e,a)=>{e_(s=>{let l={...s,[e]:a};return localStorage.setItem("table-widths-".concat(t),JSON.stringify(l)),l})},[t]),eV=(0,l.useCallback)(async()=>{try{$(new Set),c(t),await eq(!0),p({title:"Success",description:"Table data refreshed"})}catch(e){console.error("Refresh failed:",e),p({variant:"destructive",title:"Refresh Failed",description:e.message})}},[eq,c,t,p]),eX=(0,l.useCallback)((e,a)=>{ep(t=>({...t,[e]:a}));let s=a&&g.length?[...g].sort((t,s)=>{let l=t[e],n=s[e];if(null==l&&null==n)return 0;if(null==l)return 1;if(null==n)return -1;if("number"==typeof l&&"number"==typeof n)return"asc"===a?l-n:n-l;let r=String(l).toLowerCase(),o=String(n).toLowerCase();return"asc"===a?r.localeCompare(o):o.localeCompare(r)}):g;b(s);let l=r(t);l&&o(t,s,l.schema,{...l.meta,currentPage:K,limit:J},K,J)},[g,t,r,o]),eG=(0,l.useCallback)((e,t,a)=>{let s="".concat(e,"::").concat(t);I(e=>new Map(e).set(s,{value:a,originalValue:a})),U(t=>new Set(t).add(e))},[]),e$=(0,l.useCallback)((e,t,a)=>{var s,l;let n="".concat(e,"::").concat(t),r=g[e],o=null!=(l=null==(s=O.get(n))?void 0:s.originalValue)?l:r[t];if(a===o){let t=new Map(O);if(t.delete(n),I(t),!Array.from(t.keys()).some(t=>t.startsWith("".concat(e,"::")))){let t=new Set(L);t.delete(e),U(t)}}else I(e=>new Map(e).set(n,{value:a,originalValue:o})),U(t=>new Set(t).add(e))},[g,O,L]),e0=(0,l.useCallback)(()=>{if(eW)return void p({variant:"destructive",title:"Remote is read-only",description:"Switch source to Local to add records."});H(e=>[...e,{}])},[eW,p]),e1=(0,l.useCallback)(e=>{H(t=>t.filter((t,a)=>a!==e))},[]),e2=(0,l.useCallback)((e,t)=>{H(a=>{let s=[...a];return s[e]=t,s})},[]),e5=async function(){let e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];E(!0);try{if(q.length>0){let e=0,a=j.filter(e=>"datetime"===e.type.toLowerCase()).map(e=>e.name);for(let s of q){let l=Object.keys(s).filter(e=>{let t=s[e];if(null===t||""===t||"null"===t){let t=j.find(t=>t.name===e);return!!((null==t?void 0:t.dflt_value)&&(t.dflt_value.includes("datetime(")||t.dflt_value.includes("randomblob")||t.dflt_value.includes("lower(hex(")))}if((null==t?void 0:t.toString())==="[Auto-generated INT]"){let t=j.find(t=>t.name===e);if(t&&1===t.pk&&"INTEGER"===t.type.toUpperCase())return!1}return null==t||t.toString(),!0});if(a.forEach(e=>{l.includes(e)||l.push(e)}),0===l.length)continue;let r=e=>(null==e?void 0:e.dflt_value)&&"null"!==e.dflt_value&&null!==e.dflt_value,o=l.map(e=>{if(a.includes(e))return"CURRENT_TIMESTAMP";let t=s[e];if(null===t||""===t||"null"===t){let t=j.find(t=>t.name===e);return r(t)?t.dflt_value:"NULL"}let l=t.toString();if("[Auto-generated UUID]"===l){let t=j.find(t=>t.name===e);if(r(t))return t.dflt_value}if("[Auto-generated]"===l){let t=j.find(t=>t.name===e);return r(t)?t.dflt_value:"lower(hex(randomblob(8)))"}if("[Auto-generated INT]"===l){let t=j.find(t=>t.name===e);if(t&&1===t.pk&&"INTEGER"===t.type.toUpperCase())return null}return l.includes("randomblob")||l.includes("CURRENT_TIMESTAMP")||l.includes("lower(hex(")||l.includes("datetime(")||l.startsWith("(")&&l.endsWith(")")?l:"'".concat(l.replace(/'/g,"''"),"'")}),i="INSERT INTO ".concat(t," (").concat(l.join(", "),") VALUES (").concat(o.join(", "),");");await n(i),e++}if(e>0){let a=await m(t);b(a.data),C(a.meta.total),G(Math.ceil(a.meta.total/J)),h("success","Added ".concat(e," ").concat(1===e?"record":"records"," \xb7 ").concat(t)),p({title:"Success",description:"Added ".concat(e," ").concat(1===e?"record":"records")})}H([])}if(O.size>0&&(e||!ex)){var a;let e=j.find(e=>1===e.pk),s=null==e?void 0:e.name;if(!s)throw Error("No primary key found in table schema");let l=(null==e||null==(a=e.type)?void 0:a.toUpperCase())==="INTEGER",r=new Map;O.forEach((e,t)=>{let a=t.indexOf("::"),l=t.substring(0,a),n=t.substring(a+2),o=g[l];r.has(o[s])||r.set(o[s],new Map),r.get(o[s]).set(n,e.value)});let o=0;for(let[e,a]of r){let r=Array.from(a.entries()).map(e=>{let[t,a]=e;return null===a||""===a||"null"===a?"".concat(t," = NULL"):"CURRENT_TIMESTAMP"===a?"".concat(t," = CURRENT_TIMESTAMP"):"".concat(t," = '").concat(a.toString().replace(/'/g,"''"),"'")}).join(", "),i=l?e:"'".concat(String(e).replace(/'/g,"''"),"'"),c="\n UPDATE ".concat(t,"\n SET ").concat(r,"\n WHERE ").concat(s," = ").concat(i,";\n ");await n(c),o++}if(o>0){let e=await m(t);b(e.data),C(e.meta.total),G(Math.ceil(e.meta.total/J)),h("success","Updated ".concat(o," ").concat(1===o?"record":"records"," \xb7 ").concat(t)),p({title:"Success",description:"Updated ".concat(o," ").concat(1===o?"record":"records")})}}I(new Map),U(new Set),eh(null)}catch(s){let e=el(s),a={...e,stderr:s.stderr,stdout:s.stdout};h("error",e.message||"Save failed","Error saving changes to ".concat(t)),eh(a),em(!0)}finally{E(!1)}},e4=(0,l.useCallback)(async()=>{if(!eW&&t){en(!0),eo(null);try{var e;let a,s=null==(e=j.find(e=>1===e.pk))?void 0:e.name;if(!s)throw Error("No primary key found in table schema");let l=Array.from(Z).map(e=>g[e][s]);if(0===l.length)return;let r=e=>null==e||"null"===e,o=l.filter(r),i=l.filter(e=>!r(e)),c=i.map(e=>"string"==typeof e&&isNaN(e)?"'".concat(e.replace(/'/g,"''"),"'"):e);a=o.length>0&&i.length>0?"DELETE FROM ".concat(t," WHERE ").concat(s," IS NULL OR ").concat(s," IN (").concat(c.join(","),");"):o.length>0?"DELETE FROM ".concat(t," WHERE ").concat(s," IS NULL;"):"DELETE FROM ".concat(t," WHERE ").concat(s," IN (").concat(c.join(","),");"),await n(a);let d=new Set(l),u=g.filter(e=>!d.has(e[s]));b(u),C(e=>e-l.length),G(Math.ceil((y-l.length)/J)),K>1&&g.length===l.length&&V(e=>e-1),ea(!1),$(new Set),h("success","Deleted ".concat(l.length," ").concat(1===l.length?"record":"records"," \xb7 ").concat(t)),p({title:"Success",description:"Deleted ".concat(l.length," ").concat(1===l.length?"record":"records")})}catch(e){console.error("Delete operation failed:",e),h("error",e.message||"Delete failed","Error deleting from ".concat(t)),eo(e.message||String(e))}finally{en(!1)}}},[t,Z,g,j,n,m,eq,K,J,y,p,r,o,h,eW]);(0,l.useEffect)(()=>{if(!t)return;I(new Map),U(new Set),H([]),A(null),$(new Set),V(1);let e=r(t,1,J);e?(b(e.data),v(e.schema),C(e.meta.total),G(e.meta.totalPages),eL(th(t,e.schema.map(e=>e.name))),eE(null),eY(t)):eq(!1)},[t,J,f]),(0,l.useEffect)(()=>{if(u===t){let e=r(t);e&&(b(e.data),v(e.schema),C(e.meta.total),G(e.meta.totalPages))}},[u,t,r]),(0,l.useEffect)(()=>{if(t&&!S){var e;let a=r(t,K,J);a&&K===(null==(e=a.meta)?void 0:e.currentPage)||eq(!1)}},[t,K,J,S,eq,r]),(0,l.useEffect)(()=>{ee.current&&(ee.current.indeterminate=Z.size>0&&Z.size<eM.length)},[Z.size,eM.length]);let e3=(0,l.useCallback)(e=>{$(e?new Set(eM.map((e,t)=>t)):new Set)},[eM]),e6=(0,l.useCallback)((e,t)=>{$(a=>{let s=new Set(a);return t?s.add(e):s.delete(e),s})},[]);(0,l.useCallback)(e=>{e_(e),localStorage.setItem("table-widths-".concat(t),JSON.stringify(e))},[t]);let e8=(0,l.useCallback)(async e=>{let a=tm(e);if(!a){ew(null),eC(0);return}try{var s,l,r,o,i,c,d,u,m;let e=(null==(s=j.find(e=>1===e.pk))?void 0:s.name)||(null==(l=j[0])?void 0:l.name)||"rowid",x=await n('SELECT COUNT(*) as total FROM "'.concat(t,'" WHERE ').concat(a)),h=null!=(m=null==x||null==(c=x.results)||null==(i=c[0])||null==(o=i.results)||null==(r=o[0])?void 0:r.total)?m:0,f=await n('SELECT * FROM "'.concat(t,'" WHERE ').concat(a,' ORDER BY "').concat(e,'" DESC LIMIT ').concat(J," OFFSET ").concat((K-1)*J)),p=(null==f||null==(u=f.results)||null==(d=u[0])?void 0:d.results)||[];eC(h),ew(p)}catch(e){ew([]),eC(0)}},[t,j,n,J,K]);return((0,l.useEffect)(()=>(eT.current&&clearTimeout(eT.current),ej&&eg.length>0?eg.some(e=>{var t;return e.column&&e.operator&&(td.has(e.operator)||String(null!=(t=e.value)?t:"").trim())})&&(eT.current=setTimeout(()=>e8(eg),400)):(ew(null),eC(0)),()=>{eT.current&&clearTimeout(eT.current)}),[ej,eg,e8]),(0,l.useEffect)(()=>{eb([]),ew(null),eC(0)},[t]),T)?(0,s.jsx)("div",{className:"p-4",children:(0,s.jsxs)(N,{variant:"destructive",children:[(0,s.jsx)(i.A,{className:"h-4 w-4"}),(0,s.jsxs)(w,{children:["Failed to load table: ",T]})]})}):(0,s.jsxs)("div",{className:"h-full flex flex-col",children:[(0,s.jsx)(eJ,{selectedCount:Z.size,totalRows:eD,currentPage:K,totalPages:eF,rowsPerPage:J,onPageChange:V,onRowsPerPageChange:Q,onAdd:e0,onDelete:()=>ea(!0),onImport:()=>{if(eW)return void p({variant:"destructive",title:"Remote is read-only",description:"Switch source to Local to import records."});ec(!0)},onRefresh:eV,tableName:t,disabled:eB,readOnly:eW,editCount:O.size,newRecordsCount:q.length,isSaving:R,onSaveChanges:e5,onDiscardChanges:()=>{I(new Map),U(new Set),H([]),eh(null),p({description:"Changes discarded"})},schema:j,visibleColumns:eO,onColumnToggle:eH,showFilters:ej,onToggleFilters:()=>ev(e=>!e),activeFilterCount:eg.filter(e=>e.column&&e.operator).length}),ej&&(0,s.jsx)(tx,{schema:j,filters:eg,onFiltersChange:eb,onOpenInSQL:()=>{let e=tm(eg);if(e&&a){var s,l;let n=(null==(s=j.find(e=>1===e.pk))?void 0:s.name)||(null==(l=j[0])?void 0:l.name)||"rowid";a('SELECT * FROM "'.concat(t,'" WHERE ').concat(e,' ORDER BY "').concat(n,'" DESC LIMIT ').concat(J,";"))}}}),(0,s.jsx)("div",{className:"flex-1 overflow-hidden bg-background font-mono",children:(0,s.jsxs)(M,{className:"w-full h-full",children:[(0,s.jsx)("div",{className:"w-max min-w-full pb-4",children:(0,s.jsxs)(F,{style:{tableLayout:"fixed",width:"".concat(eP,"px")},children:[!S&&(0,s.jsx)(z,{className:"bg-neutral-100 sticky top-0 z-30",children:(0,s.jsxs)(B,{children:[(0,s.jsx)(W,{className:"p-0 border-r border-neutral-200",style:{width:"40px",minWidth:"40px"},children:(0,s.jsx)("div",{className:"py-0.5 px-3 flex items-center",children:(0,s.jsx)(eu,{ref:ee,checked:Z.size===eM.length&&eM.length>0,onCheckedChange:e3,disabled:eB})})}),ez.map(e=>{var t;return(0,s.jsx)(ta,{width:eA[e.name]||150,onResize:t=>eK(e.name,t),className:"py-0.5 whitespace-nowrap border-r border-neutral-200",children:(0,s.jsxs)(tr,{column:e.name,sortConfig:ef,onSort:eX,children:[(0,s.jsx)("span",{className:"text-xs font-bold text-foreground",children:e.name}),(0,s.jsx)("span",{className:"text-xs text-muted-foreground",children:null==(t=e.type)?void 0:t.toLowerCase()})]})},e.name)})]})}),(0,s.jsxs)(P,{children:[S&&(0,s.jsx)(B,{className:"hover:bg-transparent",children:(0,s.jsx)(Y,{colSpan:ez.length+1,className:"h-64",children:(0,s.jsx)("div",{className:"h-full w-full flex items-center justify-center",children:(0,s.jsx)(x.A,{className:"h-6 w-6 animate-spin text-primary"})})})}),!S&&(0,s.jsxs)(s.Fragment,{children:[q.map((e,t)=>(0,s.jsx)(eZ,{schema:ez,onRemove:()=>e1(t),onChange:e=>e2(t,e)},"new-".concat(t))),0===eM.length&&0===q.length?(0,s.jsx)(B,{className:"hover:bg-transparent",children:(0,s.jsx)(Y,{colSpan:ez.length+1,className:"h-64 text-center text-muted-foreground",children:eU?"No rows match the current filters":"Table is empty"})}):eM.map((e,t)=>(0,s.jsxs)(l.Fragment,{children:[(0,s.jsxs)(B,{className:Z.has(t)?"bg-blue-50":"bg-white",children:[(0,s.jsx)(Y,{className:"p-0",children:(0,s.jsx)("div",{className:"px-3 py-2.5 flex items-center",children:(0,s.jsx)(eu,{checked:Z.has(t),onCheckedChange:e=>e6(t,e),disabled:eB||eW})})}),ez.map(a=>{let l="".concat(t,"::").concat(a.name),n=O.get(l);return(0,s.jsx)(eQ,{value:n?n.value:e[a.name],row:e,column:a.name,schema:j,onChange:e=>e$(t,a.name,e),onStartEdit:eU||eW?void 0:e=>eG(t,a.name,e),isEditing:!eU&&O.has(l),style:{width:"".concat(eA[a.name]||150,"px")},fkInfo:eS[a.name],onShowRelation:eS[a.name]?()=>eE({rowIndex:t,relatedTable:eS[a.name].table,relatedKey:eS[a.name].to,value:e[a.name]}):void 0},a.name)})]}),(null==eR?void 0:eR.rowIndex)===t&&(0,s.jsx)(to,{relatedTable:eR.relatedTable,relatedKey:eR.relatedKey,value:eR.value,colSpan:ez.length+1,onClose:()=>eE(null)})]},t))]})]})]})}),(0,s.jsx)(D,{orientation:"horizontal"})]})}),(0,s.jsx)(e9,{isOpen:et,onClose:()=>{ea(!1),eo(null)},onConfirm:e4,selectedCount:Z.size,tableName:t,isDeleting:es,error:er}),(0,s.jsx)(e7,{isOpen:ed,onClose:()=>em(!1),error:ex,onOpenInSQL:()=>{em(!1)},onUpdateAnyway:async()=>{em(!1),await e5(!0)},showSQLOption:(null==ex?void 0:ex.type)==="NULLABLE_PK"}),(0,s.jsx)(tt,{open:ei,onClose:()=>ec(!1),tableName:t,schema:j,onImportComplete:()=>{ec(!1),eV()}})]})}function tp(e){let{migration:t}=e;return t.orphaned?(0,s.jsxs)("span",{className:"inline-flex items-center gap-1 text-xs font-medium text-amber-600",children:[(0,s.jsx)(i.A,{className:"h-3 w-3"})," Orphaned"]}):t.applied?(0,s.jsxs)("span",{className:"inline-flex items-center gap-1 text-xs font-medium text-green-600",children:[(0,s.jsx)(te.A,{className:"h-3 w-3"})," Applied"]}):(0,s.jsxs)("span",{className:"inline-flex items-center gap-1 text-xs font-medium text-amber-600",children:[(0,s.jsx)(p.A,{className:"h-3 w-3"})," Pending"]})}function tg(){let{currentDatabase:e,executeQuery:t,refreshTables:a}=(0,_.t)(),{toast:n}=(0,eI.dj)(),[r,o]=(0,l.useState)([]),[c,d]=(0,l.useState)(""),[u,m]=(0,l.useState)(!0),[h,f]=(0,l.useState)(!1),[p,g]=(0,l.useState)(null),b=(0,l.useCallback)(async()=>{m(!0),g(null);try{let t=null==e?void 0:e.database_name,a=t?"?db=".concat(encodeURIComponent(t)):"",s=await fetch("/api/migrations/list".concat(a)),l=await s.json();if(!s.ok||!l.success)throw Error(l.error||"Failed to load migrations");o(l.migrations),d(l.migrationsDir)}catch(e){g(e.message)}finally{m(!1)}},[e]);(0,l.useEffect)(()=>{b()},[b]);let j=r.filter(e=>!e.applied&&!e.orphaned),v=(0,l.useCallback)(async()=>{if(0===j.length)return;f(!0);let t=0;try{for(let a of j){await fetch("/api/migrations/list?file=".concat(encodeURIComponent(a.name)).concat((null==e?void 0:e.database_name)?"&db=".concat(encodeURIComponent(e.database_name)):""));let s=await fetch("/api/migrations/sync-files",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({migrationName:a.name,db:null==e?void 0:e.database_name})}),l=await s.json();if(!s.ok||!l.success)throw Error("Failed to apply ".concat(a.name,": ").concat(l.error||"Unknown error"));t++}n({title:"Migrations Applied",description:"".concat(t," migration").concat(1!==t?"s":""," applied successfully.")}),await a(),await b()}catch(e){n({variant:"destructive",title:"Apply Failed",description:e.message})}finally{f(!1)}},[j,e,n,a,b]);return(0,s.jsxs)("div",{className:"h-full flex flex-col",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between p-4 border-b",children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("h2",{className:"text-sm font-semibold",children:"Migrations"}),c&&(0,s.jsx)("p",{className:"text-xs text-muted-foreground mt-0.5 font-mono",children:c})]}),(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[j.length>0&&(0,s.jsx)(L,{size:"sm",onClick:v,disabled:h,className:"font-semibold",children:h?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin"})," Applying…"]}):"Apply ".concat(j.length," pending")}),(0,s.jsx)(L,{variant:"outline",size:"icon",className:"h-8",onClick:b,disabled:u,children:(0,s.jsx)(eA.A,{className:"h-4 w-4 ".concat(u?"animate-spin":"")})})]})]}),(0,s.jsxs)("div",{className:"flex-1 overflow-auto",children:[p&&(0,s.jsx)("div",{className:"p-4",children:(0,s.jsxs)(N,{variant:"destructive",children:[(0,s.jsx)(i.A,{className:"h-4 w-4"}),(0,s.jsx)(w,{children:p})]})}),!u&&!p&&0===r.length&&(0,s.jsxs)("div",{className:"flex flex-col items-center justify-center h-64 text-muted-foreground gap-2",children:[(0,s.jsx)(eF.A,{className:"h-8 w-8"}),(0,s.jsx)("p",{className:"text-sm",children:"No migration files found"}),(0,s.jsx)("p",{className:"text-xs",children:"Migration files appear here when you run DDL statements in the SQL editor"})]}),!p&&r.length>0&&(0,s.jsxs)(F,{children:[(0,s.jsx)(z,{className:"bg-neutral-100 sticky top-0 z-10",children:(0,s.jsxs)(B,{children:[(0,s.jsx)(W,{className:"text-xs font-bold",children:"Migration file"}),(0,s.jsx)(W,{className:"text-xs font-bold w-28",children:"Status"}),(0,s.jsx)(W,{className:"text-xs font-bold w-44",children:"Applied at"}),(0,s.jsx)(W,{className:"text-xs font-bold w-20",children:"Checksum"})]})}),(0,s.jsx)(P,{children:r.map(e=>(0,s.jsxs)(B,{className:e.orphaned?"opacity-60":"",children:[(0,s.jsx)(Y,{className:"font-mono text-xs",children:e.name}),(0,s.jsx)(Y,{children:(0,s.jsx)(tp,{migration:e})}),(0,s.jsx)(Y,{className:"text-xs text-muted-foreground",children:e.applied_at?new Date(e.applied_at).toLocaleString():"—"}),(0,s.jsx)(Y,{className:"font-mono text-xs text-muted-foreground",children:e.checksum?e.checksum.slice(0,8):"—"})]},e.name))})]})]})]})}let tb=(0,a(7909).default)(()=>Promise.all([a.e(714),a.e(862),a.e(95),a.e(950)]).then(a.bind(a,7950)).then(e=>({default:e.SchemaERD})),{loadableGenerated:{webpack:()=>[7950]},ssr:!1,loading:()=>(0,s.jsx)("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center",background:"#0d1117",color:"#8b949e"},children:(0,s.jsx)("span",{style:{fontSize:14},children:"Loading…"})})});function tj(){let{currentDatabase:e,sourceMode:t}=(0,_.t)();return(0,s.jsx)(tb,{db:null==e?void 0:e.database_name,source:t})}let tv={bg:"#ffffff",bg2:"#f9f8f6",border:"#e8e5e0",border2:"#d4d0c8",text:"#1a1714",text2:"#5a5650",text3:"#9a9590",orange:"#d4581e",orangeBg:"#fff4ee",orangeBorder:"#fddcc8"},tN=["January","February","March","April","May","June","July","August","September","October","November","December"],tw={2:[3,7,10,14,16],1:[5,12,18,22]};function ty(e){return String(e).padStart(2,"0")}function tC(e){let{year:t,month:a,selDay:l,today:n,earliest:r,onSelect:o,onPrev:i,onNext:c}=e,d=new Date(t,a,1).getDay(),u=new Date(t,a+1,0).getDate(),m=tw[a]||[],x=n.getUTCFullYear(),h=n.getUTCMonth(),f=n.getUTCDate(),p=[];for(let e=0;e<d;e++)p.push((0,s.jsx)("div",{},"b".concat(e)));for(let e=1;e<=u;e++){let n=t>x||t===x&&a>h||t===x&&a===h&&e>f,i=new Date(t,a,e).getTime(),c=!!r&&i<new Date(r.getFullYear(),r.getMonth(),r.getDate()).getTime(),d=t===x&&a===h&&e===f,u=e===l,g=m.includes(e),b=n||c;p.push((0,s.jsxs)("div",{onClick:b?void 0:()=>o(e),style:{fontSize:11,textAlign:"center",padding:"4px 0",borderRadius:4,cursor:b?"default":"pointer",color:b?tv.border2:u?tv.bg:d?tv.text:tv.text2,background:u?tv.text:"transparent",fontWeight:d?600:void 0,pointerEvents:b?"none":void 0,position:"relative",userSelect:"none"},className:b||u?"":"hover:bg-[#f2f0ec]",children:[e,g&&!n&&(0,s.jsx)("span",{style:{position:"absolute",bottom:1,left:"50%",transform:"translateX(-50%)",width:3,height:3,borderRadius:"50%",background:u?tv.bg:tv.orange,display:"block"}})]},e))}return(0,s.jsxs)("div",{style:{padding:"14px 16px",borderBottom:"1px solid ".concat(tv.border)},children:[(0,s.jsxs)("div",{style:{display:"flex",alignItems:"center",justifyContent:"space-between",marginBottom:10},children:[(0,s.jsxs)("span",{style:{fontSize:13,fontWeight:500,color:tv.text},children:[tN[a]," ",t]}),(0,s.jsx)("div",{style:{display:"flex",gap:3},children:[["‹",i],["›",c]].map(e=>{let[t,a]=e;return(0,s.jsx)("button",{onClick:a,style:{width:24,height:24,border:"1px solid ".concat(tv.border),borderRadius:6,background:"transparent",cursor:"pointer",fontSize:14,color:tv.text2,display:"flex",alignItems:"center",justifyContent:"center",lineHeight:1},className:"hover:bg-[#f9f8f6]",children:t},t)})})]}),(0,s.jsxs)("div",{style:{display:"grid",gridTemplateColumns:"repeat(7, 1fr)",gap:1},children:[["Su","Mo","Tu","We","Th","Fr","Sa"].map(e=>(0,s.jsx)("div",{style:{fontSize:10,fontWeight:500,color:tv.text3,textAlign:"center",padding:"3px 0"},children:e},e)),p]})]})}function tS(e){let{kind:t,text:a}=e,l={a:{bg:"#f0fdf4",color:"#15803d",markBg:"#f0fdf4",markColor:"#16a34a",mark:"+"},r:{bg:"#fef2f2",color:"#b91c1c",markBg:"#fef2f2",markColor:"#dc2626",mark:"−"},c:{bg:"#f9fafb",color:tv.text3,markBg:"#f9fafb",markColor:tv.text3,mark:"~"}}[t];return(0,s.jsxs)("div",{style:{display:"flex",alignItems:"stretch",fontSize:11,fontFamily:"ui-monospace,SF Mono,monospace",borderRadius:5,overflow:"hidden",marginBottom:2},children:[(0,s.jsx)("div",{style:{width:22,minWidth:22,display:"flex",alignItems:"center",justifyContent:"center",fontWeight:600,fontSize:12,background:l.markBg,color:l.markColor},children:l.mark}),(0,s.jsx)("div",{style:{flex:1,padding:"4px 8px",whiteSpace:"nowrap",overflow:"hidden",textOverflow:"ellipsis",background:l.bg,color:l.color},children:a})]})}function tk(e){let{type:t,children:a}=e;return(0,s.jsx)("span",{style:{fontSize:10,padding:"1px 6px",borderRadius:3,fontFamily:"ui-monospace,SF Mono,monospace",...{rm:{background:"#fef2f2",color:"#dc2626",border:"1px solid #fecaca"},add:{background:"#f0fdf4",color:"#16a34a",border:"1px solid #bbf7d0"},ch:{background:"#fffbeb",color:"#d97706",border:"1px solid #fde68a"}}[t]},children:a})}function tR(e){let{label:t,bookmark:a,onCancel:l,onConfirm:n}=e;return(0,s.jsx)("div",{style:{position:"absolute",inset:0,background:"rgba(0,0,0,0.2)",display:"flex",alignItems:"center",justifyContent:"center",zIndex:30,borderRadius:12},children:(0,s.jsxs)("div",{style:{background:tv.bg,border:"1px solid ".concat(tv.border),borderRadius:12,padding:24,boxShadow:"0 8px 40px rgba(0,0,0,0.12)",width:340},children:[(0,s.jsx)("div",{style:{width:38,height:38,borderRadius:10,display:"flex",alignItems:"center",justifyContent:"center",background:"#fef2f2",border:"1px solid #fecaca",marginBottom:14},children:(0,s.jsxs)("svg",{width:"18",height:"18",viewBox:"0 0 18 18",fill:"none",stroke:"#dc2626",strokeWidth:"1.5",children:[(0,s.jsx)("path",{d:"M9 2L2 15h14L9 2z"}),(0,s.jsx)("path",{d:"M9 7v4M9 12.5v.5"})]})}),(0,s.jsx)("div",{style:{fontSize:14,fontWeight:600,color:tv.text,marginBottom:5},children:"Restore database?"}),(0,s.jsx)("div",{style:{fontSize:12,color:tv.text2,lineHeight:1.6,marginBottom:10},children:"This will overwrite your remote D1 database in place. All in-flight queries and transactions will be cancelled."}),(0,s.jsxs)("div",{style:{fontFamily:"ui-monospace,SF Mono,monospace",fontSize:12,padding:"8px 10px",background:tv.bg2,borderRadius:6,color:tv.text,marginBottom:10,lineHeight:1.7},children:[t,a&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("br",{}),(0,s.jsxs)("span",{style:{fontSize:10,color:tv.text3},children:["bookmark: ",a]})]})]}),(0,s.jsx)("div",{style:{fontSize:11,color:"#c2410c",background:tv.orangeBg,border:"1px solid ".concat(tv.orangeBorder),borderRadius:6,padding:"8px 10px",marginBottom:16,lineHeight:1.55},children:"⚠ A pre-restore bookmark will be saved automatically so you can undo this restore if needed."}),(0,s.jsxs)("div",{style:{display:"flex",gap:8},children:[(0,s.jsx)("button",{onClick:l,style:{flex:1,padding:8,border:"1px solid ".concat(tv.border),borderRadius:7,background:"transparent",color:tv.text2,fontSize:12,cursor:"pointer",fontFamily:"system-ui,sans-serif"},className:"hover:text-[#1a1714]",children:"Cancel"}),(0,s.jsx)("button",{onClick:n,style:{flex:1,padding:8,background:"#dc2626",color:"white",border:"none",borderRadius:7,fontSize:12,fontWeight:500,cursor:"pointer",fontFamily:"system-ui,sans-serif"},className:"hover:bg-[#b91c1c]",children:"Yes, restore database"})]})]})})}function tE(e){let{label:t,undoBookmark:a,onDone:l}=e;return(0,s.jsx)("div",{style:{position:"absolute",inset:0,background:"rgba(0,0,0,0.2)",display:"flex",alignItems:"center",justifyContent:"center",zIndex:30,borderRadius:12},children:(0,s.jsxs)("div",{style:{background:tv.bg,border:"1px solid ".concat(tv.border),borderRadius:12,padding:24,boxShadow:"0 8px 40px rgba(0,0,0,0.12)",width:320,textAlign:"center"},children:[(0,s.jsx)("div",{style:{width:38,height:38,borderRadius:"50%",display:"flex",alignItems:"center",justifyContent:"center",background:"#f0fdf4",border:"1px solid #bbf7d0",margin:"0 auto 14px"},children:(0,s.jsx)("svg",{width:"18",height:"18",viewBox:"0 0 20 20",fill:"none",stroke:"#16a34a",strokeWidth:"2",children:(0,s.jsx)("path",{d:"M4 10l4 4 8-8"})})}),(0,s.jsx)("div",{style:{fontSize:14,fontWeight:600,color:tv.text,marginBottom:5},children:"Database restored"}),(0,s.jsxs)("div",{style:{fontSize:12,color:tv.text2,lineHeight:1.6,marginBottom:10},children:["Database has been restored to ",t," successfully."]}),a&&(0,s.jsxs)("div",{style:{fontSize:11,color:tv.text2,background:tv.bg2,border:"1px solid ".concat(tv.border),borderRadius:6,padding:"7px 10px",marginBottom:14,fontFamily:"ui-monospace,SF Mono,monospace",textAlign:"left"},children:["undo bookmark: ",a,(0,s.jsx)("br",{}),(0,s.jsx)("span",{style:{fontSize:10,color:tv.text3},children:"Use this to revert if something looks wrong"})]}),(0,s.jsx)("button",{onClick:l,style:{width:"100%",padding:9,background:tv.text,color:tv.bg,border:"none",borderRadius:7,fontSize:12,fontWeight:500,cursor:"pointer",fontFamily:"system-ui,sans-serif"},children:"Done"})]})})}function tT(){let{currentDatabase:e}=(0,_.t)(),t=(null==e?void 0:e.database_name)||"database",a=new Date,[n,r]=(0,l.useState)(a.getUTCFullYear()),[o,i]=(0,l.useState)(a.getUTCMonth()),[c,d]=(0,l.useState)(a.getUTCDate()),[u,m]=(0,l.useState)("".concat(ty(a.getUTCHours()),":").concat(ty(a.getUTCMinutes()),":00")),[x,h]=(0,l.useState)(null),[f,p]=(0,l.useState)(null),[g,b]=(0,l.useState)(!0),[j,v]=(0,l.useState)(null),[N,w]=(0,l.useState)(null),y=new Date(a.getTime()-2592e6),C=N||y,[S,k]=(0,l.useState)(null),[R,E]=(0,l.useState)(!1),[T,A]=(0,l.useState)(null),[O,I]=(0,l.useState)(!1),[L,U]=(0,l.useState)(!1),[M,D]=(0,l.useState)(null),[F,z]=(0,l.useState)(!1),[P,B]=(0,l.useState)(null),W="".concat(tN[o].slice(0,3)," ").concat(ty(c),", ").concat(n," \xb7 ").concat(u||"00:00:00"," UTC"),Y="".concat(tN[o].slice(0,3)," ").concat(ty(c),", ").concat(n," at ").concat(u||"00:00:00"," UTC"),q=(0,l.useCallback)(()=>{i(e=>0===e?(r(e=>e-1),11):e-1),d(1),k(null)},[]),H=(0,l.useCallback)(()=>{let e=11===o?0:o+1,t=11===o?n+1:n;t>a.getUTCFullYear()||t===a.getUTCFullYear()&&e>a.getUTCMonth()||(i(e),r(t),d(1),k(null))},[o,n,a]),K=(0,l.useCallback)(e=>{d(e),h(null),k(null)},[]),V=(0,l.useCallback)((e,t,a,s,l)=>{r(t),i(a),d(s),m(l),h(e),k(null)},[]),J=(0,l.useCallback)(()=>{let e=new Date;r(e.getUTCFullYear()),i(e.getUTCMonth()),d(e.getUTCDate()),m("".concat(ty(e.getUTCHours()),":").concat(ty(e.getUTCMinutes()),":00")),h(null),k(null)},[]),Q=(0,l.useCallback)(async()=>{E(!0),A(null),k(null);try{let t=(null==e?void 0:e.database_name)?"?db=".concat(encodeURIComponent(e.database_name)):"",a=await fetch("/api/time-travel/preview".concat(t)),s=await a.json();if(!a.ok||!s.success)throw Error(s.error||"Failed to refresh remote state");p(s.currentBookmark),k({bookmark:s.currentBookmark})}catch(e){A(e.message)}finally{E(!1)}},[e]),X=(0,l.useCallback)(async()=>{I(!1),U(!0),D(null);try{var t;let a=new Date("".concat(n,"-").concat(ty(o+1),"-").concat(ty(c),"T").concat(u,"Z")).toISOString(),s=await fetch("/api/time-travel/restore",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({db:null==e?void 0:e.database_name,bookmark:null!=(t=null==S?void 0:S.bookmark)?t:null,timestamp:a})}),l=await s.json();if(!s.ok||!l.success)throw Error(l.error||"Restore failed");B(l.undoBookmark),z(!0)}catch(e){D(e.message)}finally{U(!1)}},[n,o,c,u,e,S]);(0,l.useEffect)(()=>{let t=!1;return k(null),A(null),async function(){b(!0),v(null);try{let a=(null==e?void 0:e.database_name)?"?db=".concat(encodeURIComponent(e.database_name)):"",s=await fetch("/api/time-travel/preview".concat(a)),l=await s.json();if(t)return;l.success?p(l.currentBookmark):v(l.error)}catch(e){t||v(e.message)}finally{t||b(!1)}}(),()=>{t=!0}},[null==e?void 0:e.database_name]);let G=new Date(a-18e5),Z=new Date(a);Z.setHours(12,0,0,0);let $=new Date(a-864e5);$.setHours(18,0,0,0);let ee=new Date(a-1728e5);ee.setHours(9,0,0,0);let et=new Date(a-6048e5);et.setHours(0,0,0,0);let ea=[{id:"q30",label:"30 minutes ago",time:"".concat(ty(G.getUTCHours()),":").concat(ty(G.getUTCMinutes())),date:G,y:G.getUTCFullYear(),m:G.getUTCMonth(),d:G.getUTCDate(),t:"".concat(ty(G.getUTCHours()),":").concat(ty(G.getUTCMinutes()),":00"),icon:"clock"},{id:"qtoday",label:"Earlier today",time:"12:00",date:Z,y:Z.getUTCFullYear(),m:Z.getUTCMonth(),d:Z.getUTCDate(),t:"12:00:00",icon:"clock"},{id:"qyest",label:"Yesterday evening",time:"".concat(tN[$.getUTCMonth()].slice(0,3)," ").concat($.getUTCDate()),date:$,y:$.getUTCFullYear(),m:$.getUTCMonth(),d:$.getUTCDate(),t:"18:00:00",icon:"clock"},{id:"qmig",label:"Before last migration",time:"".concat(tN[ee.getUTCMonth()].slice(0,3)," ").concat(ee.getUTCDate()),date:ee,y:ee.getUTCFullYear(),m:ee.getUTCMonth(),d:ee.getUTCDate(),t:"09:00:00",icon:"shield"},{id:"qweek",label:"1 week ago",time:"".concat(tN[et.getUTCMonth()].slice(0,3)," ").concat(et.getUTCDate()),date:et,y:et.getUTCFullYear(),m:et.getUTCMonth(),d:et.getUTCDate(),t:"00:00:00",icon:"clock"}];return(0,s.jsxs)("div",{style:{display:"flex",flexDirection:"column",height:"100%",position:"relative",overflow:"hidden",background:tv.bg},children:[(0,s.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:12,padding:"12px 18px",borderBottom:"1px solid ".concat(tv.border),flexShrink:0},children:[(0,s.jsxs)("div",{children:[(0,s.jsx)("div",{style:{fontSize:13,fontWeight:600,color:tv.text},children:"Time travel"}),(0,s.jsxs)("div",{style:{fontSize:11,color:tv.text3,marginTop:1},children:[t," \xb7 remote \xb7"," ",g?"loading available window…":j?"could not load window info":f?"latest snapshot: ".concat(f.slice(0,18),"…"):"restore to any point in the available window"]})]}),(0,s.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:6,padding:"5px 11px",background:tv.orangeBg,border:"1px solid ".concat(tv.orangeBorder),borderRadius:20,fontSize:11,color:tv.orange,marginLeft:"auto",flexShrink:0},children:[(0,s.jsxs)("svg",{width:"12",height:"12",viewBox:"0 0 13 13",fill:"none",stroke:"currentColor",strokeWidth:"1.3",children:[(0,s.jsx)("path",{d:"M6.5 1L1 11h11z"}),(0,s.jsx)("path",{d:"M6.5 5v3M6.5 9.5v.5"})]}),"Restore overwrites remote DB in place"]})]}),(0,s.jsxs)("div",{style:{flex:1,display:"flex",overflow:"hidden"},children:[(0,s.jsxs)("div",{style:{width:290,minWidth:290,borderRight:"1px solid ".concat(tv.border),display:"flex",flexDirection:"column",overflow:"hidden"},children:[(0,s.jsx)(tC,{year:n,month:o,selDay:c,today:a,earliest:C,onSelect:K,onPrev:q,onNext:H}),(0,s.jsxs)("div",{style:{padding:"12px 16px",borderBottom:"1px solid ".concat(tv.border),flexShrink:0},children:[(0,s.jsx)("div",{style:{fontSize:11,fontWeight:500,color:tv.text3,marginBottom:7},children:"Time (UTC)"}),(0,s.jsxs)("div",{style:{display:"flex",gap:7,alignItems:"center"},children:[(0,s.jsx)("input",{value:u,onChange:e=>{m(e.target.value),k(null)},style:{flex:1,padding:"7px 10px",border:"1px solid ".concat(tv.border),borderRadius:6,fontFamily:"ui-monospace,SF Mono,monospace",fontSize:13,color:tv.text,background:tv.bg,outline:"none"},onFocus:e=>e.target.style.borderColor=tv.border2,onBlur:e=>e.target.style.borderColor=tv.border,placeholder:"HH:MM:SS"}),(0,s.jsx)("button",{onClick:J,style:{padding:"7px 11px",border:"1px solid ".concat(tv.border),borderRadius:6,fontSize:11,color:tv.text2,background:tv.bg2,cursor:"pointer",fontFamily:"system-ui,sans-serif",whiteSpace:"nowrap"},className:"hover:text-[#1a1714]",children:"Now"})]})]}),(0,s.jsxs)("div",{style:{padding:"10px 16px",flex:1,overflowY:"auto"},children:[(0,s.jsx)("div",{style:{fontSize:11,fontWeight:500,color:tv.text3,marginBottom:9},children:"Quick points"}),ea.map(e=>{let t=x===e.id,a=e.date<new Date(C.getFullYear(),C.getMonth(),C.getDate());return(0,s.jsxs)("button",{onClick:a?void 0:()=>V(e.id,e.y,e.m,e.d,e.t),disabled:a,title:a?"Outside available Time Travel window":void 0,style:{display:"flex",alignItems:"center",gap:8,width:"100%",padding:"7px 10px",border:"1px solid ".concat(t?tv.text:tv.border),borderRadius:7,background:t?tv.bg2:tv.bg,cursor:a?"not-allowed":"pointer",fontFamily:"system-ui,sans-serif",fontSize:12,color:a?tv.border2:t?tv.text:tv.text2,marginBottom:5,textAlign:"left",transition:"border-color 0.1s",opacity:a?.5:1},className:a?"":"hover:border-[#d4d0c8] hover:text-[#1a1714]",children:["clock"===e.icon?(0,s.jsxs)("svg",{width:"13",height:"13",viewBox:"0 0 13 13",fill:"none",stroke:"currentColor",strokeWidth:"1.2",style:{opacity:t?.8:.4,flexShrink:0},children:[(0,s.jsx)("circle",{cx:"6.5",cy:"6.5",r:"5"}),(0,s.jsx)("path",{d:"M6.5 3.5v3l2 1.5"})]}):(0,s.jsx)("svg",{width:"13",height:"13",viewBox:"0 0 13 13",fill:"none",stroke:"currentColor",strokeWidth:"1.2",style:{opacity:t?.8:.4,flexShrink:0},children:(0,s.jsx)("path",{d:"M6.5 1L1 4v3c0 3 2.4 5.5 5.5 6.5 3.1-1 5.5-3.5 5.5-6.5V4z"})}),e.label,(0,s.jsx)("span",{style:{marginLeft:"auto",fontFamily:"ui-monospace,SF Mono,monospace",fontSize:10,color:tv.text3},children:e.time})]},e.id)})]})]}),(0,s.jsxs)("div",{style:{flex:1,display:"flex",flexDirection:"column",overflow:"hidden"},children:[(0,s.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:10,padding:"11px 16px",borderBottom:"1px solid ".concat(tv.border),flexShrink:0},children:[(0,s.jsxs)("span",{style:{fontSize:12,fontWeight:500,color:tv.text,flex:1},children:["Snapshot — ",Y]}),(null==S?void 0:S.bookmark)&&(0,s.jsx)("span",{style:{fontFamily:"ui-monospace,SF Mono,monospace",fontSize:10,color:tv.text3,padding:"2px 7px",background:tv.bg2,border:"1px solid ".concat(tv.border),borderRadius:3},children:S.bookmark.length>22?S.bookmark.slice(0,20)+"…":S.bookmark})]}),(0,s.jsxs)("div",{style:{flex:1,overflowY:"auto",padding:"14px 16px"},children:[T&&(0,s.jsx)("div",{style:{fontSize:12,color:"#dc2626",background:"#fef2f2",border:"1px solid #fecaca",borderRadius:7,padding:"10px 12px",marginBottom:12},children:T}),M&&(0,s.jsxs)("div",{style:{fontSize:12,color:"#dc2626",background:"#fef2f2",border:"1px solid #fecaca",borderRadius:7,padding:"10px 12px",marginBottom:12},children:["Restore failed: ",M]}),R&&(0,s.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:8,fontSize:12,color:tv.text3,padding:"20px 0"},children:[(0,s.jsxs)("svg",{width:"14",height:"14",viewBox:"0 0 14 14",fill:"none",stroke:"currentColor",strokeWidth:"1.5",style:{animation:"spin 1s linear infinite"},children:[(0,s.jsx)("circle",{cx:"7",cy:"7",r:"5",strokeOpacity:"0.3"}),(0,s.jsx)("path",{d:"M7 2a5 5 0 0 1 5 5"})]}),"Loading snapshot info…",(0,s.jsx)("style",{children:"@keyframes spin { to { transform: rotate(360deg); } }"})]}),!R&&!S&&!T&&(0,s.jsxs)("div",{style:{padding:"20px 0",color:tv.text3,fontSize:12},children:[f&&(0,s.jsxs)("div",{style:{background:tv.bg2,border:"1px solid ".concat(tv.border),borderRadius:8,padding:"12px 14px",marginBottom:14},children:[(0,s.jsx)("div",{style:{fontSize:11,fontWeight:500,color:tv.text3,marginBottom:6},children:"Current snapshot (latest)"}),(0,s.jsx)("div",{style:{fontFamily:"ui-monospace,SF Mono,monospace",fontSize:11,color:tv.text,wordBreak:"break-all"},children:f})]}),(0,s.jsxs)("div",{style:{textAlign:"center",paddingTop:f?4:24},children:[(0,s.jsxs)("svg",{width:"32",height:"32",viewBox:"0 0 13 13",fill:"none",stroke:"currentColor",strokeWidth:"1",style:{margin:"0 auto 10px",display:"block",opacity:.3},children:[(0,s.jsx)("circle",{cx:"6.5",cy:"6.5",r:"5"}),(0,s.jsx)("path",{d:"M6.5 3.5v3l2 1.5"})]}),"Select a date & time, then click ",(0,s.jsx)("strong",{children:"Refresh preview"})," to load snapshot info."]})]}),!R&&S&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)("div",{style:{marginBottom:18},children:[(0,s.jsxs)("div",{style:{fontSize:11,fontWeight:600,color:tv.text,marginBottom:7,display:"flex",alignItems:"center",gap:6},children:["Remote state",(0,s.jsx)(tk,{type:"add",children:"refreshed"})]}),(0,s.jsx)(tS,{kind:"c",text:"Restore target: ".concat(Y)}),S.bookmark?(0,s.jsx)(tS,{kind:"a",text:"current bookmark: ".concat(S.bookmark)}):(0,s.jsx)(tS,{kind:"c",text:"current bookmark: (not returned by wrangler)"}),(0,s.jsx)(tS,{kind:"c",text:"Restore will roll back all changes made after the selected time"})]}),(0,s.jsx)("div",{style:{fontSize:11,color:tv.text3,background:tv.bg2,border:"1px solid ".concat(tv.border),borderRadius:7,padding:"10px 12px",lineHeight:1.6},children:"Cloudflare Time Travel does not provide a row-level diff before restore. After confirming, the database will be rolled back atomically to the selected point in time. An undo bookmark will be shown after restore."})]})]}),(0,s.jsxs)("div",{style:{padding:"12px 16px",borderTop:"1px solid ".concat(tv.border),display:"flex",alignItems:"center",gap:10,background:tv.bg,flexShrink:0},children:[(0,s.jsxs)("div",{style:{flex:1},children:[(0,s.jsxs)("div",{style:{fontSize:12,fontWeight:500,color:tv.text},children:["Restore to ",W]}),(null==S?void 0:S.bookmark)&&(0,s.jsxs)("div",{style:{fontSize:11,color:tv.text3,fontFamily:"ui-monospace,SF Mono,monospace",marginTop:1},children:["bookmark: ",S.bookmark]})]}),(0,s.jsxs)("button",{onClick:Q,disabled:R,style:{display:"flex",alignItems:"center",gap:5,padding:"8px 14px",borderRadius:7,border:"1px solid ".concat(tv.border),background:tv.bg,color:tv.text,fontSize:12,fontWeight:500,cursor:R?"default":"pointer",fontFamily:"system-ui,sans-serif",whiteSpace:"nowrap",opacity:R?.6:1},className:"hover:bg-[#f9f8f6]",children:[(0,s.jsxs)("svg",{width:"12",height:"12",viewBox:"0 0 12 12",fill:"none",stroke:"currentColor",strokeWidth:"1.3",children:[(0,s.jsx)("path",{d:"M1 6A5 5 0 0 1 10 3M11 6A5 5 0 0 1 2 9"}),(0,s.jsx)("path",{d:"M9 1.5v2h2M1 8.5v2h2"})]}),"Refresh preview"]}),(0,s.jsxs)("button",{onClick:()=>I(!0),disabled:L,style:{display:"flex",alignItems:"center",gap:5,padding:"8px 14px",borderRadius:7,border:"none",background:L?"#f87171":"#dc2626",color:"white",fontSize:12,fontWeight:500,cursor:L?"default":"pointer",fontFamily:"system-ui,sans-serif",whiteSpace:"nowrap"},className:"hover:bg-[#b91c1c]",children:[(0,s.jsxs)("svg",{width:"12",height:"12",viewBox:"0 0 12 12",fill:"none",stroke:"currentColor",strokeWidth:"1.3",children:[(0,s.jsx)("path",{d:"M1 6A5 5 0 0 1 10 3M11 6A5 5 0 0 1 2 9"}),(0,s.jsx)("path",{d:"M9 1.5v2h2M1 8.5v2h2"})]}),L?"Restoring…":"Restore database"]})]})]})]}),O&&(0,s.jsx)(tR,{label:W,bookmark:null==S?void 0:S.bookmark,onCancel:()=>I(!1),onConfirm:X}),F&&(0,s.jsx)(tE,{label:W,undoBookmark:P,onDone:()=>{z(!1),k(null)}})]})}function tA(e){let{open:t,onClose:a,onSubmit:n}=e,[r,o]=(0,l.useState)(""),[i,c]=(0,l.useState)(""),[d,u]=(0,l.useState)(""),[m,h]=(0,l.useState)(!1),f=async e=>{e.preventDefault(),u(""),h(!0);try{await n({accountId:r,apiToken:i}),localStorage.setItem("cf_credentials",JSON.stringify({accountId:r,apiToken:i})),a()}catch(e){u(e.message)}finally{h(!1)}};return(0,s.jsx)(e0,{open:t,onOpenChange:a,children:(0,s.jsxs)(e5,{className:"sm:max-w-md",children:[(0,s.jsxs)(e4,{children:[(0,s.jsx)(e6,{children:"Connect to Cloudflare"}),(0,s.jsx)(e8,{children:"Enter your Cloudflare credentials to push your database to production."})]}),(0,s.jsxs)("form",{onSubmit:f,className:"space-y-4 mt-4",children:[d&&(0,s.jsx)(N,{variant:"destructive",children:(0,s.jsx)(w,{children:d})}),(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsx)("label",{className:"text-sm font-medium",children:"Account ID"}),(0,s.jsx)(b,{placeholder:"Your Cloudflare Account ID",value:r,onChange:e=>o(e.target.value),required:!0})]}),(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsx)("label",{className:"text-sm font-medium",children:"API Token"}),(0,s.jsx)(b,{type:"password",placeholder:"Your D1 Database API Token",value:i,onChange:e=>c(e.target.value),required:!0})]}),(0,s.jsxs)("div",{className:"flex justify-end gap-3",children:[(0,s.jsx)(L,{type:"button",variant:"outline",onClick:a,children:"Cancel"}),(0,s.jsxs)(L,{type:"submit",disabled:m,children:[m&&(0,s.jsx)(x.A,{className:"mr-2 h-4 w-4 animate-spin"}),"Connect"]})]})]})]})})}function t_(){let{refreshTables:e}=(0,_.t)(),[t,a]=(0,l.useState)(!1),[n,r]=(0,l.useState)(null),[o,c]=(0,l.useState)(null),{isConnecting:d,isPushing:u,showPreview:m,connectToCloudflare:h,pushToRemote:f,pullToRemotePreview:p,validateStoredCredentials:g,PreviewUI:b}=V(),j=async()=>{c(null),r("push");try{if(!await g())return void a(!0);await f(),c({type:"success",message:"Push to remote initiated. Check the preview for details."})}catch(e){c({type:"error",message:e.message})}},v=async()=>{c(null),r("pull");try{if(!await g())return void a(!0);await p()}catch(e){c({type:"error",message:e.message})}},y=d||u;return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)("div",{className:"h-full flex flex-col",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2 p-4 border-b",children:[(0,s.jsx)(eA.A,{className:"h-4 w-4"}),(0,s.jsx)("h2",{className:"text-sm font-semibold",children:"Remote Sync"})]}),(0,s.jsx)("div",{className:"flex-1 overflow-auto p-6",children:(0,s.jsxs)("div",{className:"max-w-lg flex flex-col gap-6",children:[(0,s.jsx)("p",{className:"text-sm text-muted-foreground",children:"Sync your local D1 database with Cloudflare. Pushing uploads your local data to production; pulling downloads the remote state to your local environment."}),o&&(0,s.jsxs)(N,{variant:"error"===o.type?"destructive":"default",children:["error"===o.type?(0,s.jsx)(i.A,{className:"h-4 w-4"}):(0,s.jsx)(te.A,{className:"h-4 w-4"}),(0,s.jsx)(w,{children:o.message})]}),(0,s.jsxs)("div",{className:"grid grid-cols-2 gap-4",children:[(0,s.jsxs)("div",{className:"border rounded-md p-4 bg-white flex flex-col gap-3",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)(ew.A,{className:"h-4 w-4 text-neutral-600"}),(0,s.jsx)("span",{className:"text-sm font-semibold",children:"Push to remote"})]}),(0,s.jsx)("p",{className:"text-xs text-muted-foreground",children:"Upload local database state to your Cloudflare D1 database."}),(0,s.jsx)(L,{size:"sm",onClick:j,disabled:y,className:"w-full",children:u&&"push"===n?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin mr-2"})," Pushing…"]}):(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(ew.A,{className:"h-4 w-4 mr-2"})," Push"]})})]}),(0,s.jsxs)("div",{className:"border rounded-md p-4 bg-white flex flex-col gap-3",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)(eU.A,{className:"h-4 w-4 text-neutral-600"}),(0,s.jsx)("span",{className:"text-sm font-semibold",children:"Pull from remote"})]}),(0,s.jsx)("p",{className:"text-xs text-muted-foreground",children:"Download Cloudflare D1 data into your local environment."}),(0,s.jsx)(L,{size:"sm",variant:"outline",onClick:v,disabled:y,className:"w-full",children:d&&"pull"===n?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(x.A,{className:"h-4 w-4 animate-spin mr-2"})," Pulling…"]}):(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(eU.A,{className:"h-4 w-4 mr-2"})," Pull"]})})]})]}),(0,s.jsx)("p",{className:"text-xs text-muted-foreground",children:"Cloudflare API credentials are required. You'll be prompted to enter them if not already saved."})]})})]}),(0,s.jsx)(tA,{open:t,onClose:()=>a(!1),onSubmit:h}),b]})}let tO={loading:"text-blue-500 border-blue-500/40 bg-blue-500/5",success:"text-green-600 border-green-500/40 bg-green-500/5",error:"text-red-500 border-red-500/40 bg-red-500/5",info:"text-muted-foreground border-border bg-transparent"},tI={loading:null,success:"✓",error:"✗",info:"─"};function tL(){var e,t,a,n,r;let{lastStatus:o}=(0,_.t)(),i=function(e){let[t,a]=(0,l.useState)("");return(0,l.useEffect)(()=>{if(!e)return void a("");let t=()=>{let t=Math.floor((Date.now()-e.getTime())/1e3);t<5?a("just now"):t<60?a("".concat(t,"s ago")):t<3600?a("".concat(Math.floor(t/60),"m ago")):a(e.toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"}))};t();let s=setInterval(t,5e3);return()=>clearInterval(s)},[e]),t}(null!=(e=null==o?void 0:o.ts)?e:null),c=null!=(t=null==o?void 0:o.type)?t:"info",d=null!=(a=null==o?void 0:o.message)?a:"Ready",u=null!=(n=null==o?void 0:o.detail)?n:null,m=null!=(r=tO[c])?r:tO.info,h=tI[c];return(0,s.jsxs)("div",{className:"flex items-center justify-between h-8 px-4 border-t font-mono text-xs select-none shrink-0 ".concat(m),role:"status","aria-live":"polite",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2 min-w-0",children:[(0,s.jsx)("span",{className:"shrink-0 w-4 flex items-center justify-center",children:"loading"===c?(0,s.jsx)(x.A,{className:"h-3 w-3 animate-spin"}):(0,s.jsx)("span",{children:h})}),(0,s.jsx)("span",{className:"truncate",children:d}),u&&(0,s.jsxs)("span",{className:"text-muted-foreground truncate hidden sm:inline",children:["\xb7 ",u]})]}),i&&(0,s.jsx)("span",{className:"text-muted-foreground shrink-0 pl-4",children:i})]})}function tU(){let[e,t]=(0,l.useState)("sql-editor"),[a,n]=(0,l.useState)(null),[r,o]=(0,l.useState)("");return(0,s.jsxs)("div",{className:"flex h-screen bg-background",children:[(0,s.jsx)(Q,{className:"w-64 border-r flex-shrink-0",currentView:e,onNavigate:t,onTableSelect:e=>{n(e),t("table-view")}}),(0,s.jsxs)("div",{className:"flex-1 flex flex-col overflow-hidden",children:[(0,s.jsxs)("main",{className:"flex-1 overflow-hidden",children:["sql-editor"===e&&(0,s.jsx)(ec,{initialQuery:r}),"schema"===e&&(0,s.jsx)(tj,{}),"migrations"===e&&(0,s.jsx)(tg,{}),"time-travel"===e&&(0,s.jsx)(tT,{}),"remote-sync"===e&&(0,s.jsx)(t_,{}),"table-view"===e&&a&&(0,s.jsx)(tf,{tableName:a,onOpenInSQL:e=>{o(e),t("sql-editor")}})]}),(0,s.jsx)(tL,{})]})]})}},4576:(e,t,a)=>{Promise.resolve().then(a.bind(a,930))}},e=>{e.O(0,[421,69,823,441,255,358],()=>e(e.s=4576)),_N_E=e.O()}]);