import{r as l,j as e}from"./react-D_B_5QVd.js";import{e as b,C as N,M as v}from"./index-BdwitBZy.js";import"./jszip-C9ownNzX.js";import"./react-dom-EAO2-NBm.js";import"./scheduler-DYLXRpC5.js";import"./@google-4tyssLU_.js";import"./marked-CesSW9Du.js";import"./idb-Dob3nYDb.js";const f=({text:r="AI is analyzing..."})=>e.jsxs("div",{className:"flex items-center justify-center space-x-2",children:[e.jsx("div",{className:"w-2 h-2 rounded-full bg-primary animate-pulse",style:{animationDelay:"0s"}}),e.jsx("div",{className:"w-2 h-2 rounded-full bg-primary animate-pulse",style:{animationDelay:"0.2s"}}),e.jsx("div",{className:"w-2 h-2 rounded-full bg-primary animate-pulse",style:{animationDelay:"0.4s"}}),e.jsx("span",{className:"text-text-secondary ml-2",children:r})]}),w=`const bubbleSort = (arr) => {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
};`,C=r=>r.replace(/&/g,"&").replace(//g,">").replace(/(const|let|var|function|return|if|for|=>)/g,'$1').replace(/(\'|")(.*?)(\'|")/g,'$1$2$3').replace(/(\/\/.*)/g,'$1').replace(/(\{|\}|\(|\)|\[|\])/g,'$1'),M=({initialCode:r})=>{const[i,m]=l.useState(r||w),[a,x]=l.useState(null),[n,p]=l.useState(!1),[o,c]=l.useState(""),[u,h]=l.useState("summary"),d=l.useCallback(async s=>{if(!s.trim()){c("Please enter some code to explain.");return}p(!0),c(""),x(null),h("summary");try{const t=await b(s);x(t)}catch(t){const j=t instanceof Error?t.message:"An unknown error occurred.";c(`Failed to get explanation: ${j}`)}finally{p(!1)}},[]);l.useEffect(()=>{r&&(m(r),d(r))},[r,d]);const y=l.useMemo(()=>C(i),[i]),g=()=>{if(!a)return null;switch(u){case"summary":return e.jsx(v,{content:a.summary});case"lineByLine":return e.jsx("div",{className:"space-y-3",children:a.lineByLine.map((s,t)=>e.jsxs("div",{className:"p-3 bg-background rounded-md border border-border",children:[e.jsxs("p",{className:"font-mono text-xs text-primary mb-1",children:["Lines: ",s.lines]}),e.jsx("p",{className:"text-sm",children:s.explanation})]},t))});case"complexity":return e.jsxs("div",{children:[e.jsxs("p",{children:[e.jsx("strong",{children:"Time Complexity:"})," ",e.jsx("span",{className:"font-mono text-amber-600",children:a.complexity.time})]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Space Complexity:"})," ",e.jsx("span",{className:"font-mono text-amber-600",children:a.complexity.space})]})]});case"suggestions":return e.jsx("ul",{className:"list-disc list-inside space-y-2",children:a.suggestions.map((s,t)=>e.jsx("li",{children:s},t))})}};return e.jsxs("div",{className:"h-full flex flex-col p-4 sm:p-6 lg:p-8 text-text-primary",children:[e.jsxs("header",{className:"mb-6",children:[e.jsxs("h1",{className:"text-3xl font-bold flex items-center",children:[e.jsx(N,{}),e.jsx("span",{className:"ml-3",children:"AI Code Explainer"})]}),e.jsx("p",{className:"text-text-secondary mt-1",children:"Get a detailed, structured analysis of any code snippet."})]}),e.jsxs("div",{className:"flex-grow flex flex-col gap-4 min-h-0",children:[e.jsxs("div",{className:"flex flex-col h-2/5 min-h-[200px]",children:[e.jsx("label",{htmlFor:"code-input",className:"text-sm font-medium text-text-secondary mb-2",children:"Your Code"}),e.jsxs("div",{className:"relative flex-grow",children:[e.jsx("textarea",{id:"code-input",value:i,onChange:s=>m(s.target.value),placeholder:"Paste your code here...",spellCheck:"false",className:"absolute inset-0 w-full h-full p-4 bg-transparent border border-border rounded-md resize-none font-mono text-sm text-transparent caret-primary focus:ring-2 focus:ring-primary focus:outline-none z-10"}),e.jsx("pre",{"aria-hidden":"true",className:"absolute inset-0 w-full h-full p-4 bg-gray-50 border border-transparent rounded-md font-mono text-sm pointer-events-none z-0 whitespace-pre-wrap",dangerouslySetInnerHTML:{__html:y+`
`}})]})]}),e.jsx("div",{className:"flex-shrink-0",children:e.jsx("button",{onClick:()=>d(i),disabled:n,className:"btn-primary w-full max-w-xs mx-auto flex items-center justify-center px-6 py-3",children:n?e.jsx(f,{text:"Analyzing..."}):"Analyze Code"})}),e.jsxs("div",{className:"flex flex-col flex-grow min-h-0",children:[e.jsx("label",{className:"text-sm font-medium text-text-secondary mb-2",children:"AI Analysis"}),e.jsxs("div",{className:"relative flex-grow flex flex-col bg-surface border border-border rounded-md overflow-hidden",children:[e.jsx("div",{className:"flex-shrink-0 flex border-b border-border",children:["summary","lineByLine","complexity","suggestions"].map(s=>e.jsx("button",{onClick:()=>h(s),disabled:!a,className:`px-4 py-2 text-sm font-medium capitalize transition-colors ${u===s?"bg-gray-100 text-primary":"text-text-secondary hover:bg-gray-50 disabled:text-gray-300"}`,children:s.replace(/([A-Z])/g," $1")},s))}),e.jsxs("div",{className:"p-4 flex-grow overflow-y-auto",children:[n&&e.jsx("div",{className:"flex items-center justify-center h-full",children:e.jsx(f,{})}),o&&e.jsx("p",{className:"text-red-500",children:o}),a&&!n&&g(),!n&&!a&&!o&&e.jsx("div",{className:"text-text-secondary h-full flex items-center justify-center",children:"The analysis will appear here."})]})]})]})]})]})};export{M as AiCodeExplainer};
//# sourceMappingURL=AiCodeExplainer-CLIi0ncg.js.map