import{a as e,n as t,r as n}from"./useStore-CusP06SD.js";import{L as r}from"./sidebar-y9WJu7Jw.js";import{i}from"./useBaseQuery-BvrWhgOK.js";import{t as a}from"./Page-C7KClZ28.js";import{t as o}from"./button-1cIaa9Lf.js";import{f as s,t as c,u as l}from"./index-DinSQfHc.js";var u=e(n()),d=t();function f(){let[e,t]=(0,u.useState)(``),[n,f]=(0,u.useState)(!0),[p,m]=(0,u.useState)(``),h=(0,u.useRef)(null),g=r(),_=i(),{data:v,isLoading:y}=s(g.settings.get.queryOptions()),b=l(g.settings.replace.mutationOptions({onSuccess:()=>{c.success(`Settings saved successfully!`,{description:`Your changes will take effect on the next restart.`}),_.invalidateQueries({queryKey:[`settings.get`]})},onError:e=>{let t=e instanceof Error?e.message:`An unknown error occurred`;c.error(`Failed to save settings`,{description:t})}})),x=()=>{let e=h.current;if(e){let t=window.pageYOffset||document.documentElement.scrollTop;e.style.height=`auto`,e.style.height=`${e.scrollHeight}px`,requestAnimationFrame(()=>{window.scrollTo(0,t)})}};(0,u.useEffect)(()=>{v?.config&&t(JSON.stringify(v.config,null,2))},[v]),(0,u.useEffect)(()=>{x()},[e]);let S=e=>{try{return JSON.parse(e),f(!0),m(``),!0}catch(e){return f(!1),m(e instanceof Error?e.message:`Invalid JSON`),!1}},C=e=>{t(e),S(e),setTimeout(x,0)};return y?(0,d.jsx)(a,{breadcrumbs:[`Diagnostics`,`Debug`],children:(0,d.jsx)(`div`,{className:`flex items-center justify-center p-8`,children:(0,d.jsx)(`div`,{children:`Loading settings...`})})}):(0,d.jsx)(a,{breadcrumbs:[`Diagnostics`,`Debug`],actions:(0,d.jsxs)(`div`,{className:`flex gap-2`,children:[(0,d.jsx)(o,{variant:`outline`,onClick:()=>{v?.config&&(t(JSON.stringify(v.config,null,2)),f(!0),m(``),setTimeout(x,0))},disabled:b.isPending,children:`Reset`}),(0,d.jsx)(o,{onClick:()=>{if(!n){c.error(`Cannot save invalid JSON`);return}try{let t=JSON.parse(e);b.mutate(t)}catch{c.error(`Failed to parse JSON`)}},disabled:!n||b.isPending,children:b.isPending?`Saving...`:`Save`})]}),children:(0,d.jsxs)(`div`,{className:`space-y-4`,children:[(0,d.jsxs)(`div`,{children:[(0,d.jsx)(`h1`,{className:`text-2xl font-bold`,children:`Debug Settings`}),(0,d.jsx)(`p`,{className:`text-muted-foreground`,children:`Raw JSON editor for debugging configuration issues`})]}),(0,d.jsx)(`div`,{className:`relative`,children:(0,d.jsx)(`textarea`,{ref:h,value:e,onChange:e=>C(e.target.value),className:`min-h-96 w-full resize-none overflow-hidden rounded-md border p-3 font-mono text-sm ${n?`border-gray-300 dark:border-gray-600`:`border-red-500 dark:border-red-400`} bg-white text-gray-900 dark:bg-gray-900 dark:text-gray-100`,spellCheck:!1,placeholder:`Loading settings...`})}),!n&&(0,d.jsxs)(`div`,{className:`rounded-md bg-red-50 p-3 text-sm text-red-700 dark:bg-red-900/20 dark:text-red-400`,children:[(0,d.jsx)(`strong`,{children:`JSON Error:`}),` `,p]})]})})}export{f as component};