1"use strict";(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[3212],{23594:(e,a,t)=>{t.d(a,{O:()=>i,w:()=>l});var s=t(53455),r=t(59950),n=t(49327);function l(e,a=!0){let{data:t,isLoading:i,error:d,...o}=(0,s.useQuery)({queryKey:r.l.multitenant.config(e),queryFn:async()=>(0,n.dD)(e),enabled:!!e&&a,retry:2,refetchOnReconnect:!0});return{data:t,isLoading:i,error:d,...o}}function i({workspaceId:e,tenantTable:a,tenantColumn:t,enabled:l=!0}){return(0,s.useQuery)({queryKey:r.l.multitenant.analyze(e,a||"",t||void 0),queryFn:async()=>{if(!a)throw Error("Tenant table is required");let s=await (0,n.h4)({workspace_id:e,tenant_table:a,tenant_column:t||void 0}),r={...s.data,tenant_id_column:s.data.tenant_id_column||s.data.tenant_column||""};return{...s,data:r}},enabled:!!e&&!!a&&l,retry:!1})}},33212:(e,a,t)=>{t.d(a,{m:()=>e_});var s=t(95155),r=t(12115),n=t(35626),l=t(35299),i=t(3998),d=t(6132),o=t(89559),c=t(85710),m=t(80003),h=t(14639),x=t(8803),u=t(52987),p=t(37354),f=t(22539),g=t(49799),b=t(70877),j=t(41669),y=t(24033),N=t(12108),w=t(70532),v=t(57828),k=t(33828);let _=(0,r.memo)(({data:e,selected:a})=>{let{table:t,isPrimary:n,onSelectAsTenant:l,hiddenHandles:d,hasSampleData:o,isSampleDataVisible:m,onToggleSampleData:h}=e,x=t.columns.filter(e=>e.primaryKey),p=t.foreignKeyRelationships,[b,_]=(0,r.useState)(!1),C=t.columns.filter(e=>!e.primaryKey),S=b?C:C.slice(0,3),$=C.length-S.length,A=e=>!d?.includes(e);return(0,s.jsxs)("div",{className:` 2 min-w-[460px] max-w-[520px] rounded-lg border-2 bg-card shadow-lg transition-all 3 ${a?"border-[#07825e] shadow-2xl scale-105":"border-border shadow-md"} 4 `,children:[(0,s.jsxs)("div",{className:"bg-gradient-to-r from-emerald-50 to-teal-50 dark:from-emerald-950/40 dark:to-teal-950/40 border-b-2 border-border p-5",children:[(0,s.jsxs)("div",{className:"flex items-center gap-3 mb-2",children:[(0,s.jsx)("div",{className:"p-2.5 rounded bg-[#07825e] dark:bg-[#09a378]",children:(0,s.jsx)(j.A,{className:"w-5 h-5 text-white"})}),(0,s.jsx)("h3",{className:"font-bold text-lg text-foreground truncate flex-1",children:t.name}),n&&(0,s.jsx)("span",{className:"text-sm bg-[#07825e] text-white px-2.5 py-1 rounded-full font-medium",children:"Primary"})]}),(0,s.jsxs)("div",{className:"flex items-center gap-2.5 text-sm text-muted-foreground",children:[(0,s.jsx)(c.A,{className:"w-4 h-4"}),(0,s.jsxs)("span",{children:[t.columns.length," columns"]}),p.length>0&&(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("span",{children:"â¢"}),(0,s.jsx)(k.pXu,{className:"w-4 h-4"}),(0,s.jsxs)("span",{children:[p.length," ",1===p.length?"Foreign Key":"Foreign Keys"]})]})]})]}),(0,s.jsx)("div",{className:"relative",children:(0,s.jsxs)("div",{className:`p-4 space-y-1.5 bg-card ${b?"overflow-visible":"max-h-[360px] overflow-y-auto"}`,children:[x.length>0&&(0,s.jsx)("div",{className:"mb-2",children:x.map(e=>(0,s.jsxs)("div",{className:"flex items-center justify-between p-2.5 rounded bg-green-50 dark:bg-green-950/20 border border-green-200 dark:border-green-800",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2.5 min-w-0 flex-1",children:[(0,s.jsx)(k.pXu,{className:"w-4 h-4 text-green-accent dark:text-green-accent shrink-0"}),(0,s.jsx)("span",{className:"font-mono text-sm font-bold text-green-accent dark:text-green-accent truncate",children:e.name})]}),(0,s.jsx)("span",{className:"text-sm text-muted-foreground font-mono ml-2 shrink-0",children:e.type})]},e.name))}),S.map(e=>{let a=p.some(a=>a.constrained_columns.includes(e.name));return(0,s.jsxs)("div",{className:`flex items-center justify-between p-2 rounded text-sm ${a?"bg-orange-50 dark:bg-orange-950/20 border border-orange-200 dark:border-orange-800":"bg-muted/50"}`,children:[(0,s.jsxs)("div",{className:"flex items-center gap-2.5 min-w-0 flex-1",children:[a&&(0,s.jsx)(u.A,{className:"w-4 h-4 text-orange-600 dark:text-orange-400 shrink-0"}),(0,s.jsx)("span",{className:"font-mono truncate",children:e.name})]}),(0,s.jsx)("span",{className:"text-muted-foreground font-mono ml-2 shrink-0 text-xs",children:e.type.length>15?e.type.substring(0,12)+"...":e.type})]},e.name)}),$>0&&!b&&(0,s.jsxs)("button",{onClick:e=>{e.stopPropagation(),_(!0)},className:"w-full text-sm text-muted-foreground hover:text-foreground text-center pt-2 pb-1 font-medium transition-colors flex items-center justify-center gap-1.5 hover:bg-muted/50 rounded",children:[(0,s.jsx)(y.A,{className:"w-4 h-4"}),"+",$," more columns"]}),b&&C.length>3&&(0,s.jsxs)("button",{onClick:e=>{e.stopPropagation(),_(!1)},className:"w-full text-sm text-muted-foreground hover:text-foreground text-center pt-2 pb-1 font-medium transition-colors flex items-center justify-center gap-1.5 hover:bg-muted/50 rounded mt-1",children:[(0,s.jsx)(N.A,{className:"w-4 h-4"}),"Show less"]}),o&&h&&(0,s.jsx)("button",{onClick:e=>{e.stopPropagation(),h()},className:"w-full text-sm text-muted-foreground hover:text-foreground text-center pt-2 pb-1 font-medium transition-colors flex items-center justify-center gap-1.5 hover:bg-muted/50 rounded",children:m?(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(w.A,{className:"w-4 h-4"}),"Hide Sample Data"]}):(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(v.A,{className:"w-4 h-4"}),"Show Sample Data"]})})]})}),l&&(0,s.jsx)("div",{className:"border-t border-border p-3 bg-muted/30",children:(0,s.jsx)(i.$,{size:"sm",variant:"outline",className:"w-full text-sm h-8",onClick:()=>l(t),children:"Use as Tenant Table"})}),A(g.yX.Left)&&(0,s.jsx)(f.h7,{type:"target",position:g.yX.Left,className:"!w-4 !h-4 !bg-[#07825e] !border-2 !border-white dark:!border-gray-800"}),A(g.yX.Right)&&(0,s.jsx)(f.h7,{type:"source",position:g.yX.Right,className:"!w-4 !h-4 !bg-[#07825e] !border-2 !border-white dark:!border-gray-800"}),m&&(0,s.jsx)(f.h7,{type:"source",position:g.yX.Right,className:"!w-4 !h-4 !bg-blue-500 !border-2 !border-white dark:!border-gray-800",id:"sample-data"})]})});_.displayName="TableNode";let C=(e,a,t=0)=>{let r=t>0?{marginLeft:`${1.5*t}rem`}:{};if(null==a)return[(0,s.jsxs)("div",{className:"flex items-center justify-between p-2 rounded bg-muted/50 text-sm",style:r,children:[(0,s.jsx)("div",{className:"flex items-center gap-2.5 min-w-0 flex-1",children:(0,s.jsx)("span",{className:"font-mono truncate",children:e})}),(0,s.jsx)("span",{className:"text-muted-foreground font-mono ml-2 shrink-0 text-xs truncate max-w-[200px]",title:"null",children:"null"})]},e)];if(Array.isArray(a)){let n,l=JSON.stringify(a,null,2);
4n=0===a.length?"[]":a.length>0&&"object"!=typeof a[0]?`[${a.slice(0,3).join(", ")}${a.length>3?"...":""}]`:`[${a.length} items]`;let i=[(0,s.jsxs)("div",{className:"flex items-center justify-between p-2 rounded bg-muted/50 text-sm",style:r,children:[(0,s.jsx)("div",{className:"flex items-center gap-2.5 min-w-0 flex-1",children:(0,s.jsx)("span",{className:"font-mono truncate",children:e})}),(0,s.jsx)("span",{className:"text-muted-foreground font-mono ml-2 shrink-0 text-xs truncate max-w-[200px]",title:l,children:n})]},e)];return a.length>0&&"object"==typeof a[0]&&null!==a[0]&&a.forEach((a,s)=>{"object"==typeof a&&null!==a&&Object.keys(a).forEach(r=>{i.push(...C(`${e}[${s}].${r}`,a[r],t+1))})}),i}if("object"==typeof a){let n=[],l=Object.keys(a);return l.length>0?(n.push((0,s.jsxs)("div",{className:"flex items-center justify-between p-2 rounded bg-muted/50 text-sm",style:r,children:[(0,s.jsx)("div",{className:"flex items-center gap-2.5 min-w-0 flex-1",children:(0,s.jsx)("span",{className:"font-mono truncate font-semibold",children:e})}),(0,s.jsx)("span",{className:"text-muted-foreground font-mono ml-2 shrink-0 text-xs",children:`{${l.length} ${1===l.length?"field":"fields"}}`})]},e)),l.forEach(s=>{let r=a[s],l=`${e}.${s}`;n.push(...C(l,r,t+1))})):n.push((0,s.jsxs)("div",{className:"flex items-center justify-between p-2 rounded bg-muted/50 text-sm",style:r,children:[(0,s.jsx)("div",{className:"flex items-center gap-2.5 min-w-0 flex-1",children:(0,s.jsx)("span",{className:"font-mono truncate",children:e})}),(0,s.jsx)("span",{className:"text-muted-foreground font-mono ml-2 shrink-0 text-xs truncate max-w-[200px]",title:"{}"})]},e)),n}let n=String(a),l=n.length>30?n.substring(0,27)+"...":n;return[(0,s.jsxs)("div",{className:"flex items-center justify-between p-2 rounded bg-muted/50 text-sm",style:r,children:[(0,s.jsx)("div",{className:"flex items-center gap-2.5 min-w-0 flex-1",children:(0,s.jsx)("span",{className:"font-mono truncate",children:e})}),(0,s.jsx)("span",{className:"text-muted-foreground font-mono ml-2 shrink-0 text-xs truncate max-w-[200px]",title:n,children:l})]},e)]},S=(0,r.memo)(({data:e,selected:a})=>{let{table:t,sampleRecords:n}=e,[l,i]=(0,r.useState)(!1),d=l?n:n.slice(0,3),o=n.length-d.length;if(!n||0===n.length)return(0,s.jsxs)("div",{className:` 5 min-w-[460px] max-w-[520px] rounded-lg border-2 bg-card shadow-lg transition-all 6 ${a?"border-blue-500 shadow-2xl scale-105":"border-border shadow-md"} 7 `,children:[(0,s.jsx)("div",{className:"bg-gradient-to-r from-blue-50 to-indigo-50 dark:from-blue-950/40 dark:to-indigo-950/40 border-b-2 border-border p-5",children:(0,s.jsxs)("div",{className:"flex items-center gap-3 mb-2",children:[(0,s.jsx)("div",{className:"p-2.5 rounded bg-blue-500 dark:bg-blue-600",children:(0,s.jsx)(c.A,{className:"w-5 h-5 text-white"})}),(0,s.jsxs)("h3",{className:"font-bold text-lg text-foreground truncate flex-1",children:["Sample Data: ",t.name]})]})}),(0,s.jsx)("div",{className:"p-4 text-center text-muted-foreground",children:(0,s.jsx)("p",{className:"text-sm",children:"No sample data available"})}),(0,s.jsx)(f.h7,{type:"target",position:g.yX.Top,className:"!w-4 !h-4 !bg-blue-500 !border-2 !border-white dark:!border-gray-800"})]});let m=n.length>0?Object.keys(n[0]):t.columns.map(e=>e.name);return(0,s.jsxs)("div",{className:` 8 min-w-[460px] max-w-[520px] rounded-lg border-2 bg-card shadow-lg transition-all 9 ${a?"border-blue-500 shadow-2xl scale-105":"border-border shadow-md"} 10 `,children:[(0,s.jsx)("div",{className:"bg-gradient-to-r from-blue-50 to-indigo-50 dark:from-blue-950/40 dark:to-indigo-950/40 border-b-2 border-border p-5",children:(0,s.jsxs)("div",{className:"flex items-center gap-3 mb-2",children:[(0,s.jsx)("div",{className:"p-2.5 rounded bg-blue-500 dark:bg-blue-600",children:(0,s.jsx)(c.A,{className:"w-5 h-5 text-white"})}),(0,s.jsxs)("h3",{className:"font-bold text-lg text-foreground truncate flex-1",children:["Sample Data: ",t.name]})]})}),(0,s.jsx)("div",{className:"relative",children:(0,s.jsxs)("div",{className:"p-4 space-y-2 bg-card",children:[d.map((e,a)=>(0,s.jsx)("div",{className:"space-y-1.5 p-3 rounded bg-muted/30 border border-border/50 hover:bg-muted/50 transition-colors",children:m.map(a=>{let t=e[a];return(0,s.jsx)(r.Fragment,{children:C(a,t)},a)})},a)),o>0&&!l&&(0,s.jsxs)("button",{onClick:e=>{e.stopPropagation(),i(!0)},className:"w-full text-sm text-muted-foreground hover:text-foreground text-center pt-2 pb-1 font-medium transition-colors flex items-center justify-center gap-1.5 hover:bg-muted/50 rounded",children:[(0,s.jsx)(y.A,{className:"w-4 h-4"}),"+",o," more records"]}),l&&n.length>3&&(0,s.jsxs)("button",{onClick:e=>{e.stopPropagation(),i(!1)},className:"w-full text-sm text-muted-foreground hover:text-foreground text-center pt-2 pb-1 font-medium transition-colors flex items-center justify-center gap-1.5 hover:bg-muted/50 rounded mt-1",children:[(0,s.jsx)(N.A,{className:"w-4 h-4"}),"Show less"]})]})}),(0,s.jsx)(f.h7,{type:"target",position:g.yX.Left,className:"!w-4 !h-4 !bg-blue-500 !border-2 !border-white dark:!border-gray-800"})]})});S.displayName="SampleDataNode";var $=t(33464),A=t(1226),T=t(26550),R=t(60919),z=t(23389),L=t(11253),D=t(57497);function E({workspaceId:e,dataSourceId:a,parsedSchema:t,analysis:n,selectTenantTable:l,selectedTable:i,onSelectTable:d,viewType:x,onViewTypeChange:p,onRefreshSchema:f,canToggleSchemaData:g=!1,hideTableList:b=!1}){let[y,N]=(0,r.useState)(""),[w,v]=(0,r.useState)("schema"),[_,C]=(0,r.useState)(!1),[S,E]=(0,r.useState)(new Map),[K,F]=(0,r.useState)(50);(0,r.useEffect)(()=>E(new Map),[a]);let B=S.get(i??"")??1,M=g&&"data"===w&&!!i,{data:V,isLoading:P,isFetching:O,isPlaceholderData:I,error:Z,refetch:W}=(0,L.i0)(e,a,i??"",B,K,M),{data:q}=(0,L.hA)(e),{mutate:H}=(0,L.UU)(),X=q?.data?.meta?.table_column_widths??{},J=e=>{F(e),E(new Map)},U=(0,r.useMemo)(()=>{let e=y.trim().toLowerCase();return e?t.tables.filter(a=>a.name.toLowerCase().includes(e)):t.tables},[t.tables,y]),G=(e,a)=>{E(t=>new Map(t).set(e,a))};return(0,s.jsxs)("div",{className:"h-full flex flex-col",children:[b&&(0,s.jsxs)("div",{className:"flex items-center gap-2 px-4 h-10 border-b flex-shrink-0",children:[n.primaryTables.length>0&&(0,s.jsxs)("span",{className:"text-xs bg-green-100 dark:bg-green-900 text-green-accent dark:text-green-400 px-2 py-1 rounded",children:[n.primaryTables.length," Primary"]}),(0,s.jsxs)("div",{className:"ml-auto flex items-center gap-1",children:[(0,s.jsx)($.$n,{variant:"outline",onClick:f,size:"sm",className:"h-7 px-2 text-xs gap-1",title:"Refresh Schema","aria-label":"Refresh schema",children:(0,s.jsx)(o.A,{className:"w-3 h-3"})}),(0,s.jsx)($.$n,{variant:"list"===x?"default":"outline",onClick:()=>p("list"),size:"sm",className:"h-7 px-2 text-xs gap-1",title:"List View","aria-label":"List view","aria-pressed":"list"===x,children:(0,s.jsx)(m.A,{className:"w-3 h-3"})}),(0,s.jsx)($.$n,{variant:"diagram"===x?"default":"outline",onClick:()=>p("diagram"),size:"sm",className:"h-7 px-2 text-xs gap-1",title:"Diagram View","aria-label":"Diagram view","aria-pressed":"diagram"===x,children:(0,s.jsx)(h.A,{className:"w-3 h-3"})})]})]}),(0,s.jsx)("div",{className:"flex-1 overflow-hidden",children:(0,s.jsxs)("div",{className:"h-full flex",children:[b?null:_?(0,s.jsx)("div",{className:"border-r bg-gray-50 dark:bg-background p-2",children:(0,s.jsx)($.$n,{variant:"ghost",size:"sm",onClick:()=>C(!1),className:"h-7 w-7 p-0",title:"Show tables sidebar","aria-label":"Show tables sidebar",children:(0,s.jsx)(A.A,{className:"w-4 h-4"})})}):(0,s.jsxs)("div",{className:"w-80 border-r bg-gray-50 dark:bg-background overflow-y-auto",children:[(0,s.jsxs)("div",{className:"p-4 border-b bg-white dark:bg-card",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between mb-3",children:[(0,s.jsx)("h4",{className:"font-medium text-gray-900 dark:text-gray-100",children:"Tables"}),(0,s.jsx)("div",{className:"flex items-center gap-1",children:(0,s.jsx)($.$n,{variant:"ghost",size:"sm",onClick:()=>C(!0),className:"h-7 px-2",title:"Hide tables sidebar","aria-label":"Hide tables sidebar",children:(0,s.jsx)(T.A,{className:"w-3 h-3"})})})]}),(0,s.jsx)("div",{className:"mb-3",children:(0,s.jsx)($.pd,{value:y,onChange:e=>N(e.target.value),placeholder:"Search tables...",className:"h-9 text-sm"})}),(0,s.jsxs)("div",{className:"flex flex-wrap justify-between",children:[n.primaryTables.length>0&&(0,s.jsxs)("span",{className:"text-xs bg-green-100 dark:bg-green-900 text-green-accent dark:text-green-400 px-2 py-1 rounded",children:[n.primaryTables.length," Primary"]}),(0,s.jsxs)("div",{className:"flex items-center gap-1",children:[(0,s.jsx)($.$n,{variant:"outline",onClick:f,size:"sm",className:"h-7 px-2 text-xs gap-1",title:"Refresh Schema","aria-label":"Refresh schema",children:(0,s.jsx)(o.A,{className:"w-3 h-3"})}),(0,s.jsx)($.$n,{variant:"list"===x?"default":"outline",onClick:()=>p("list"),size:"sm",className:"h-7 px-2 text-xs gap-1",title:"List View","aria-label":"List view","aria-pressed":"list"===x,children:(0,s.jsx)(m.A,{className:"w-3 h-3"})}),(0,s.jsx)($.$n,{variant:"diagram"===x?"default":"outline",onClick:()=>p("diagram"),size:"sm",className:"h-7 px-2 text-xs gap-1",title:"Diagram View","aria-label":"Diagram view","aria-pressed":"diagram"===x,children:(0,s.jsx)(h.A,{className:"w-3 h-3"})})]})]})]}),(0,s.jsx)("div",{className:"p-4 space-y-2",children:0===U.length?(0,s.jsx)("div",{className:"text-xs text-gray-500 dark:text-gray-400",children:"No tables found"}):U.map(e=>(0,s.jsxs)("div",{className:`flex items-center gap-2 p-2 rounded cursor-pointer transition-colors${i===e.name?"bg-green-100 dark:bg-green-900 text-green-accent dark:text-green-300":"hover:bg-gray-100 dark:hover:bg-muted"}`,onClick:()=>d(i===e.name?"":e.name),children:[(0,s.jsx)(j.A,{className:"size-4 shrink-0"}),(0,s.jsx)("span",{className:"font-mono text-sm overflow-hidden text-ellipsis whitespace-nowrap",children:e.name}),(0,s.jsx)("span",{className:"text-xs text-gray-500 dark:text-gray-400 ml-auto",children:e.columns.length})]},e.name))})]}),(0,s.jsx)("div",{className:(0,D.cn)("flex-1 min-h-0",M?"flex flex-col overflow-hidden":"overflow-y-auto"),children:i?(0,s.jsx)("div",{className:(0,D.cn)("p-3 sm:p-6",M&&"flex flex-1 min-h-0 flex-col"),children:(a=>{let t=V?.rows??[],r=g?w:"schema",i="data"===r,d=O&&V?.table_name!==a.name,m="border border-gray-200 dark:border-border rounded flex-1 min-h-0 flex flex-col";return(0,s.jsxs)("div",{className:(0,D.cn)("border rounded-lg bg-white dark:bg-card shadow-sm",i&&"flex flex-1 min-h-0 flex-col"),children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-2 p-3 sm:p-4 border-b bg-gray-50 dark:bg-muted",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2 sm:gap-3 min-w-0",children:[(0,s.jsx)(j.A,{className:"w-5 h-5 shrink-0 text-green-accent"}),(0,s.jsx)("h5",{className:"font-semibold text-gray-900 dark:text-gray-100 text-base sm:text-lg break-all",children:a.name}),l&&(0,s.jsx)($.$n,{size:"sm",variant:"outline",onClick:()=>l(a),className:"text-xs px-2 py-1 h-auto ml-2",children:"Use as Tenant"})]}),(0,s.jsxs)("div",{className:"flex flex-wrap items-center gap-2",children:[(0,s.jsxs)("span",{className:"text-xs bg-gray-100 dark:bg-muted text-gray-600 dark:text-muted-foreground px-2 py-1 rounded",children:[a.columns.length," columns"]}),a.foreignKeyRelationships.length>
100&&(0,s.jsxs)("span",{className:"text-xs bg-orange-100 dark:bg-orange-900 text-orange-600 dark:text-orange-400 px-2 py-1 rounded flex items-center gap-1",children:[(0,s.jsx)(k.pXu,{className:"w-3 h-3"}),a.foreignKeyRelationships.length," ",1===a.foreignKeyRelationships.length?"Foreign Key":"Foreign Keys"]}),n?.primaryTables.includes(a.name)&&(0,s.jsx)("span",{className:"text-xs bg-green-100 dark:bg-green-900 text-green-accent dark:text-green-400 px-2 py-1 rounded",children:"Primary"})]})]}),(0,s.jsxs)("div",{className:(0,D.cn)("p-3 sm:p-4",i&&"flex flex-1 min-h-0 flex-col"),children:[g&&(0,s.jsxs)("div",{className:"flex shrink-0 items-center gap-1 mb-4 border-b border-gray-200 dark:border-border",children:[(0,s.jsxs)("button",{type:"button",onClick:()=>v("schema"),className:`flex items-center gap-2 px-3 py-2 text-sm font-medium border-b-2 -mb-px transition-colors ${"schema"===w?"border-green-accent text-green-accent":"border-transparent text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-200"}`,"aria-pressed":"schema"===w,children:[(0,s.jsx)(c.A,{className:"w-4 h-4"}),"Schema"]}),(0,s.jsxs)("button",{type:"button",onClick:()=>v("data"),className:`flex items-center gap-2 px-3 py-2 text-sm font-medium border-b-2 -mb-px transition-colors ${"data"===w?"border-green-accent text-green-accent":"border-transparent text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-200"}`,"aria-pressed":"data"===w,children:[(0,s.jsx)(j.A,{className:"w-4 h-4"}),"Data"]})]}),"schema"===r?(0,s.jsx)("div",{className:"space-y-2",children:a.columns.map(e=>(0,s.jsxs)("div",{className:"flex flex-wrap items-center justify-between gap-2 bg-gray-50 dark:bg-muted p-3 rounded border border-gray-200 dark:border-border",children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center gap-2 sm:gap-3 min-w-0",children:[(0,s.jsx)("code",{className:"font-mono text-sm bg-white dark:bg-card px-2 py-1 rounded border border-gray-200 dark:border-border",children:e.name}),(0,s.jsx)("span",{className:"text-sm text-gray-600 dark:text-gray-400 font-mono",children:e.type}),e.primaryKey&&(0,s.jsxs)("span",{className:"flex items-center gap-1 text-xs bg-green-100 dark:bg-green-900 text-green-600 dark:text-green-400 px-2 py-1 rounded",children:[(0,s.jsx)(k.pXu,{className:"w-3 h-3"}),"Primary Key"]})]}),(0,s.jsx)("div",{className:"text-xs text-gray-500 dark:text-gray-400",children:e.nullable?"Nullable":"Not Null"})]},e.name))}):P||d?(0,s.jsx)("div",{className:m,children:(0,s.jsx)(z.O,{columnCount:a.columns.length,serverPagination:!0,fillHeight:!0})}):Z?(0,s.jsx)("div",{className:"flex flex-1 items-center justify-center py-12",children:(0,s.jsxs)("div",{className:"text-center",children:[(0,s.jsx)("p",{className:"text-sm text-gray-500 dark:text-gray-400 mb-3",children:(e=>{if((0,R.F0)(e))switch(e.response?.status){case 400:return"Data browsing is not supported for this source.";case 403:return"You do not have permission to view this data.";case 404:return"Table not found in this source. Try refreshing the schema.";case 409:return"This source is not queryable right now. Reconnect or re-sync it.";case 503:return"Data is currently being updated. Try again shortly."}return"Failed to load table rows."})(Z)}),(0,s.jsx)($.$n,{size:"sm",variant:"outline",onClick:()=>W(),children:"Try Again"})]})}):0===t.length?(0,s.jsx)("div",{className:"flex flex-1 items-center justify-center rounded-lg border border-dashed border-gray-200 dark:border-border py-16",children:(0,s.jsxs)("div",{className:"flex flex-col items-center text-center max-w-sm px-6",children:[(0,s.jsx)("div",{className:"flex items-center justify-center w-12 h-12 rounded-full bg-gray-100 dark:bg-muted mb-4",children:(0,s.jsx)(j.A,{className:"w-6 h-6 text-gray-400 dark:text-gray-500"})}),(0,s.jsx)("h6",{className:"text-sm font-medium text-gray-900 dark:text-gray-100 mb-1",children:B>1?"No rows on this page":"No data yet"}),(0,s.jsx)("p",{className:"text-sm text-gray-500 dark:text-gray-400 mb-4",children:B>1?"You have paged past the last row of this table.":`The ${a?.name} table doesn't contain any rows.`}),B>1?(0,s.jsx)($.$n,{size:"sm",variant:"outline",onClick:()=>G(a.name,1),children:"Back to first page"}):(0,s.jsxs)($.$n,{size:"sm",variant:"outline",onClick:()=>W(),children:[(0,s.jsx)(o.A,{className:"w-3.5 h-3.5 mr-1.5"}),"Refresh"]})]})}):(0,s.jsx)("div",{className:(0,D.cn)(m,"transition-opacity",O&&I?"opacity-60":""),children:(0,s.jsx)(z.b,{data:t,initialColumnSizing:X[a.name],onColumnSizingChange:t=>{var s;return s=a.name,void(q?.data&&H({id:e,workspace:{...q.data,meta:{...q.data.meta,table_column_widths:{...X,[s]:t}}}}))},serverPagination:{page:B,pageSize:K,hasMore:V?.has_more??!1,isFetching:O,onPageChange:e=>G(a.name,e),onPageSizeChange:J}},a.name)})]}
10),"schema"===r&&a.foreignKeyRelationships.length>0&&(0,s.jsxs)("div",{className:"p-3 sm:p-4 border-t bg-gray-50 dark:bg-muted",children:[(0,s.jsxs)("div",{className:"flex items-center gap-2 mb-4",children:[(0,s.jsx)(u.A,{className:"w-4 h-4 text-gray-600 dark:text-gray-400"}),(0,s.jsx)("h6",{className:"font-medium text-gray-900 dark:text-gray-100",children:"Foreign Key Relationships"})]}),(0,s.jsx)("div",{className:"space-y-2",children:a.foreignKeyRelationships.map((e,a)=>(0,s.jsxs)("div",{className:"flex flex-col gap-2 bg-white dark:bg-card p-3 rounded border border-gray-200 dark:border-border",children:[(0,s.jsx)("div",{className:"text-xs text-gray-500 dark:text-gray-400 font-medium",children:e.name}),(0,s.jsxs)("div",{className:"flex flex-wrap items-center gap-3",children:[(0,s.jsx)("div",{className:"flex flex-wrap items-center gap-2",children:e.constrained_columns.map((e,a)=>(0,s.jsx)("code",{className:"font-mono text-sm bg-gray-100 dark:bg-muted px-2 py-1 rounded",children:e},a))}),(0,s.jsx)(u.A,{className:"w-4 h-4 text-gray-400"}),(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)("code",{className:"font-mono text-sm bg-green-100 dark:bg-green-900 text-green-accent dark:text-green-400 px-2 py-1 rounded",children:e.referred_table}),(0,s.jsx)("span",{className:"text-gray-400",children:"."}),(0,s.jsx)("div",{className:"flex items-center gap-1",children:e.referred_columns.map((e,a)=>(0,s.jsx)("code",{className:"font-mono text-sm bg-green-100 dark:bg-green-900 text-green-accent dark:text-green-400 px-2 py-1 rounded",children:e},a))})]})]}),e.comment&&(0,s.jsx)("div",{className:"text-xs text-gray-500 dark:text-gray-400 mt-1",children:e.comment})]},a))})]})]})})(t.tables.find(e=>e.name===i))}):(0,s.jsx)("div",{className:"h-full flex items-center justify-center text-gray-500 dark:text-gray-400",children:(0,s.jsxs)("div",{className:"text-center px-6",children:[(0,s.jsx)(c.A,{className:"w-12 h-12 mx-auto mb-4 text-gray-300 dark:text-gray-600"}),(0,s.jsx)("p",{className:"text-base sm:text-lg font-medium",children:"Select a table to view details"}),(0,s.jsx)("p",{className:"text-sm",children:"Choose a table from the list to explore its structure"})]})})})]})})]})}var K=t(76619),F=t(94946),B=t.n(F),M=t(9643),V=t(53455),P=t(59950),O=t(5379),I=t(99967),Z=t(63111),W=t(58493),q=t(7393);let H={tableNode:_,sampleDataNode:S},X=(e,a,t="TB",s,r)=>{let n=new(B()).graphlib.Graph;n.setDefaultEdgeLabel(()=>({})),n.setGraph({rankdir:t,nodesep:200,ranksep:250}),e.forEach(e=>{"sampleDataNode"===e.type?n.setNode(e.id,{width:520,height:600}):n.setNode(e.id,{width:520,height:500})}),a.forEach(e=>{e.source&&e.target&&n.setEdge(e.source,e.target)});try{B().layout(n);let t=e.map(e=>{let a=n.node(e.id);if(!a)return{...e,position:{x:0,y:0}};let t="sampleDataNode"===e.type?600:500;return{...e,position:{x:a.x-260,y:a.y-t/2}}}),l=new Map,i=new Map;t.forEach(e=>{if("tableNode"===e.type)l.set(e.id,e);else if("sampleDataNode"===e.type&&e.id.endsWith("_sample_data")){let a=e.id.replace("_sample_data","");i.set(a,e)}}),s&&s.size>0&&t.forEach(e=>{if("tableNode"===e.type){let a=s.get(e.id);a&&(e.position=a)}});let d=(e,a)=>e.x<a.x+a.width&&e.x+e.width>a.x&&e.y<a.y+a.height&&e.y+e.height>a.y;return i.forEach((e,a)=>{let t=`${a}_sample_data`;if(r?.has(t))return;let n=l.get(a);if(n){let t=s?.get(a)||n.position,o={x:t.x+520+40,y:t.y},c=!1,m=0;do{c=!1;let e={x:o.x,y:o.y,width:520,height:600};for(let[t,r]of l.entries()){if(t===a)continue;let n=s?.get(t)||r.position,l={x:n.x,y:n.y,width:520,height:500};if(d(e,l)){c=!0,o.y+=50,m++;break}}if(!c)for(let[t,s]of i.entries()){if(t===a)continue;let n=`${t}_sample_data`;if(!r?.has(n)&&s.position&&0!==s.position.x&&0!==s.position.y){let a={x:s.position.x,y:s.position.y,width:520,height:600};if(d(e,a)){c=!0,o.y+=50,m++;break}}}if(m>=20){let e=Array.from(l.values()).map(e=>(s?.get(e.id)||e.position).y+500),a=Array.from(i.values()).filter(e=>{let a=`${e.id.replace("_sample_data","")}_sample_data`;return!!r?.has(a)||e.position&&0!==e.position.x&&0!==e.position.y}).map(e=>(r?.get(e.id)||e.position).y+600),t=[...e,...a];o.y=(t.length>0?Math.max(...t):0)+40,c=!1;break}}while(c);e.position=o}}),{nodes:t,edges:a}}catch(t){return{nodes:e.map((e,a)=>({...e,position:{x:200*a,y:100}})),edges:a}}};function J({workspaceId:e,dataSourceId:a,sourceType:t,selectTenantTable:n,selectedTableName:i,onSelectTableName:j}){let y=!!j,{resolvedTheme:N}=(0,O.D)(),{notify:w}=(0,I.o)(),{currentWorkspace:v}=(0,Z.GV)(e=>
10e.workspace),{role:k,isViewer:_}=(0,q.v)(e),C=null!==k?_:v?.user_role==="viewer",S=a?W.Qn.includes(t?.toLowerCase()??""):v?.workspace_id===e&&W.Qn.includes(v.workspace_type?.toLowerCase()??""),[A,T]=(0,r.useState)(!1),[z,L,D]=(0,f.ck)([]),[F,B,J]=(0,f.fM)([]),[U,G]=(0,r.useState)("TB"),[Q,Y]=(0,r.useState)("list"),[ee,ea]=(0,r.useState)(!1),[et,es]=(0,r.useState)(null),[er,en]=(0,r.useState)(new Set),[el,ei]=(0,r.useState)(new Map),[ed,eo]=(0,r.useState)(new Map),ec=r.useRef([]),em=(0,r.useCallback)(e=>{en(a=>{let t=new Set(a);return t.has(e)?t.delete(e):t.add(e),t})},[]),eh=(0,r.useCallback)(e=>{D(e)},[D]),ex=(0,r.useCallback)((e,a)=>{a.id.endsWith("_sample_data")?eo(e=>{let t=new Map(e);return t.set(a.id,a.position),t}):"tableNode"===a.type&&ei(e=>{let t=new Map(e);return t.set(a.id,a.position),t})},[]);(0,r.useEffect)(()=>{ec.current=z},[z]);let{data:eu,isLoading:ep,error:ef,refetch:eg}=(0,V.useQuery)({queryKey:a?P.l.workspace.dataSourceSchema(e,a):P.l.workspace.schema(e),queryFn:()=>a?(0,M.t6)(e,a):(0,M.x9)(e),enabled:!!e}),{parsedSchema:eb,analysis:ej}=(0,r.useMemo)(()=>{if(!eu?.success)return{parsedSchema:null,analysis:null};let e=K.B.parseSchemaResponse(eu);return{parsedSchema:e,analysis:K.B.analyzeSchema(e)}},[eu]);(0,r.useEffect)(()=>{if(eb&&eb.tables){let e=eb.tables.filter(e=>e.sampleRecords&&e.sampleRecords.length>0).map(e=>e.name);en(a=>0===a.size?new Set(e):a)}},[eb]);let ey=ef?(0,R.F0)(ef)&&ef.response?.data?.message||(ef instanceof Error?ef.message:"Unknown error occurred"):null,eN=async({refresh:t=!1}={})=>{if(e){if(!t)return void eg();T(!0);try{await (a?(0,M.Cl)(e,a):(0,M.Gr)(e)),await eg(),w("schema.refreshed",{label:"Database"})}catch(e){w("schema.failed",{label:"Database"})}finally{T(!1)}}};(0,r.useEffect)(()=>{if(!eb||!ej)return;let e=new Set(eb.tables.map(e=>e.name)),a=new Map;eb.tables.forEach(e=>{a.set(e.name,{incoming:!1,outgoing:!1})});let t=[];eb.tables.forEach(s=>{s.foreignKeyRelationships.forEach((r,n)=>{if(e.has(r.referred_table)){t.push({id:`${s.name}-${r.referred_table}-${n}`,source:s.name,target:r.referred_table,type:"smoothstep",animated:!0,style:{stroke:"#07825e",strokeWidth:2},markerEnd:{type:g.TG.ArrowClosed,color:"#07825e",width:20,height:20},label:1===r.constrained_columns.length&&1===r.referred_columns.length?`${r.constrained_columns[0]} â ${r.referred_columns[0]}`:r.name,labelStyle:{fill:"#064332",fontSize:12,fontWeight:600},labelBgStyle:{fill:"#ffffff",fillOpacity:.9},labelBgPadding:[6,6],labelBgBorderRadius:4});let e=a.get(s.name),l=a.get(r.referred_table);e&&(e.outgoing=!0),l&&(l.incoming=!0)}})});let s=new Set;t.forEach(e=>{s.add(e.source),s.add(e.target)});let r=eb.tables.map(e=>{let t=a.get(e.name),s=[];return t?.incoming||s.push(g.yX.Left),t?.outgoing||s.push(g.yX.Right),{id:e.name,type:"tableNode",data:{table:e,isPrimary:ej.primaryTables.includes(e.name),onSelectAsTenant:n,hiddenHandles:s,hasSampleData:!!(e.sampleRecords&&e.sampleRecords.length>0),isSampleDataVisible:!er.has(e.name),onToggleSampleData:()=>em(e.name)},position:{x:0,y:0}}}),l=eb.tables.filter(e=>e.sampleRecords&&e.sampleRecords.length>0&&!er.has(e.name)).map(e=>({id:`${e.name}_sample_data`,type:"sampleDataNode",data:{table:e,sampleRecords:e.sampleRecords},position:{x:0,y:0}})),i=eb.tables.filter(e=>e.sampleRecords&&e.sampleRecords.length>0&&!er.has(e.name)).map(e=>({id:`${e.name}_to_sample_data`,source:e.name,sourceHandle:"sample-data",target:`${e.name}_sample_data`,targetHandle:null,type:"smoothstep",animated:!1,style:{stroke:"#3b82f6",strokeWidth:2,strokeDasharray:"5,5"},label:"Sample Data",labelStyle:{fill:"#1e40af",fontSize:11,fontWeight:500},labelBgStyle:{fill:"#ffffff",fillOpacity:.9},labelBgPadding:[4,4],labelBgBorderRadius:4})),d=t.filter(e=>s.has(e.source)&&s.has(e.target)),o=[...r,...l],c=[...d,...i],m=new Map;ec.current.forEach(e=>{"tableNode"===e.type&&m.set(e.id,{...e.position})});let{nodes:h,edges:x}=X(o,c,U,m.size>0?m:el.size>0?el:void 0,ed);if(0===el.size&&0===m.size){let e=new Map;h.forEach(a=>{"tableNode"===a.type&&e.set(a.id,{...a.position})}),e.size>0&&ei(e)}let u=h.filter(e=>{if("sampleDataNode"===e.type&&e.id.endsWith("_sample_data")){let a=e.id.replace("_sample_data","");return!er.has(a)}return!0}),p=(e,a)=>e.x<a.x+a.width&&e.x+e.width>a.x&&e.y<a.y+a.height&&e.y+e.height>a.y;
10u.forEach(e=>{if("sampleDataNode"===e.type&&e.id.endsWith("_sample_data")){let a=ed.get(e.id);if(a)e.position=a;else{let a=e.id.replace("_sample_data",""),t=el.get(a),s=ec.current.find(e=>e.id===a&&"tableNode"===e.type),r=t||s?.position;if(r){let t={x:r.x+520+40,y:r.y},s=!1,n=0;do{s=!1;let r={x:t.x,y:t.y,width:520,height:600};for(let l of ec.current)if("tableNode"===l.type&&l.id!==a){if(p(r,{x:l.position.x,y:l.position.y,width:520,height:500})){s=!0,t.y+=50,n++;break}}else if("sampleDataNode"===l.type&&l.id!==e.id&&p(r,{x:l.position.x,y:l.position.y,width:520,height:600})){s=!0,t.y+=50,n++;break}if(n>=20){let a=[...ec.current.filter(e=>"tableNode"===e.type).map(e=>e.position.y+500),...ec.current.filter(a=>"sampleDataNode"===a.type&&a.id!==e.id).map(e=>e.position.y+600)];t.y=(a.length>0?Math.max(...a):0)+40,s=!1;break}}while(s);e.position=t,eo(a=>{let s=new Map(a);return s.set(e.id,t),s})}}}});let f=x.filter(e=>{if(e.target&&e.target.endsWith("_sample_data")){let a=e.target.replace("_sample_data","");return!er.has(a)}return!0});L(u),B(f),ec.current=u},[eb,ej,n,U,er,ed,el]);let ew=(0,r.useCallback)(e=>{ei(new Map),eo(new Map);let{nodes:a,edges:t}=X(z,F,e,void 0,void 0);L(a),B(t),G(e)},[z,F]);return ep||A?(0,s.jsx)("div",{className:"h-full flex items-center justify-center bg-white dark:bg-background",children:(0,s.jsxs)("div",{className:"text-center",children:[(0,s.jsx)(l.A,{className:"w-12 h-12 mx-auto mb-4 text-green-accent animate-spin"}),(0,s.jsx)("p",{className:"text-lg font-semibold text-foreground mb-2",children:"Loading Database Schema"}),(0,s.jsx)("p",{className:"text-muted-foreground",children:"Analyzing your database structure..."})]})}):ey?(0,s.jsx)("div",{className:"h-full flex items-center justify-center bg-gradient-to-br from-slate-50 to-red-50 dark:from-slate-950 dark:to-red-950",children:(0,s.jsxs)("div",{className:"text-center max-w-md",children:[(0,s.jsx)(d.A,{className:"w-16 h-16 mx-auto mb-4 text-red-500"}),(0,s.jsx)("p",{className:"text-xl font-semibold text-foreground mb-2",children:"Failed to Load Schema"}),(0,s.jsx)("p",{className:"text-muted-foreground mb-6",children:ey}),(0,s.jsxs)($.$n,{onClick:()=>eN({refresh:!0}),variant:"outline",className:"gap-2",children:[(0,s.jsx)(o.A,{className:"w-4 h-4"}),"Try Again"]})]})}):eb&&ej?(0,s.jsxs)("div",{className:"flex flex-col h-full w-full",children:[(0,s.jsx)("div",{className:"flex-1 overflow-hidden w-full min-h-0",children:"diagram"===Q?(0,s.jsx)("div",{className:"relative bg-background dark:bg-background/60 w-full h-full min-h-[600px]",children:(0,s.jsxs)(f.Gc,{nodes:z,edges:F,onNodesChange:eh,onEdgesChange:J,onNodeDragStop:ex,nodeTypes:H,fitView:!0,fitViewOptions:{padding:.25},minZoom:.2,maxZoom:2,defaultEdgeOptions:{animated:!0},proOptions:{hideAttribution:!0},children:[(0,s.jsx)(f.H2,{}),(0,s.jsx)(f.of,{}),(0,s.jsx)(f.VS,{variant:f._5.Dots,gap:16,size:1.5,color:"dark"===N?"rgba(255,255,255,0.25)":"rgba(148, 163, 184, 0.7)"}),(0,s.jsx)(f.Zk,{position:"top-left",className:"m-2",children:(0,s.jsx)($.Bc,{children:(0,s.jsx)("div",{className:"bg-white/90 dark:bg-card/90 backdrop-blur-sm rounded-md shadow-lg border border-border p-2",children:(0,s.jsxs)("div",{className:"flex items-center gap-1.5",children:[(0,s.jsxs)($.m_,{children:[(0,s.jsx)($.k$,{asChild:!0,children:(0,s.jsx)($.$n,{onClick:()=>Y("list"),variant:"list"===Q?"default":"outline",size:"sm",className:"h-8 w-8 min-w-8 max-w-8 p-0 flex-shrink-0 shadow-none","aria-label":"List view","aria-pressed":"list"===Q,children:(0,s.jsx)(m.A,{className:"w-4 h-4"})})}),(0,s.jsx)($.ZI,{children:(0,s.jsx)("p",{children:"List View"})})]}),(0,s.jsxs)($.m_,{children:[(0,s.jsx)($.k$,{asChild:!0,children:(0,s.jsx)($.$n,{onClick:()=>Y("diagram"),variant:"diagram"===Q?"default":"outline",size:"sm",className:"h-8 w-8 min-w-8 max-w-8 p-0 flex-shrink-0 shadow-none","aria-label":"Diagram view","aria-pressed":"diagram"===Q,children:(0,s.jsx)(h.A,{className:"w-4 h-4"})})}),(0,s.jsx)($.ZI,{children:(0,s.jsx)("p",{children:"Diagram View"})})]}),(0,s.jsxs)($.m_,{children:[(0,s.jsx)($.k$,{asChild:!0,children:(0,s.jsx)($.$n,{onClick:()=>ew("TB"),variant:"TB"===U?"default":"outline",size:"sm",className:"h-8 w-8 min-w-8 max-w-8 p-0 flex-shrink-0 shadow-none","aria-label":"Vertical layout","aria-pressed":"TB"===U,children:(0,s.jsx)(x.A,{className:"w-4 h-4"})})}),(0,s.jsx)($.ZI,{children:(0,s.jsx)("p",{children:"Vertical Layout"})})]}),(0,s.jsxs)($.m_,{children:[(0,s.jsx)($.k$,{asChild:!0,children:(0,s.jsx)($.$n,{onClick:()=>ew("LR"),variant:"LR"===U?"default":"outline",size:"sm",className:"h-8 w-8 min-w-8 max-w-8 p-0 flex-shrink-0 shadow-none","aria-label":"Horizontal layout","aria-pressed":"LR"===U,children:(0,s.jsx)(u.A,{className:"w-4 h-4"})})}),(0,s.jsx)($.ZI,{children:(0,s.jsx)("p",{children:"Horizontal Layout"})})]}),(0,s.jsxs)($.m_,{children:[(0,s.jsx)($.k$,{asChild:!0,children:(0,s.jsx)($.$n,{onClick:()=>eN({refresh:!0}),variant:"outline",size:"sm",className:"h-8 w-8 min-w-8 max-w-8 p-0 flex-shrink-0 shadow-none","aria-label":"Refresh schema",children:(0,s.jsx)(o.A,{className:"w-4 h-4"})})}),(0,s.jsx)($.ZI,{children:(0,s.jsx)("p",{children:"Refresh"})})]}),(0,s.jsxs)($.m_,{children:[(0,s.jsx)($.k$,{asChild:!0,children:(0,s.jsx)($.$n,{variant:"outline",size:"sm",onClick:()=>ea(!0),className:"h-8 w-8 min-w-8 max-w-8 p-0 flex-shrink-0 shadow-none","aria-label":"Expand schema viewer",children:(0,s.jsx)(p.A,{className:"w-4 h-4"})})}),(0,s.jsx)($.ZI,{children:(0,s.jsx)("p",{children:"Expand"})})]})]})})})})]})}):eb&&ej&&(0,s.jsx)(E,{workspaceId:e,dataSourceId:a,parsedSchema:eb,anal
10ysis:ej,selectTenantTable:n,selectedTable:y?i??null:et,onSelectTable:j??es,hideTableList:y,viewType:Q,onViewTypeChange:Y,onRefreshSchema:()=>eN({refresh:!0}),canToggleSchemaData:S&&!C})}),(0,s.jsx)(b.lG,{open:ee,onOpenChange:ea,children:(0,s.jsxs)(b.Cf,{className:"w-screen h-screen max-w-none max-h-none p-0 m-0 rounded-none translate-x-0 translate-y-0 left-0 top-0",children:[(0,s.jsx)(b.L3,{className:"sr-only",children:"Database Schema Diagram View"}),(0,s.jsx)("div",{style:{height:"100vh",width:"100%"},className:"relative bg-background dark:bg-background/60",children:(0,s.jsxs)(f.Gc,{nodes:z,edges:F,onNodesChange:eh,onEdgesChange:J,onNodeDragStop:ex,nodeTypes:H,fitView:!0,fitViewOptions:{padding:.25},minZoom:.2,maxZoom:2,defaultEdgeOptions:{animated:!0},proOptions:{hideAttribution:!0},children:[(0,s.jsx)(f.H2,{}),(0,s.jsx)(f.of,{}),(0,s.jsx)(f.VS,{variant:f._5.Dots,gap:16,size:1.5}),(0,s.jsx)(f.Zk,{position:"top-left",className:"m-2",children:(0,s.jsx)($.Bc,{children:(0,s.jsx)("div",{className:"bg-white/90 dark:bg-card/90 backdrop-blur-sm rounded-md shadow-lg border border-border p-2",children:(0,s.jsxs)("div",{className:"flex items-center gap-1.5",children:[(0,s.jsxs)($.m_,{children:[(0,s.jsx)($.k$,{asChild:!0,children:(0,s.jsx)($.$n,{onClick:()=>Y("diagram"),variant:"diagram"===Q?"default":"outline",size:"sm",className:"h-8 w-8 min-w-8 max-w-8 p-0 flex-shrink-0 shadow-none","aria-label":"Diagram view","aria-pressed":"diagram"===Q,children:(0,s.jsx)(h.A,{className:"w-4 h-4"})})}),(0,s.jsx)($.ZI,{children:(0,s.jsx)("p",{children:"Diagram View"})})]}),(0,s.jsxs)($.m_,{children:[(0,s.jsx)($.k$,{asChild:!0,children:(0,s.jsx)($.$n,{onClick:()=>ew("TB"),variant:"TB"===U?"default":"outline",size:"sm",className:"h-8 w-8 min-w-8 max-w-8 p-0 flex-shrink-0 shadow-none","aria-label":"Vertical layout","aria-pressed":"TB"===U,children:(0,s.jsx)(x.A,{className:"w-4 h-4"})})}),(0,s.jsx)($.ZI,{children:(0,s.jsx)("p",{children:"Vertical Layout"})})]}),(0,s.jsxs)($.m_,{children:[(0,s.jsx)($.k$,{asChild:!0,children:(0,s.jsx)($.$n,{onClick:()=>ew("LR"),variant:"LR"===U?"default":"outline",size:"sm",className:"h-8 w-8 min-w-8 max-w-8 p-0 flex-shrink-0 shadow-none","aria-label":"Horizontal layout","aria-pressed":"LR"===U,children:(0,s.jsx)(u.A,{className:"w-4 h-4"})})}),(0,s.jsx)($.ZI,{children:(0,s.jsx)("p",{children:"Horizontal Layout"})})]}),(0,s.jsxs)($.m_,{children:[(0,s.jsx)($.k$,{asChild:!0,children:(0,s.jsx)($.$n,{onClick:()=>eN({refresh:!0}),variant:"outline",size:"sm",className:"h-8 w-8 min-w-8 max-w-8 p-0 flex-shrink-0 shadow-none","aria-label":"Refresh schema",children:(0,s.jsx)(o.A,{className:"w-4 h-4"})})}),(0,s.jsx)($.ZI,{children:(0,s.jsx)("p",{children:"Refresh"})})]})]})})})})]})})]})})]}):(0,s.jsx)("div",{className:"h-full flex items-center justify-center bg-gradient-to-br from-slate-50 to-gray-50 dark:from-background dark:to-background",children:(0,s.jsxs)("div",{className:"text-center max-w-md",children:[(0,s.jsx)(c.A,{className:"w-20 h-20 mx-auto mb-4 text-gray-300"}),(0,s.jsx)("p",{className:"text-2xl font-semibold text-foreground mb-2",children:"No Schema Available"}),(0,s.jsx)("p",{className:"text-muted-foreground mb-6",children:"Unable to load database schema for this workspace"}),(0,s.jsxs)($.$n,{onClick:()=>eN({refresh:!0}),variant:"outline",className:"gap-2",children:[(0,s.jsx)(o.A,{className:"w-4 h-4"}),"Retry"]})]})})}var U=t(11696),G=t(71360),Q=t(6191),Y=t(76907),ee=t(39068),ea=t(38392),et=t(4788),es=t(17787),er=t(49027),en=t(49968),el=t(11186),ei=t(23594),ed=t(11647),eo=t(86948),ec=t(76444),em=t(44846),eh=t(15894),ex=t(99776);let eu=e=>!!e&&"tenant"===e.scope&&!!e.filter?.table&&!!e.filter?.column,ep=(e,a)=>e instanceof Error?e.message:a,ef=e=>e&&(e.columns.find(e=>e.primaryKey)?.name||e.columns.find(e=>"id"===e.name.toLowerCase())?.name||e.columns[0]?.name)||"";function eg(e){return Object.entries(e||{}).reduce((e,[a,t])=>(e[a]={...t,joins:t.joins||[]},e),{})}function eb({tableName:e,joins:a,filterTable:t,filterColumn:n,compact:l=!1}){let i=((e,a)=>{let t=e?[e]:[];return a.forEach(e=>{e.to_table&&t.push(e.to_table)}),t})(e,a),d=!!t&&!!n,o=0===a.length;return(0,s.jsxs)("div",{className:l?"space-y-2 text-xs":"space-y-3 text-sm",children:[(0,s.jsxs)("div",{className:"flex flex-wrap items-center gap-2",children:[i.map((e,a)=>(0,s.jsxs)(r.Fragment,{children:[(0,s.jsx)("code",{className:"rounded-md border bg-background px-2 py-1 font-mono",children:e}),a<i.length-1&&(0,s.jsx)("span",{className:"text-muted-foreground",children:"joins to"})]},`${e}-${a}`)),0===i.length&&(0,s.jsx)("span",{className:"text-muted-foreground",children:"Choose a table to start."})]}),a.length>0&&(0,s.jsx)("div",{className:"space-y-1",children:a.map((e,a)=>(0,s.jsxs)("div",{className:"text-muted-foreground",children:["Join ",a+1,": ",(0,s.jsx)("code",{className:"font-mono",children:`${e.from_table}.${e.from_column} = ${e.to_table}.${e.to_column}`})]},`preview-join-${a}`))}),(0,s.jsxs)("div",{className:"text-muted-foreground",children:[o?"Filter rows directly with ":"After the joins, filter with ",d?(0,s.jsxs)("code",{className:"font-mono text-foreground",children:[t,".",n," = tenant id"]}):(0,s.jsx)("span",{children:"the tenant id column."})]})]})}function ej({tableName:e,rule:a,onEditPath:t,isDragging:r=!1}){return(0,s.jsxs)("div",{className:`rounded-lg border bg-white p-3 shadow-sm dark:bg-card ${r?"opacity-50":""}`,children:[(0,s.jsxs)("div",{className:"flex items-start justify-between gap-2",children:[(0,s.jsx)("code",{className:"min-w-0 break-words font-mono text-sm font-semibold",children:e}),"tenant"===a.scope&&t&&(0,s.jsx)("button",{type:"button","aria-label":`View or edit tenant path for ${e}`,onClick:()=>t(e),title:"View or edit tenant path",className:"flex-shrink-0 rounded p-1 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground",children:(0,s.jsx)(U.A,{className:"h-3.5 w-3.5"})})]}),"tenant"===a.scope&&(0,s.jsxs)("div",{className:"mt-2 flex flex-wrap gap-1.5",children:[(0,s.jsx)(ed.E,{variant:"secondary",className:"text-xs",children:(a.joins||[]).length>0?"Joined path":"Direct filter"}),(0,s.jsx)(ed.E,{variant:"outline",className:"text-xs",children:a?.filter?`${a.filter.table}.${a.filter.column}`:"No tenant filter"})]}),"tenant"===a.scope&&(0,s.jsx)("div",{className:"mt-2 rounded-md bg-muted/50 p-2",children:(0,s.jsx)(eb,{tableName:e,joins:a.joins||[],filterTable:a.filter?.table,filterColumn:a.filter?.column,compact:!0})})]})}function ey({tableName:e,rule:a,onEditPath:t}){let{attributes:r,listeners:n,setNodeRef:l,transform:i,transition:d,isDragging:o}=(0,er.gl)({id:e}),c={transform:en.Ks.Transform.toString(i),transition:d,cursor:o?"grabbing":"grab"};return(0,s.jsx)("div",{ref:l,style:c,...r,...n,children:(0,s.jsx)(ej,{tableName:e,rule:a,onEditPath:t,isDragging:o})})}function eN({id:e,children:a}){let{setNodeRef:t,isOver:r}=(0,es.zM)({id:e});return(0,s.jsx)("div",{ref:t,className:`flex flex-1 flex-col space-y-3 rounded-lg transition-colors ${r?"bg-muted/40":""}`,children:a})}function ew({label:e,tableName:a,value:t,schema:r,onChange:n}){let l=r.get(a)||[];return(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsx)(ec.J,{children:e}),(0,s.jsxs)(el.l6,{value:t,onValueChange:n,children:[(0,s.jsx)(el.bq,{children:(0,s.jsx)(el.yv,{placeholder:"Select column"})}),(0,s.jsx)(el.gC,{children:l.map(e=>(0,s.jsx)(el.eb,{value:e.name,children:e.name},e.name))})]})]})}function ev({tableName:e,open:a,schema:t,existingRule:n,onCancel:l,onSave:d}){let o=Array.from(t.keys()).sort(),[c,m]=(0,r.useState)([]),[h,x]=(0,r.useState)(""),[u,p]=(0,r.useState)(""),f=c.length>0?c[c.length-1].to_table:e||"",g=!!e&&!!h&&!!u,j=c.some(e=>!e.from_column||!e.to_table||!e.to_column);(0,r.useEffect)(()=>{a&&(m(n?.joins||[]),x(n?.filter?.table||e||""),p(n?.filter?.column||""))},[n,a,e]);let y=(e,a)=>{m(t=>t.map((t,s)=>s===e?{...t,...a}:t))};return(0,s.jsx)(b.lG,{open:a,onOpenChange:e=>!e&&l(),children:(0,s.jsxs)(b.Cf,{className:"max-h-[90vh] overflow-y-auto sm:max-w-2xl",children:[(0,s.jsxs)(b.c7,{children:[(0,s.jsx)(b.L3,{children:"Tenant Access Path"}),(0,s.jsxs)(b.rr,{children:["Tell Alan how rows in ",(0,s.jsx)("code",{className:"font-mono",children:e})," belong to the current tenant."]})]}),(0,s.jsxs)("div",{className:"space-y-5 py-2",children:[(0,s.jsxs)("div",{className:"rounded-lg border bg-muted/40 p-4",children:[(0,s.jsx)("div",{className:"mb-3 text-sm font-medium",children:"Current rule"}),(0,s.jsx)(eb,{tableName:e,joins:c,filterTable:h,filterColumn:u})]}),(0,s.jsxs)("div",{className:"rounded-lg border p-4 text-sm text-muted-foreground",children:["If ",(0,s.jsx)("code",{className:"font-mono",children:e}
10)," already has the tenant id, skip joins and choose that column below. Add joins only when the tenant id lives on another table."]}),(0,s.jsx)("div",{className:"space-y-4",children:c.map((a,r)=>(0,s.jsxs)("div",{className:"grid gap-3 rounded-lg border p-4 md:grid-cols-2",children:[(0,s.jsxs)("div",{className:"flex items-center justify-between md:col-span-2",children:[(0,s.jsxs)("div",{children:[(0,s.jsxs)("div",{className:"text-sm font-medium",children:["Join ",r+1]}),(0,s.jsxs)("div",{className:"text-xs text-muted-foreground",children:["Connect ",(0,s.jsx)("code",{className:"font-mono",children:a.from_table})," to the next table in the path."]})]}),(0,s.jsxs)(i.$,{type:"button",variant:"outline",size:"sm",onClick:()=>{m(a=>{let t=a.slice(0,r);return x(t.length>0?t[t.length-1].to_table:e||""),p(""),t})},className:"gap-2",children:[(0,s.jsx)(G.A,{className:"h-4 w-4"}),"Remove"]})]}),(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsx)(ec.J,{children:"Starting table for this join"}),(0,s.jsx)("div",{className:"rounded-md border bg-muted px-3 py-2 font-mono text-sm",children:a.from_table})]}),(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsx)(ec.J,{children:"Table it joins to"}),(0,s.jsxs)(el.l6,{value:a.to_table,onValueChange:e=>{y(r,{to_table:e,to_column:""}),x(e),p("")},children:[(0,s.jsx)(el.bq,{children:(0,s.jsx)(el.yv,{placeholder:"Select table"})}),(0,s.jsx)(el.gC,{children:o.map(e=>(0,s.jsx)(el.eb,{value:e,children:e},e))})]})]}),(0,s.jsx)(ew,{label:`Column on ${a.from_table}`,tableName:a.from_table,value:a.from_column,schema:t,onChange:e=>y(r,{from_column:e})}),(0,s.jsx)(ew,{label:`Matching column on ${a.to_table||"the joined table"}`,tableName:a.to_table,value:a.to_column,schema:t,onChange:e=>y(r,{to_column:e})})]},`join-${r}`))}),(0,s.jsxs)(i.$,{variant:"outline",onClick:()=>{m(e=>[...e,{from_table:f,from_column:"",to_table:"",to_column:""}])},className:"gap-2",children:[(0,s.jsx)(Q.A,{className:"h-4 w-4"}),"Add Join Table"]}),(0,s.jsxs)("div",{className:"grid gap-3 rounded-lg border p-4 md:grid-cols-2",children:[(0,s.jsxs)("div",{className:"md:col-span-2",children:[(0,s.jsx)("div",{className:"text-sm font-medium",children:"Tenant id location"}),(0,s.jsx)("p",{className:"mt-1 text-sm text-muted-foreground",children:"This is the column Alan compares with the tenant id from the API request."})]}),(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsx)(ec.J,{children:"Table that contains the tenant id"}),(0,s.jsxs)(el.l6,{value:h,onValueChange:e=>{x(e),p("")},children:[(0,s.jsx)(el.bq,{children:(0,s.jsx)(el.yv,{placeholder:"Select table"})}),(0,s.jsx)(el.gC,{children:o.map(e=>(0,s.jsx)(el.eb,{value:e,children:e},e))})]})]}),(0,s.jsx)(ew,{label:"Tenant id column",tableName:h,value:u,schema:t,onChange:p})]}),j&&(0,s.jsx)("div",{className:"rounded-lg border border-amber-200 bg-amber-50 p-3 text-sm text-amber-800 dark:border-amber-800 dark:bg-amber-950/30 dark:text-amber-200",children:"Finish each join by choosing both columns and the joined table."})]}),(0,s.jsxs)(b.Es,{children:[(0,s.jsx)(i.$,{variant:"outline",onClick:l,children:"Cancel"}),(0,s.jsx)(i.$,{disabled:!g||j,onClick:()=>{e&&d(e,{scope:"tenant",joins:c,filter:{table:h,column:u}})},children:"Save Path"})]})]})})}function ek({workspaceId:e,selectedTable:a,onClose:t,onConfigure:n,onSuccess:m,shouldFetchConfig:h=!0}){let x=(0,ex.useQueryClient)(),{data:u}=(0,ei.w)(e,h),p=u?.data,[f,g]=(0,r.useState)(!1),[b,j]=(0,r.useState)({}),[y,N]=(0,r.useState)({}),[w,v]=(0,r.useState)(!1),[k,_]=(0,r.useState)(!0),[C,S]=(0,r.useState)(new Map),[$,A]=(0,r.useState)(null),[T,R]=(0,r.useState)(()=>ef(a)),[z,L]=(0,r.useState)(null),D=(0,es.FR)((0,es.MS)(es.AN,{activationConstraint:{distance:5}})),E=a?.columns.some(e=>e.primaryKey)??!1,F=p?.tenant_table===a?.name,B=!h||!p||!F||f,{data:V,error:O,isLoading:I,refetch:Z}=(0,ei.O)({workspaceId:e,tenantTable:a?.name||null,tenantColumn:T,enabled:B}),W=(0,r.useMemo)(()=>f&&V?.data?V.data:p&&p.tenant_table===a?.name?{tenant_table:p.tenant_table,tenant_id_column:T||p.tenant_column,tenant_column:T||p.tenant_column,tenant_hierarchy:p.tenant_hierarchy}:V?.data,[V?.data,p,a?.name,T,f]);if((0,r.useEffect)(()=>
10{R(ef(a)),g(!1)},[a]),(0,r.useEffect)(()=>{if(!W)return;let e=eg(W.tenant_hierarchy.tables);j(e),N(a=>({...a,...e}))},[W]),(0,r.useEffect)(()=>{p&&_(p.enabled??!0)},[p]),(0,r.useEffect)(()=>{if(!e)return;let a=!1;return(0,M.x9)(e).then(e=>{if(a||!e.success)return;let t=K.B.parseSchemaResponse(e),s=new Map;t.tables.forEach(e=>s.set(e.name,e.columns)),S(s)}).catch(e=>{}),()=>{a=!0}},[e]),!a)return null;let q={tenant:Object.entries(b).filter(([,e])=>"tenant"===e.scope),shared:Object.entries(b).filter(([,e])=>"shared"===e.scope),excluded:Object.entries(b).filter(([,e])=>"excluded"===e.scope)},H=async()=>{if(a)try{g(!0);let e=await Z({throwOnError:!0});if(e.data?.data?.tenant_hierarchy?.tables){let a=eg(e.data.data.tenant_hierarchy.tables);j(a),N(e=>({...e,...a}))}(0,eh.toast)({title:"Reanalysis Complete",description:"Tenant policies have been regenerated."})}catch(e){g(!1),(0,eh.toast)({title:"Reanalysis Error",description:ep(e,"Failed to reanalyze tenant table."),variant:"destructive"})}},X=async a=>{let t=Object.entries(b).filter(([,e])=>"tenant"===e.scope&&!eu(e));if(t.length>0)return void(0,eh.toast)({title:"Missing Tenant Paths",description:`Add tenant paths for: ${t.map(([e])=>e).join(", ")}`,variant:"destructive"});v(!0);try{await (0,M.DJ)({workspace_id:e,enabled:k,tenant_table:a.name,tenant_column:T||W?.tenant_id_column||W?.tenant_column||"",tenant_hierarchy:{tables:b}}),x.invalidateQueries({queryKey:P.l.multitenant.config(e)}),(0,eh.toast)({title:"Tenant Policies Saved",description:"Multi-tenant configuration was saved successfully."}),n(a),m?.()}catch(e){(0,eh.toast)({title:"Configuration Failed",description:"Failed to save tenant policies. Please review the paths and try again.",variant:"destructive"})}finally{v(!1)}},J={tenant:"column-tenant",shared:"column-shared",excluded:"column-excluded"},U={"column-tenant":"tenant","column-shared":"shared","column-excluded":"excluded"},G=z?b[z]:null;return(0,s.jsxs)("div",{className:"h-full overflow-y-auto",children:[(0,s.jsxs)("div",{className:"space-y-6 px-6 pb-24",children:[(0,s.jsxs)("div",{className:"space-y-2",children:[(0,s.jsxs)("div",{className:"flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between",children:[(0,s.jsxs)("div",{className:"flex items-center gap-3",children:[(0,s.jsx)(c.A,{className:"h-6 w-6 text-green-accent"}),(0,s.jsx)("h2",{className:"text-2xl font-semibold",children:"Tenant Table Configuration"})]}),(0,s.jsxs)("div",{className:"flex flex-wrap items-center gap-3",children:[(0,s.jsxs)(i.$,{variant:"outline",size:"sm",onClick:H,disabled:I||!a||!T,className:"gap-2",children:[I?(0,s.jsx)(l.A,{className:"h-4 w-4 animate-spin"}):(0,s.jsx)(o.A,{className:"h-4 w-4"}),"Re-run Analysis"]}),(0,s.jsx)(em.d,{id:"enable-tenant",checked:k,onCheckedChange:_}),(0,s.jsx)(ec.J,{htmlFor:"enable-tenant",children:k?"Enabled":"Disabled"})]})]}),(0,s.jsxs)("p",{className:"text-sm text-muted-foreground",children:["Review generated policies for"," ",(0,s.jsx)("code",{className:"rounded bg-muted px-2 py-1 font-mono",children:a.name}),". Use the path builder only when analysis cannot infer a tenant path."]}),(0,s.jsxs)("div",{className:"max-w-md space-y-2",children:[(0,s.jsx)(ec.J,{htmlFor:"tenant-column",children:"Tenant identifier column"}),(0,s.jsxs)(el.l6,{value:T,onValueChange:R,children:[(0,s.jsx)(el.bq,{id:"tenant-column",children:(0,s.jsx)(el.yv,{placeholder:"Choose tenant identifier column"})}),(0,s.jsx)(el.gC,{children:a.columns.map(e=>(0,s.jsxs)(el.eb,{value:e.name,children:[e.name,e.primaryKey?" (primary key)":""]},e.name))})]}),!E&&(0,s.jsx)("p",{className:"text-sm text-amber-600 dark:text-amber-400",children:"This table has no declared primary key in the schema. Choose the column that identifies each tenant."})]})]}),I&&!W?(0,s.jsx)("div",{className:"flex min-h-[400px] items-center justify-center",children:(0,s.jsxs)("div",{className:"text-center",children:[(0,s.jsx)(l.A,{className:"mx-auto mb-4 h-8 w-8 animate-spin text-green-accent"}),(0,s.jsx)("h3",{className:"mb-2 text-lg font-semibold",children:"Analyzing Table"}),(0,s.jsx)("p",{className:"text-muted-foreground",children:"Generating tenant policies."})]})}):O&&!W?(0,s.jsxs)(eo.Zp,{className:"border-destructive/40",children:[(0,s.jsxs)(eo.aR,{children:[(0,s.jsxs)(eo.ZB,{className:"flex items-center gap-2 text-base text-destructive",children:[(0,s.jsx)(d.A,{className:"h-4 w-4"}),"Analysis failed"]}),(0,s.jsx)(eo.BT,{children:ep(O,"The selected table could not be analyzed for tenant policies.")})]}),(0,s.jsx)(eo.Wu,{children:(0,s.jsxs)(i.$,{variant:"outline",size:"sm",onClick:H,disabled:I||!a||!T,className:"gap-2",children:[I?(0,s.jsx)(l.A,{className:"h-4 w-4 animate-spin"}):(0,s.jsx)(o.A,{className:"h-4 w-4"}),"Try again"]})})]}):(0,s.jsxs)(es.Mp,{sensors:D,onDragStart:function(e){L(e.active.id)},onDragEnd:function(e){let{active:a,over:t}=e;if(L(null),!t)return;let s=t.id,r=U[s]??b[s]?.scope;r&&((e,a)=>{if("tenant"===a){let a=y[e];return eu(a)?j(t=>({...t,[e]:a})):A(e)}j(t=>({...t,[e]:{scope:a}}))})(a.id,r)},children:[(0,s.jsx)("div",{className:"grid grid-cols-1 gap-4 xl:grid-cols-3",children:[{scope:"tenant",label:"Tenant Data",description:"Rows are filtered to the active tenant.",icon:(0,s.jsx)(Y.A,{className:"h-4 w-4 text-green-600"}),items:q.tenant},{scope:"shared",label:"Shared Data",description:"Tables can be read without tenant filtering.",icon:(0,s.jsx)(ee.A,{className:"h-4 w-4 text-gray-600"}),items:q.shared},{scope:"excluded",label:"Blocked Data",description:"Tables cannot be queried in tenant mode.",icon:(0,s.jsx)(ea.A,{className:"h-4 w-4 text-red-600"}),items:q.excluded}
10].map(({scope:e,label:a,description:t,icon:r,items:n})=>(0,s.jsxs)(eo.Zp,{className:"flex flex-col",children:[(0,s.jsxs)(eo.aR,{children:[(0,s.jsxs)(eo.ZB,{className:"flex items-center gap-2 text-base",children:[r,a,(0,s.jsx)(ed.E,{variant:"secondary",className:"ml-auto",children:n.length})]}),(0,s.jsx)(eo.BT,{children:t})]}),(0,s.jsx)(eo.Wu,{className:"flex flex-1 flex-col",children:(0,s.jsx)(er.gB,{items:n.map(([e])=>e),id:J[e],children:(0,s.jsxs)(eN,{id:J[e],children:[n.map(([e,a])=>(0,s.jsx)(ey,{tableName:e,rule:a,onEditPath:A},e)),0===n.length&&(0,s.jsx)("div",{className:"flex flex-1 items-center justify-center rounded-lg border border-dashed text-sm text-muted-foreground",children:"Drop tables here"})]})})})]},e))}),(0,s.jsx)(es.Hd,{children:z&&G?(0,s.jsx)("div",{className:"rotate-1 opacity-95",children:(0,s.jsx)(ej,{tableName:z,rule:G})}):null})]}),(0,s.jsxs)("div",{className:"flex items-center gap-2 text-sm text-muted-foreground",children:[(0,s.jsx)(et.A,{className:"h-4 w-4"}),"Linked tables use the generated path before applying the tenant filter."]})]}),(0,s.jsxs)("div",{className:"fixed bottom-0 left-0 right-0 z-50 flex justify-end gap-3 border-t bg-muted/30 p-4 md:left-64",children:[(0,s.jsx)(i.$,{variant:"outline",onClick:t,children:"Cancel"}),(0,s.jsx)(i.$,{onClick:()=>X(a),disabled:w||I||!W,children:w?"Saving...":"Save Tenant Policies"})]}),(0,s.jsx)(ev,{tableName:$,open:!!$,schema:C,existingRule:$?b[$]:void 0,onCancel:()=>A(null),onSave:(e,a)=>{j(t=>({...t,[e]:a})),N(t=>({...t,[e]:a})),A(null)}})]})}function e_({workspaceId:e,dataSourceId:a,sourceType:t,showTenantButton:d=!1,onConfigSaved:o,initialTableName:c,selectedTableName:m,onSelectTableName:h}){let[x,u]=(0,r.useState)(null),[p,f]=(0,r.useState)(!!c),g=(0,r.useRef)(!1),b=(0,r.useRef)(!1),j=()=>{u(null),g.current=!1,b.current=!0,f(!1)};return(0,r.useEffect)(()=>{g.current=!1,b.current=!1,c||f(!1)},[c]),(0,r.useEffect)(()=>{!c||g.current||b.current||(g.current=!0,f(!0),(async()=>{try{let t=a?await (0,M.t6)(e,a):await (0,M.x9)(e);if(t.success){let e=K.B.parseSchemaResponse(t).tables.find(e=>e.name===c);e?u(e):(b.current=!0,(0,eh.toast)({title:"Saved tenant table not found",description:"Choose a table from the current schema and save the tenant policy again.",variant:"destructive"}))}}catch(e){b.current=!0,(0,eh.toast)({title:"Schema Load Failed",description:"Could not load the current schema. Try refreshing the workspace schema.",variant:"destructive"})}finally{f(!1)}})())},[c,e,a]),(0,s.jsxs)("div",{className:"flex flex-col h-full w-full overflow-hidden",children:[x&&(0,s.jsx)("div",{className:"flex items-center gap-2 p-4 flex-shrink-0",children:(0,s.jsxs)(i.$,{variant:"ghost",size:"sm",onClick:j,className:"gap-2",children:[(0,s.jsx)(n.A,{className:"h-4 w-4"}),"Back to Database Schema"]})}),(0,s.jsx)("div",{className:"flex-1 min-h-0 w-full overflow-hidden",children:p?(0,s.jsx)("div",{className:"flex items-center justify-center h-full",children:(0,s.jsxs)("div",{className:"text-center",children:[(0,s.jsx)(l.A,{className:"w-8 h-8 animate-spin text-green-accent mx-auto mb-4"}),(0,s.jsx)("p",{className:"text-muted-foreground",children:"Loading table configuration..."})]})}):x?(0,s.jsx)(ek,{workspaceId:e,selectedTable:x,onClose:j,isOpen:!0,onConfigure:e=>{},onSuccess:()=>{o&&o()},shouldFetchConfig:!!c&&c===x.name&&!b.current}):(0,s.jsx)(J,{workspaceId:e,dataSourceId:a,sourceType:t,selectTenantTable:d?e=>{u(e)}:void 0,selectedTableName:m,onSelectTableName:h})})]})}},44846:(e,a,t)=>{t.d(a,{d:()=>i});var s=t(95155),r=t(12115),n=t(27599),l=t(57497);let i=r.forwardRef(({className:e,...a},t)=>(0,s.jsx)(n.bL,{className:(0,l.cn)("peer inline-flex h-5 w-9 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=unchecked]:bg-input",e),...a,ref:t,children:(0,s.jsx)(n.zi,{className:(0,l.cn)("pointer-events-none block h-4 w-4 roun
10ded-full bg-background shadow-lg ring-0 transition-transform data-[state=checked]:translate-x-4 data-[state=unchecked]:translate-x-0")})}));i.displayName=n.bL.displayName},76619:(e,a,t)=>{t.d(a,{B:()=>s});class s{static parseSchemaResponse(e){let{workspace_id:a,schema:t,cached:s=!1,last_refreshed:r}=e.data,n=[];if("string"==typeof t)n=this.parseSchemaString(t);else if(t&&"object"==typeof t){let e=t.schema||t;n=this.normalizeStructuredSchema(e)}return{workspaceId:a,tables:n,totalTables:n.length,totalColumns:n.reduce((e,a)=>e+a.columns.length,0),totalForeignKeys:n.reduce((e,a)=>e+a.foreignKeyRelationships.length,0),cached:s,lastRefreshed:r}}static normalizeStructuredSchema(e){let a=e.schema||e;return a.database_type?.toLowerCase(),(a.tables||a.collections||[]).map(e=>{let a=(e.fields||e.columns||[]).map(a=>{let t=a.field_name||a.name,s=a.data_type||a.type,r=a.array||!1,n=a.nested||!1,l=s;return r&&(l=`${s}[]`),n&&!t.includes(".")&&(l=`${l} (nested)`),{name:t,type:l,nullable:a.nullable??!0,primaryKey:(e.primary_keys||[]).includes(t),foreignKey:!1,defaultValue:a.default??null}}),t=(e.foreign_keys||[]).map(e=>({name:e.name||`fk_${e.constrained_columns?.[0]||"unknown"}`,constrained_columns:e.constrained_columns||[],referred_schema:e.referred_schema||null,referred_table:e.referred_table,referred_columns:e.referred_columns||[],options:e.options||{},comment:e.comment||null}));return t.forEach(e=>{e.constrained_columns.forEach(e=>{let t=a.find(a=>a.name===e);t&&(t.foreignKey=!0)})}),{name:e.name,columns:a,foreignKeyRelationships:t,sampleRecords:e.sample_data||e.sampleRecords||[],rowCount:e.row_count??e.rowCount??void 0,description:e.description??void 0}})}static parseSchemaString(e){let a=[];for(let t of this.splitIntoSections(e)){let e=this.parseTableSection(t);e&&a.push(e)}return a}static splitIntoSections(e){return e.split(/(?=Table \w+:)/).filter(e=>e.trim().startsWith("Table "))}static parseTableSection(e){let a=e.split("\n").map(e=>e.trim()).filter(e=>e);if(0===a.length)return null;let t=a[0].match(/^Table (\w+):$/);if(!t)return null;let s={name:t[1],columns:[],foreignKeyRelationships:[],sampleRecords:[]},r="columns";for(let e=1;e<a.length;e++){let t=a[e];if(t.includes("Foreign Key Relationships:")){r="foreignKeys";continue}if(t.includes("Sample Records:")){r="sampleRecords";continue}switch(r){case"columns":let n=this.parseColumnLine(t);n&&s.columns.push(n);break;case"foreignKeys":let l=this.parseForeignKeyLine(t);l&&s.foreignKeyRelationships.push(l);break;case"sampleRecords":let i=this.parseSampleRecordLine(t);i&&s.sampleRecords.push(i)}}return s}static parseColumnLine(e){let a=e.match(/^- (\w+) \(([^)]+)\)(?: \[([^\]]+)\])?$/);if(!a)return null;let[,t,s,r=""]=a;return{name:t,type:s.trim(),nullable:s.includes("NULL")||!s.includes("NOT NULL"),primaryKey:"id"===t||s.includes("PRIMARY KEY")||r.toLowerCase().includes("primary_key"),foreignKey:!1}}static parseForeignKeyLine(e){let a=e.match(/^- (\w+) â (\w+)\.(\w+)$/);if(!a)return null;let[,t,s,r]=a;return{name:`fk_${t}_${s}`,constrained_columns:[t],referred_schema:null,referred_table:s,referred_columns:[r],options:{},comment:null}}static parseSampleRecordLine(e){let a=e.match(/^\d+\.\s*(.+)$/);if(!a)return null;try{let e=a[1];return this.parseRecordString(e)}catch(e){return null}}static parseRecordString(e){let a={},t=e.slice(1,-1);for(let e of this.splitRecordPairs(t)){let t=e.indexOf(":");if(t>0){let s=e.substring(0,t).trim().replace(/['"]/g,""),r=e.substring(t+1).trim();a[s]=this.parseValue(r)}}return a}static splitRecordPairs(e){let a=[],t="",s=0,r=!1,n="";for(let l=0;l<e.length;l++){let i=e[l];if(r)i===n&&"\\"!==e[l-1]&&(r=!1);else if("'"===i||'"'===i)r=!0,n=i;else if("{"===i||"["===i)s++;else if("}"===i||"]"===i)s--;else if(","===i&&0===s){a.push(t.trim()),t="";continue}t+=i}return t.trim()&&a.push(t.trim()),a}static parseValue(e){let a=e.trim();if("NULL"===a||"'NULL'"===a||'"NULL"'===a)return null;if(a.startsWith("'")&&a.endsWith("'")||a.startsWith('"')&&a.endsWith('"'))return a.slice(1,-1);if(/^\d+(\.\d+)?$/.test(a))return a.includes(".")?parseFloat(a):parseInt(a,10);if("True"===a)return!0;
10if("False"===a)return!1;if(a.includes("datetime.datetime")){let e=a.match(/datetime\.datetime\(([^)]+)\)/);if(e)return e[1]}if(a.includes("Decimal(")){let e=a.match(/Decimal\('([^']+)'\)/);return e?parseFloat(e[1]):0}if(a.startsWith("[")&&a.endsWith("]"))try{let e=a.slice(1,-1);if(e.trim())return e.split(",").map(e=>this.parseValue(e.trim()));return[]}catch{}return a}static analyzeSchema(e){let a=e.tables.map(e=>e.name),t={},s={},r={};for(let a of e.tables){t[a.name]=a.columns.length,s[a.name]=a.foreignKeyRelationships.length;let e=new Set;for(let t of a.columns)e.add(t.type);r[a.name]=Array.from(e)}let n=a.filter(a=>!e.tables.some(e=>e.foreignKeyRelationships.some(e=>e.referred_table===a))),l=a.filter(a=>{let t=e.tables.find(e=>e.name===a);if(!t)return!1;let s=t.foreignKeyRelationships.length,r=t.columns.length;return s>0&&s/r>.5});return{tableNames:a,columnCounts:t,relationshipCounts:s,dataTypes:r,primaryTables:n,junctionTables:l}}static getRelationshipGraph(e){let a={};for(let t of e.tables)for(let e of(a[t.name]||(a[t.name]=[]),t.foreignKeyRelationships))a[e.referred_table]||(a[e.referred_table]=[]),a[e.referred_table].push(t.name);return a}static findTablesByColumnType(e,a){return e.tables.filter(e=>e.columns.some(e=>e.type.includes(a))).map(e=>e.name)}static getTableColumns(e,a){let t=e.tables.find(e=>e.name===a);return t?t.columns:[]}}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.