import{o as e}from"./rolldown-runtime-FhOqtrmT.js";import{b as t,x as n}from"./vendor-markdown-0Mxgxy0L.js";import{i as r,t as i}from"./api-client-bbJLzRVE.js";import{n as a}from"./settings-store-B5e8CNie.js";import"./vendor-mermaid-v5g7E-EQ.js";import{D as o,mt as s}from"./index-48KAFlD7.js";import{n as c,t as l}from"./use-monaco-theme-DAPyqocR.js";var u=e(n(),1),d=t();function f(e){return{js:`javascript`,jsx:`javascript`,ts:`typescript`,tsx:`typescript`,py:`python`,html:`html`,css:`css`,scss:`scss`,json:`json`,md:`markdown`,mdx:`markdown`,yaml:`yaml`,yml:`yaml`,sh:`shell`,bash:`shell`,go:`go`,rs:`rust`,java:`java`,rb:`ruby`,php:`php`,swift:`swift`,sql:`sql`,xml:`xml`,toml:`toml`}[e.split(`.`).pop()?.toLowerCase()??``]??`plaintext`}function p(e){let t=e.split(`
`),n=[],r=0,i=0;for(;r<t.length;){let e=t[r];if(e.startsWith(`<<<<<<<`)){let a=r,o=e.substring(7).trim(),s=[];for(r++;r<t.length&&!t[r].startsWith(`=======`);)s.push(t[r]),r++;if(r>=t.length)break;let c=r,l=[];for(r++;r<t.length&&!t[r].startsWith(`>>>>>>>`);)l.push(t[r]),r++;if(r>=t.length)break;let u=t[r].substring(7).trim();n.push({id:i++,startLine:a+1,separatorLine:c+1,endLine:r+1,currentContent:s.join(`
`),incomingContent:l.join(`
`),currentLabel:o,incomingLabel:u})}r++}return n}function m({metadata:e}){let t=e?.filePath,n=e?.projectName,[m,g]=(0,u.useState)(null),[_,v]=(0,u.useState)(!0),[y,b]=(0,u.useState)(null),[x,S]=(0,u.useState)(0),C=(0,u.useRef)(null),w=(0,u.useRef)(null),T=(0,u.useRef)([]),E=(0,u.useRef)(null),{wordWrap:D}=a(o(e=>({wordWrap:e.wordWrap}))),O=l(),k=(0,u.useRef)(null),[A,j]=(0,u.useState)();(0,u.useEffect)(()=>{let e=k.current;if(!e)return;let t=new ResizeObserver(([e])=>{e&&j(Math.floor(e.contentRect.height))});return t.observe(e),()=>t.disconnect()},[]),(0,u.useEffect)(()=>{!t||!n||(v(!0),i.get(`${r(n)}/files/read?path=${encodeURIComponent(t)}`).then(e=>{g(e.content),v(!1)}).catch(e=>{b(e.message||`Failed to load file`),v(!1)}))},[t,n]);let M=(0,u.useCallback)(()=>{let e=C.current,t=w.current;if(!e||!t)return;let n=p(e.getModel()?.getValue()||``);S(n.length);for(let t of T.current)e.removeContentWidget(t);if(T.current=[],E.current&&E.current.clear(),n.length===0)return;let r=[];for(let e of n)r.push({range:new t.Range(e.startLine,1,e.startLine,1),options:{isWholeLine:!0,className:`conflict-marker-line`,glyphMarginClassName:`conflict-glyph-current`}}),r.push({range:new t.Range(e.separatorLine,1,e.separatorLine,1),options:{isWholeLine:!0,className:`conflict-marker-line`}}),r.push({range:new t.Range(e.endLine,1,e.endLine,1),options:{isWholeLine:!0,className:`conflict-marker-line`,glyphMarginClassName:`conflict-glyph-incoming`}}),e.separatorLine-e.startLine>1&&r.push({range:new t.Range(e.startLine+1,1,e.separatorLine-1,1),options:{isWholeLine:!0,className:`conflict-current-content`}}),e.endLine-e.separatorLine>1&&r.push({range:new t.Range(e.separatorLine+1,1,e.endLine-1,1),options:{isWholeLine:!0,className:`conflict-incoming-content`}});E.current=e.createDecorationsCollection(r);for(let r of n){let n=`conflict-widget-${r.id}`,i=document.createElement(`div`);i.className=`conflict-actions`,i.innerHTML=`<span class="conflict-label">Current Change (${h(r.currentLabel||`HEAD`)})</span><button class="conflict-btn conflict-btn-current" data-action="current">Accept Current</button><button class="conflict-btn conflict-btn-incoming" data-action="incoming">Accept Incoming</button><button class="conflict-btn conflict-btn-both" data-action="both">Accept Both</button>`,i.addEventListener(`click`,e=>{let t=e.target.closest(`[data-action]`);if(!t)return;let n=t.getAttribute(`data-action`);N(r.id,n)});let a={getId:()=>n,getDomNode:()=>i,getPosition:()=>({position:{lineNumber:r.startLine,column:1},preference:[t.editor.ContentWidgetPositionPreference.ABOVE]})};e.addContentWidget(a),T.current.push(a)}},[]),N=(0,u.useCallback)((e,t)=>{let n=C.current,r=w.current;if(!n||!r)return;let i=n.getModel();if(!i)return;let a=p(i.getValue()).find(t=>t.id===e);if(!a)return;let o;switch(t){case`current`:o=a.currentContent;break;case`incoming`:o=a.incomingContent;break;case`both`:o=a.currentContent+`
`+a.incomingContent;break}let s=new r.Range(a.startLine,1,a.endLine+1,1);i.pushEditOperations([],[{range:s,text:o+`
`}],()=>null),P(i.getValue()),setTimeout(()=>M(),50)},[M]),P=(0,u.useCallback)(async e=>{if(!(!t||!n))try{await i.put(`${r(n)}/files/write`,{path:t,content:e})}catch(e){console.error(`[conflict-editor] save failed:`,e)}},[t,n]),F=(e,t)=>{C.current=e,w.current=t;let n=e.getDomNode()?.ownerDocument??document;if(!n.getElementById(`conflict-editor-styles`)){let e=n.createElement(`style`);e.id=`conflict-editor-styles`,e.textContent=`
        .conflict-current-content { background: rgba(34, 197, 94, 0.1) !important; }
        .conflict-incoming-content { background: rgba(59, 130, 246, 0.1) !important; }
        .conflict-marker-line { background: rgba(100, 100, 100, 0.15) !important; font-style: italic; }
        .conflict-glyph-current { background: #22c55e !important; }
        .conflict-glyph-incoming { background: #3b82f6 !important; }
        .conflict-actions { display: flex; gap: 8px; align-items: center; padding: 2px 0; font-size: 12px; font-family: system-ui; }
        .conflict-label { color: #22c55e; font-weight: 600; margin-right: 8px; }
        .conflict-btn { padding: 1px 8px; border-radius: 3px; border: none; cursor: pointer; font-size: 11px; opacity: 0.9; }
        .conflict-btn:hover { opacity: 1; }
        .conflict-btn-current { color: #22c55e; background: rgba(34, 197, 94, 0.15); }
        .conflict-btn-incoming { color: #3b82f6; background: rgba(59, 130, 246, 0.15); }
        .conflict-btn-both { color: #a855f7; background: rgba(168, 85, 247, 0.15); }
      `,n.head?.appendChild(e)}M()},I=t?.split(/[\\/]/).pop()??`unknown`,L=f(I);return _?(0,d.jsx)(`div`,{className:`flex items-center justify-center h-full`,children:(0,d.jsx)(s,{className:`size-6 animate-spin text-primary`})}):y?(0,d.jsx)(`div`,{className:`flex items-center justify-center h-full text-destructive`,children:y}):(0,d.jsxs)(`div`,{className:`h-full w-full flex flex-col`,children:[(0,d.jsxs)(`div`,{className:`flex items-center gap-2 px-3 py-1.5 text-xs border-b border-border bg-muted/50 flex-shrink-0`,children:[(0,d.jsx)(`span`,{className:`font-medium`,children:I}),(0,d.jsx)(`span`,{className:`text-muted-foreground`,children:`—`}),x>0?(0,d.jsxs)(`span`,{className:`text-destructive font-medium`,children:[x,` conflict`,x===1?``:`s`,` remaining`]}):(0,d.jsx)(`span`,{className:`text-green-500 font-medium`,children:`All conflicts resolved`})]}),(0,d.jsx)(`div`,{ref:k,className:`flex-1 min-h-0`,children:m!==null&&A&&(0,d.jsx)(c,{height:A,language:L,value:m,onMount:F,theme:O,options:{fontSize:13,fontFamily:`Menlo, Monaco, Consolas, monospace`,wordWrap:D?`on`:`off`,glyphMargin:!0,readOnly:!1,automaticLayout:!0,scrollBeyondLastLine:!1,minimap:{enabled:!1}}})})]})}function h(e){return e.replace(/&/g,`&amp;`).replace(/</g,`&lt;`).replace(/>/g,`&gt;`).replace(/"/g,`&quot;`)}export{m as ConflictEditor};