import{r as e}from"./rolldown-runtime-S-ySWqyJ.js";import{An as t,Fa as n,Gt as r,Ht as i,Mi as a,Sn as o,Ta as s,Tn as c,Un as l,Vn as u,Yt as d,Z as f,dn as p,en as m,in as h,ka as g,ln as _,oi as v,qi as y,sn as b}from"./index-gtiNiwC2.js";var x=e(n(),1),S=e(g(),1);function C({open:e,onOpenChange:n,report:g,onSuccess:C}){let[w,T]=(0,x.useState)(`regular`),[E,D]=(0,x.useState)({}),[O,k]=(0,x.useState)(!1),[A,j]=(0,x.useState)(``);(0,x.useEffect)(()=>{if(e&&g.sql_parameters){let e={};g.sql_parameters.forEach(t=>{e[t.name]=t.default?.toString()||``}),D(e)}},[e,g.sql_parameters]);let M=async()=>{k(!0),j(``);try{let e={},t=[];if(g.sql_parameters?.forEach(n=>{let r=E[n.name];if(n.required&&(!r||r.trim()===``)){t.push(n.name);return}if(!(!r||r.trim()===``))if(n.type===`number`){let t=parseFloat(r);if(isNaN(t))throw Error(`Invalid number value for parameter "${n.name}"`);e[n.name]=t}else n.type===`boolean`?e[n.name]=r===`true`||r===`1`:e[n.name]=r}),t.length>0){j(`Please provide required parameters: ${t.join(`, `)}`),k(!1);return}let r=await f.createView(g.id,{parameter_defaults:e,view_type:w});r.success?(s.success(`View "${r.view_name}" has been created successfully.`),C?.(),n(!1)):j(r.error||`Failed to create view`)}catch(e){let t=e.response?.data?.error||e.message||`Failed to create view`;j(t),s.error(t)}finally{k(!1)}},N=g.sql_parameters&&g.sql_parameters.length>0;return(0,S.jsx)(u,{open:e,onOpenChange:n,children:(0,S.jsxs)(h,{className:`max-w-2xl`,children:[(0,S.jsxs)(p,{children:[(0,S.jsxs)(b,{className:`flex items-center gap-2`,children:[(0,S.jsx)(a,{className:`h-5 w-5`}),`Create Database View`]}),(0,S.jsx)(r,{children:`Create a PostgreSQL view from this report's SQL query. The view will be stored in the database and can be used by other tools.`})]}),(0,S.jsxs)(`div`,{className:`space-y-6 py-4`,children:[(0,S.jsxs)(t,{children:[(0,S.jsx)(v,{className:`h-4 w-4`}),(0,S.jsx)(l,{children:`The database view will use the exact same SQL query and field structure as this report. Once created, it will auto-update when you modify the report SQL.`})]}),(0,S.jsxs)(`div`,{className:`space-y-3`,children:[(0,S.jsx)(o,{children:`View Type`}),(0,S.jsxs)(m,{value:w,onValueChange:e=>T(e),children:[(0,S.jsxs)(`div`,{className:`flex items-start space-x-3 space-y-0`,children:[(0,S.jsx)(i,{value:`regular`,id:`regular`}),(0,S.jsxs)(`div`,{className:`flex-1`,children:[(0,S.jsx)(o,{htmlFor:`regular`,className:`font-medium cursor-pointer`,children:`Regular View`}),(0,S.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:`Always shows current data from base tables. Recommended for most cases.`})]})]}),(0,S.jsxs)(`div`,{className:`flex items-start space-x-3 space-y-0`,children:[(0,S.jsx)(i,{value:`materialized`,id:`materialized`}),(0,S.jsxs)(`div`,{className:`flex-1`,children:[(0,S.jsx)(o,{htmlFor:`materialized`,className:`font-medium cursor-pointer`,children:`Materialized View`}),(0,S.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:`Snapshot of data at creation time. Faster queries but requires manual refresh.`})]})]})]})]}),N&&(0,S.jsxs)(`div`,{className:`space-y-4`,children:[(0,S.jsxs)(`div`,{children:[(0,S.jsx)(o,{className:`text-base`,children:`Parameter Default Values`}),(0,S.jsx)(`p`,{className:`text-sm text-muted-foreground mt-1`,children:`Provide default values for query parameters. These will be used when querying the view.`})]}),(0,S.jsx)(`div`,{className:`space-y-3`,children:g.sql_parameters.map(e=>(0,S.jsxs)(`div`,{className:`space-y-2`,children:[(0,S.jsxs)(o,{htmlFor:e.name,className:`text-sm font-medium`,children:[e.name,e.required&&(0,S.jsx)(`span`,{className:`text-destructive ml-1`,children:`*`})]}),(0,S.jsx)(c,{id:e.name,type:e.type===`date`?`date`:e.type===`number`?`number`:`text`,value:E[e.name]||``,onChange:t=>D({...E,[e.name]:t.target.value}),placeholder:`Enter ${e.name}`,required:e.required})]},e.name))})]}),A&&(0,S.jsxs)(t,{variant:`destructive`,children:[(0,S.jsx)(y,{className:`h-4 w-4`}),(0,S.jsx)(l,{children:A})]})]}),(0,S.jsxs)(d,{children:[(0,S.jsx)(_,{variant:`outline`,onClick:()=>n(!1),children:`Cancel`}),(0,S.jsx)(_,{onClick:M,disabled:O,children:O?`Creating...`:`Create View`})]})]})})}export{C as t};