import'../../chunk-QYUPLKLP.mjs';import {PluginKey,Plugin}from'prosemirror-state';import {DecorationSet,Decoration}from'prosemirror-view';var S=/\{([#/^@]?)([a-zA-Z_][a-zA-Z0-9_]*(?:\.[a-zA-Z_][a-zA-Z0-9_]*)*)\}/g,a=new PluginKey("template");function v(e,t,n){return `${e}:${t}:${n}`}function x(e){let t=[];e.descendants((d,s)=>(d.isText&&d.text&&t.push({text:d.text,pos:s}),true));let n="",o=[];for(let d of t){for(let s=0;s<d.text.length;s++)o.push(d.pos+s);n+=d.text;}let i=[],r=[],l=new Map,g;for(S.lastIndex=0;(g=S.exec(n))!==null;){let[d,s,p]=g,I=o[g.index],b=o[g.index+d.length-1]+1,c;s==="#"?c="sectionStart":s==="/"?c="sectionEnd":s==="^"?c="invertedStart":s==="@"?c="raw":c="variable";let T=`${c}:${p}`,h=l.get(T)??0;l.set(T,h+1);let f={id:v(c,p,h),type:c,name:p,rawTag:d,from:I,to:b};if(c==="sectionStart"||c==="invertedStart")f.nestedVars=[],r.push(f);else if(c==="sectionEnd"){for(let u=r.length-1;u>=0;u--)if(r[u].name===p){r.splice(u,1);break}}else c==="variable"&&r.length>0&&(r[r.length-1].nestedVars?.push(p),f.insideSection=true);i.push(f);}return i}function A(e){switch(e){case "sectionStart":case "sectionEnd":return "#3b82f6";case "invertedStart":return "#8b5cf6";case "raw":return "#ef4444";default:return "#f59e0b"}}function m(e,t,n,o){let i=[];for(let r of t){let l=r.id===n,g=r.id===o,d=A(r.type),s=["docx-template-tag"];l&&s.push("hovered"),g&&s.push("selected"),i.push(Decoration.inline(r.from,r.to,{class:s.join(" "),"data-tag-id":r.id,style:`background-color: ${d}22; border-radius: 2px;`},{noOverlay:true}));}return DecorationSet.create(e,i)}function V(e,t){if(e.length!==t.length)return  false;for(let n=0;n<e.length;n++)if(e[n].id!==t[n].id)return  false;return  true}function w(){return new Plugin({key:a,state:{init(e,t){let n=x(t.doc);return {tags:n,decorations:m(t.doc,n)}},apply(e,t,n,o){if(e.docChanged){let r=x(o.doc),l=V(t.tags,r);return {tags:r,decorations:l?t.decorations.map(e.mapping,e.doc):m(o.doc,r,t.hoveredId,t.selectedId),hoveredId:t.hoveredId,selectedId:t.selectedId}}let i=e.getMeta(a);if(i){let r=i.hoveredId??t.hoveredId,l=i.selectedId??t.selectedId;return {...t,hoveredId:r,selectedId:l,decorations:m(o.doc,t.tags,r,l)}}return {...t,decorations:t.decorations.map(e.mapping,e.doc)}}},props:{decorations(e){return a.getState(e)?.decorations??DecorationSet.empty},handleClick(e,t){let o=(a.getState(e.state)?.tags??[]).find(r=>t>=r.from&&t<=r.to);return o?(e.dispatch(e.state.tr.setMeta(a,{selectedId:o.id})),true):(a.getState(e.state)?.selectedId&&e.dispatch(e.state.tr.setMeta(a,{selectedId:void 0})),false)},handleDOMEvents:{mouseover(e,t){let o=t.target.closest?.("[data-tag-id]")?.getAttribute("data-tag-id")||void 0,i=a.getState(e.state)?.hoveredId;return o!==i&&e.dispatch(e.state.tr.setMeta(a,{hoveredId:o})),false},mouseout(e,t){return t.relatedTarget?.closest?.("[data-tag-id]")||a.getState(e.state)?.hoveredId&&e.dispatch(e.state.tr.setMeta(a,{hoveredId:void 0})),false}}}})}function D(e){return a.getState(e)?.tags??[]}function $(e,t){e.dispatch(e.state.tr.setMeta(a,{hoveredId:t}));}function H(e,t){e.dispatch(e.state.tr.setMeta(a,{selectedId:t}));}var N=`
.docx-template-tag {
  cursor: pointer;
  transition: background-color 0.1s;
}

.docx-template-tag:hover,
.docx-template-tag.hovered {
  filter: brightness(0.95);
}

.docx-template-tag.selected {
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.5);
}
`;export{N as TEMPLATE_DECORATION_STYLES,w as createTemplatePlugin,D as getTemplateTags,$ as setHoveredElement,H as setSelectedElement,a as templatePluginKey};