import{h as j,i as v,r as s,j as a}from"./vendor-react-9M-xu2bR.js";import{DashboardEditor as w}from"./index-Dv78M4Xq.js";import{b as h}from"./vendor-ui-utils-3tkeUVaX.js";import{a as k,b as D}from"./index-CTBPkbPG.js";import{A as E}from"./vendor-icons-ndICq8DC.js";import"./framework-BM176iet.js";import"./vendor-objectstack-YKtcdalX.js";import"./vendor-zod-DxQVy2_o.js";import"./ui-components-BxvihT61.js";import"./vendor-radix-C6qchjxv.js";import"./vendor-i18n-E3-WitRV.js";import"./vendor-charts-BLNOCZWl.js";import"./infrastructure-n1j_TN6w.js";import"./plugins-core-qwgXbAqa.js";import"./vendor-dndkit-CX1oDZqy.js";import"./plugins-views-CveFN6PV.js";import"./data-adapter-rvE3xy2B.js";import"./ui-layout-C6KWQtRO.js";function z(){const b=j(),{dashboardName:t}=v(),d=k(),{dashboards:f,refresh:u}=D(),r=f?.find(e=>e.name===t),[i,x]=s.useState(()=>r||{type:"dashboard",name:t??"",title:t??"",columns:2,widgets:[]}),p=s.useRef(i);p.current=i;const n=s.useCallback(async e=>{try{if(d)return await d.update("sys_dashboard",t,e),u().catch(()=>{}),!0}catch{}return!1},[d,t,u]),l=s.useCallback(async e=>{x(e),await n(e)},[n]);s.useEffect(()=>{const e=o=>{(o.ctrlKey||o.metaKey)&&o.key==="s"&&(o.preventDefault(),n(p.current).then(c=>{c&&h.success("Dashboard saved")}))};return window.addEventListener("keydown",e),()=>window.removeEventListener("keydown",e)},[n]);const g=s.useCallback(e=>{const o=new Blob([JSON.stringify(e,null,2)],{type:"application/json"}),c=URL.createObjectURL(o),m=document.createElement("a");m.href=c,m.download=`${t||"dashboard"}.json`,m.click(),URL.revokeObjectURL(c),h.success("Dashboard schema exported")},[t]),y=s.useCallback(e=>{h.success("Dashboard schema imported"),l(e)},[l]);return r?a.jsxs("div",{className:"h-full flex flex-col","data-testid":"dashboard-design-page",children:[a.jsxs("div",{className:"flex items-center gap-3 border-b px-4 py-3 sm:px-6",children:[a.jsx("button",{type:"button",onClick:()=>b(-1),className:"rounded p-1.5 text-muted-foreground hover:text-foreground","aria-label":"Go back","data-testid":"dashboard-design-back",children:a.jsx(E,{className:"h-4 w-4"})}),a.jsxs("h1",{className:"text-lg font-semibold tracking-tight truncate",children:["Edit Dashboard: ",r.label||r.title||t]})]}),a.jsx("div",{className:"flex-1 overflow-auto p-4 sm:p-6",children:a.jsx(w,{schema:i,onChange:l,onExport:g,onImport:y})})]}):a.jsxs("div",{className:"h-full flex items-center justify-center text-muted-foreground",children:['Dashboard "',t,'" not found']})}export{z as DashboardDesignPage};
