var D=class{constructor(){this.plugins=new Map;this.commandHandlers=new Map;this.eventListeners=new Set;this.initialized=new Set;}register(e,t){let r=[];if(!e.id)return {success:false,error:"Plugin must have an id"};if(this.plugins.has(e.id))return {success:false,error:`Plugin '${e.id}' is already registered`};if(e.dependencies){for(let n of e.dependencies)if(!this.plugins.has(n))return {success:false,error:`Plugin '${e.id}' requires '${n}' which is not registered`}}if(e.commandHandlers)for(let[n,o]of Object.entries(e.commandHandlers)){if(this.commandHandlers.has(n)){let s=this.commandHandlers.get(n);r.push(`Command '${n}' from '${e.id}' overrides handler from '${s.pluginId}'`);}this.commandHandlers.set(n,{pluginId:e.id,handler:o});}if(this.plugins.set(e.id,e),e.initialize&&!this.initialized.has(e.id))try{let n=e.initialize();n instanceof Promise?n.then(()=>{this.initialized.add(e.id);}).catch(o=>{this.emit({type:"error",pluginId:e.id,error:o});}):this.initialized.add(e.id);}catch(n){this.emit({type:"error",pluginId:e.id,error:n});}return t?.debug&&console.log(`[PluginRegistry] Registered plugin: ${e.id}`),this.emit({type:"registered",plugin:e}),{success:true,plugin:e,warnings:r.length>0?r:void 0}}unregister(e){let t=this.plugins.get(e);if(!t)return  false;for(let[r,n]of this.plugins)if(n.dependencies?.includes(e))return console.warn(`Cannot unregister '${e}': '${r}' depends on it`),false;for(let[r,{pluginId:n}]of this.commandHandlers)n===e&&this.commandHandlers.delete(r);if(t.destroy)try{let r=t.destroy();r instanceof Promise&&r.catch(n=>{this.emit({type:"error",pluginId:e,error:n});});}catch(r){this.emit({type:"error",pluginId:e,error:r});}return this.plugins.delete(e),this.initialized.delete(e),this.emit({type:"unregistered",pluginId:e}),true}get(e){return this.plugins.get(e)}getAll(){return Array.from(this.plugins.values())}has(e){return this.plugins.has(e)}get size(){return this.plugins.size}getCommandHandler(e){return this.commandHandlers.get(e)?.handler}getCommandTypes(){return Array.from(this.commandHandlers.keys())}hasCommandHandler(e){return this.commandHandlers.has(e)}getMcpTools(){let e=[];for(let t of this.plugins.values())t.mcpTools&&e.push(...t.mcpTools);return e}getMcpToolsForPlugin(e){return this.plugins.get(e)?.mcpTools||[]}getMcpTool(e){for(let t of this.plugins.values())if(t.mcpTools){let r=t.mcpTools.find(n=>n.name===e);if(r)return r}}addEventListener(e){this.eventListeners.add(e);}removeEventListener(e){this.eventListeners.delete(e);}emit(e){for(let t of this.eventListeners)try{t(e);}catch(r){console.error("[PluginRegistry] Event listener error:",r);}}clear(){for(let e of this.plugins.values())if(e.destroy)try{e.destroy();}catch{}this.plugins.clear(),this.commandHandlers.clear(),this.initialized.clear();}getDebugInfo(){return {plugins:Array.from(this.plugins.keys()),commandTypes:Array.from(this.commandHandlers.keys()),mcpTools:this.getMcpTools().map(e=>e.name),initialized:Array.from(this.initialized)}}},S=new D;function te(a,e){return a.map(t=>S.register(t,e))}function ne(a){return e=>S.register(e,a)}function g(a){return JSON.parse(JSON.stringify(a))}function d(a,e){let t=0;for(let r=0;r<a.content.length;r++)if(a.content[r].type==="paragraph"){if(t===e)return r;t++;}return  -1}function P(a){let e="";for(let t of a.content)if(t.type==="run")for(let r of t.content)r.type==="text"&&(e+=r.text);else if(t.type==="hyperlink"){for(let r of t.children)if(r.type==="run")for(let n of r.content)n.type==="text"&&(e+=n.text);}return e}function T(a,e){return {type:"run",formatting:e,content:[{type:"text",text:a}]}}function I(a,e,t,r){let n=[],o=0,s=false;for(let i of a.content)if(i.type==="run"){let p=i.content.filter(c=>c.type==="text").map(c=>c.text).join(""),l=o,u=o+p.length;if(!s&&e>=l&&e<=u){let c=e-l;c>0&&n.push({...i,content:[{type:"text",text:p.slice(0,c)}]}),n.push(T(t,r||i.formatting)),c<p.length&&n.push({...i,content:[{type:"text",text:p.slice(c)}]}),s=true;}else n.push(i);o=u;}else n.push(i);return s||n.push(T(t,r)),n}function m(a,e,t){let r=[],n=0;for(let o of a.content)if(o.type==="run"){let s=o.content.filter(l=>l.type==="text").map(l=>l.text).join(""),i=n,p=n+s.length;if(p<=e||i>=t)r.push(o);else {let l="";i<e&&(l+=s.slice(0,e-i)),p>t&&(l+=s.slice(t-i)),l.length>0&&r.push({...o,content:[{type:"text",text:l}]});}n=p;}else r.push(o);return r}function w(a,e,t,r){let n=[],o=0;for(let s of a.content)if(s.type==="run"){let i=s.content.filter(u=>u.type==="text").map(u=>u.text).join(""),p=o,l=o+i.length;if(l<=e||p>=t)n.push(s);else if(p>=e&&l<=t)n.push({...s,formatting:{...s.formatting,...r}});else {let u=Math.max(e,p),c=Math.min(t,l);p<u&&n.push({...s,content:[{type:"text",text:i.slice(0,u-p)}]}),n.push({...s,formatting:{...s.formatting,...r},content:[{type:"text",text:i.slice(u-p,c-p)}]}),l>c&&n.push({...s,content:[{type:"text",text:i.slice(c-p)}]});}o=l;}else n.push(s);return n}function H(a,e){let t=g(a),r=t.package.document,n=d(r,e.position.paragraphIndex);if(n===-1)throw new Error(`Paragraph index ${e.position.paragraphIndex} not found`);let o=r.content[n];return o.content=I(o,e.position.offset,e.text,e.formatting),t}function A(a,e){let t=g(a),r=t.package.document,{start:n,end:o}=e.range;if(n.paragraphIndex===o.paragraphIndex){let s=d(r,n.paragraphIndex);if(s===-1)throw new Error(`Paragraph index ${n.paragraphIndex} not found`);let i=r.content[s];i.content=m(i,n.offset,o.offset),i.content=I(i,n.offset,e.text,e.formatting);}else {let s=d(r,n.paragraphIndex),i=r.content[s],p=P(i);i.content=m(i,n.offset,p.length),i.content=I(i,n.offset,e.text,e.formatting);let l=[];for(let u=n.paragraphIndex+1;u<=o.paragraphIndex;u++)l.push(d(r,u));for(let u=l.length-1;u>=0;u--)l[u]!==-1&&r.content.splice(l[u],1);}return t}function N(a,e){let t=g(a),r=t.package.document,{start:n,end:o}=e.range;if(n.paragraphIndex===o.paragraphIndex){let s=d(r,n.paragraphIndex);if(s===-1)throw new Error(`Paragraph index ${n.paragraphIndex} not found`);let i=r.content[s];i.content=m(i,n.offset,o.offset);}else {let s=d(r,n.paragraphIndex),i=r.content[s],p=P(i);i.content=m(i,n.offset,p.length);let l=d(r,o.paragraphIndex),u=r.content[l];u.content=m(u,0,o.offset),i.content.push(...u.content);let c=[];for(let f=n.paragraphIndex+1;f<=o.paragraphIndex;f++)c.push(d(r,f));for(let f=c.length-1;f>=0;f--)c[f]!==-1&&r.content.splice(c[f],1);}return t}function B(a,e){let t=g(a),r=t.package.document,{start:n,end:o}=e.range;if(n.paragraphIndex===o.paragraphIndex){let s=d(r,n.paragraphIndex);if(s===-1)throw new Error(`Paragraph index ${n.paragraphIndex} not found`);let i=r.content[s];i.content=w(i,n.offset,o.offset,e.formatting);}else for(let s=n.paragraphIndex;s<=o.paragraphIndex;s++){let i=d(r,s);if(i===-1)continue;let p=r.content[i],l=P(p),u=0,c=l.length;s===n.paragraphIndex&&(u=n.offset),s===o.paragraphIndex&&(c=o.offset),p.content=w(p,u,c,e.formatting);}return t}function L(a,e){let t=g(a),r=t.package.document,n=d(r,e.paragraphIndex);if(n===-1)throw new Error(`Paragraph index ${e.paragraphIndex} not found`);let o=r.content[n];if(o.formatting={...o.formatting,...e.formatting},"numPr"in e.formatting){let s=e.formatting.numPr;if(s&&s.numId!==void 0&&s.numId!==0){let i=s.ilvl??0,p=s.numId===1,l=p?"\u2022":"1.";if(t.package.numbering){let u=t.package.numbering.nums.find(c=>c.numId===s.numId);if(u){let c=t.package.numbering.abstractNums.find(f=>f.abstractNumId===u.abstractNumId);if(c){let f=c.levels.find(h=>h.ilvl===i);f&&(l=f.lvlText||l);}}}o.listRendering={level:i,numId:s.numId,marker:l,isBullet:p};}else delete o.listRendering;}return t}function M(a,e){let t=g(a),r=t.package.document,n=d(r,e.paragraphIndex);if(n===-1)throw new Error(`Paragraph index ${e.paragraphIndex} not found`);let o=r.content[n];return o.formatting={...o.formatting,styleId:e.styleId},t}function se(a){return a/1440*96}function pe(a){return a/96*1440}function le(a){return a==null||isNaN(a)?0:Math.round(a*96/914400)}function R(a){return Math.round(a/96*914400)}function ue(a){return Math.round(a/914400*1440)}function ce(a){return Math.round(a/1440*914400)}function fe(a){return a/72*96}function de(a){return a/144*96}function ge(a){return a*2}function Y(a,e=2){let t=Math.pow(10,e);return Math.round(a*t)/t}function he(a){return `${Y(a)}px`}function O(a,e){if(e<=0)return [];let t=Math.floor(a/e),r=a-t*e;return Array.from({length:e},()=>{let n=t+(r>0?1:0);return r=Math.max(0,r-1),Math.max(1,n)})}function $(a,e){let t=a?{...a}:{},r=e,n=[["ascii","asciiTheme"],["hAnsi","hAnsiTheme"],["eastAsia","eastAsiaTheme"],["cs","csTheme"]],o=new Set;for(let[s,i]of n)o.add(s),o.add(i),(r[s]!==void 0||r[i]!==void 0)&&(delete t[s],delete t[i],r[s]!==void 0&&(t[s]=r[s]),r[i]!==void 0&&(t[i]=r[i]));for(let s of Object.keys(r))!o.has(s)&&r[s]!==void 0&&(t[s]=r[s]);return t}function V(a,e){if(!e&&!a)return;if(!e)return a;if(!a)return {...e};let t={...a};for(let r of Object.keys(e)){let n=e[r];if(n!==void 0){if(r==="fontFamily"&&typeof n=="object"&&n!==null){t.fontFamily=$(a.fontFamily,n);continue}if(r==="color"&&typeof n=="object"&&n!==null){let o=n,s=!!(o.rgb||o.themeColor||o.themeTint||o.themeShade);(!o.auto||s)&&(t.color=o);continue}if(typeof n=="object"&&n!==null&&!Array.isArray(n)){t[r]={...a[r]??{},...n};continue}t[r]=n;}}return t}var Q={styleId:"Normal",type:"paragraph",name:"Normal",default:true,pPr:{spaceAfter:160,lineSpacing:259,lineSpacingRule:"auto"}},C=class{constructor(e){if(this.stylesById=new Map,this.docDefaults=e?.docDefaults,e?.styles)for(let t of e.styles)t.styleId&&this.stylesById.set(t.styleId,t);this.defaultParagraphStyle=this.findDefaultStyle("paragraph"),this.defaultTableStyle=this.findDefaultStyle("table"),this.defaultCharacterStyle=this.findDefaultStyle("character");}getStyle(e){return this.stylesById.get(e)}getParagraphStyles(){let e=[];for(let t of this.stylesById.values())t.type==="paragraph"&&!t.hidden&&!t.semiHidden&&e.push(t);return e.sort((t,r)=>{let n=t.uiPriority??99,o=r.uiPriority??99;return n!==o?n-o:(t.name??t.styleId).localeCompare(r.name??r.styleId)})}hasParagraphStyle(e){return this.stylesById.get(e)?.type==="paragraph"}resolveParagraphStyle(e){let t={};if(this.docDefaults?.pPr&&(t.paragraphFormatting={...this.docDefaults.pPr}),this.docDefaults?.rPr&&(t.runFormatting={...this.docDefaults.rPr}),!e)return this.defaultParagraphStyle&&this.mergeStyleIntoResult(t,this.defaultParagraphStyle),t;let r=this.stylesById.get(e);return r?(this.mergeStyleIntoResult(t,r),t):(this.defaultParagraphStyle&&this.mergeStyleIntoResult(t,this.defaultParagraphStyle),t)}getNextStyleId(e){if(!e)return null;let t=this.stylesById.get(e)?.next;return t&&t!==e?t:null}getTableStyles(){let e=[];for(let t of this.stylesById.values())t.type==="table"&&!t.hidden&&!t.semiHidden&&e.push(t);return e.sort((t,r)=>{let n=t.uiPriority??99,o=r.uiPriority??99;return n!==o?n-o:(t.name??t.styleId).localeCompare(r.name??r.styleId)})}resolveRunStyle(e){let t={};if(this.docDefaults?.rPr&&(t={...this.docDefaults.rPr}),this.defaultCharacterStyle?.rPr&&(t=this.mergeTextFormatting(t,this.defaultCharacterStyle.rPr)??t),!e)return Object.keys(t).length>0?t:void 0;let r=this.stylesById.get(e);if(!r?.rPr)return Object.keys(t).length>0?t:void 0;let n=this.mergeTextFormatting(t,r.rPr);return n&&Object.keys(n).length>0?n:void 0}getRunStyleOwnProperties(e){if(!e)return;let t=this.stylesById.get(e);if(t?.rPr)return Object.keys(t.rPr).length>0?{...t.rPr}:void 0}getDocDefaults(){return this.docDefaults}getDefaultParagraphStyle(){return this.defaultParagraphStyle}getDefaultTableStyle(){return this.defaultTableStyle}getDefaultCharacterStyle(){return this.defaultCharacterStyle}hasStyle(e){return this.stylesById.has(e)}findDefaultStyle(e){for(let t of this.stylesById.values())if(t.type===e&&t.default)return t;if(e==="paragraph")return this.stylesById.get("Normal")??(this.docDefaults?void 0:Q)}mergeStyleIntoResult(e,t){t.pPr&&(e.paragraphFormatting=this.mergeParagraphFormatting(e.paragraphFormatting,t.pPr)),t.rPr&&(e.runFormatting=this.mergeTextFormatting(e.runFormatting,t.rPr));}mergeParagraphFormatting(e,t){if(!t)return e;if(!e)return t?{...t}:void 0;let r={...e};for(let n of Object.keys(t)){let o=t[n];if(o!==void 0)if(n==="runProperties")r.runProperties=this.mergeTextFormatting(r.runProperties,t.runProperties);else if(n==="borders"||n==="numPr"||n==="frame"){let s=r[n],i=o;r[n]={...s||{},...i||{}};}else n==="tabs"&&Array.isArray(o)?r.tabs=[...o]:r[n]=o;}return r}mergeTextFormatting(e,t){return V(e,t)}};function k(a){return new C(a)}var E={firstRow:true,firstColumn:true,lastRow:false,lastColumn:false,noHBand:false,noVBand:true};function Z(a,e){if(a.toLowerCase()==="tablenormal")return  true;let t=e?.trim().toLowerCase();return t==="table normal"||t==="normal table"}function v(a,e){if(!e)return;if(a&&e.hasStyle(a))return a;let t=e.getDefaultTableStyle();if(t?.styleId&&!Z(t.styleId,t.name))return t.styleId}function z(a,e){let t=g(a),r=t.package.document,n=O(9360,e.columns),o=[];for(let u=0;u<e.rows;u++){let c=[];for(let f=0;f<e.columns;f++){let h=e.data?.[u]?.[f]||"";c.push({type:"tableCell",formatting:n[f]?{width:{value:n[f],type:"dxa"}}:void 0,content:[{type:"paragraph",content:h?[T(h)]:[]}]});}o.push({type:"tableRow",formatting:u===0&&e.hasHeader?{header:true}:void 0,cells:c});}let s=k(t.package.styles),i=v(t.package.settings?.defaultTableStyle,s),p={type:"table",formatting:{width:{value:9360,type:"dxa"},layout:"fixed",...i?{styleId:i,look:{...E}}:{}},columnWidths:n,rows:o},l=d(r,e.position.paragraphIndex);return l===-1?r.content.push(p):r.content.splice(l+1,0,p),t}function U(a,e){let t=g(a),r=t.package.document,n=d(r,e.position.paragraphIndex);if(n===-1)throw new Error(`Paragraph index ${e.position.paragraphIndex} not found`);let o=r.content[n],i={type:"run",content:[{type:"drawing",image:{type:"image",rId:`rId_img_${Date.now()}`,src:e.src,alt:e.alt,size:{width:R(e.width||100),height:R(e.height||100)},wrap:{type:"inline"}}}]},p=I(o,e.position.offset,"",void 0),l=false,u=0;for(let c=0;c<p.length;c++){let f=p[c];if(f.type==="run"){let h=f.content.filter(y=>y.type==="text").map(y=>y.text).join("");if(u+=h.length,!l&&u>=e.position.offset){p.splice(c+1,0,i),l=true;break}}}return l||p.push(i),o.content=p,t}function W(a,e){let t=g(a),r=t.package.document,{start:n,end:o}=e.range;if(n.paragraphIndex!==o.paragraphIndex)throw new Error("Hyperlinks cannot span multiple paragraphs");let s=d(r,n.paragraphIndex);if(s===-1)throw new Error(`Paragraph index ${n.paragraphIndex} not found`);let i=r.content[s],p=P(i),l=e.displayText||p.slice(n.offset,o.offset);i.content=m(i,n.offset,o.offset);let u={type:"hyperlink",href:e.url,tooltip:e.tooltip,children:[T(l)]},c=false,f=0,h=[];for(let y of i.content)if(y.type==="run"){let b=y.content.filter(x=>x.type==="text").map(x=>x.text).join(""),_=f+b.length;if(!c&&f<=n.offset&&n.offset<=_){let x=n.offset-f;x>0&&h.push({...y,content:[{type:"text",text:b.slice(0,x)}]}),h.push(u),x<b.length&&h.push({...y,content:[{type:"text",text:b.slice(x)}]}),c=true;}else h.push(y);f=_;}else h.push(y);return c||h.push(u),i.content=h,t}function X(a,e){let t=g(a),r=t.package.document,{start:n}=e.range,o=d(r,n.paragraphIndex);if(o===-1)throw new Error(`Paragraph index ${n.paragraphIndex} not found`);let s=r.content[o],i=[];for(let p of s.content)if(p.type==="hyperlink")for(let l of p.children)l.type==="run"&&i.push(l);else i.push(p);return s.content=i,t}function F(a,e){let t=g(a),r=t.package.document,n=d(r,e.position.paragraphIndex);if(n===-1)throw new Error(`Paragraph index ${e.position.paragraphIndex} not found`);let o=r.content[n],s=P(o),i=m({content:[...o.content]},e.position.offset,s.length),p=m({content:[...o.content]},0,e.position.offset);o.content=i;let l={type:"paragraph",formatting:o.formatting,content:p};return r.content.splice(n+1,0,l),t}function G(a,e){let t=g(a),r=t.package.document,n=d(r,e.paragraphIndex);if(n===-1)throw new Error(`Paragraph index ${e.paragraphIndex} not found`);let o=r.content[n],s=[];for(let i=1;i<=e.count;i++){let p=d(r,e.paragraphIndex+i);if(p!==-1){let l=r.content[p];o.content.push(...l.content),s.push(p);}}for(let i=s.length-1;i>=0;i--)r.content.splice(s[i],1);return t}function K(a,e){return F(a,{position:e.position})}function J(a,e){let t=g(a);return t.templateVariables||(t.templateVariables=[]),t.templateVariables.includes(e.name)||t.templateVariables.push(e.name),t}function q(a,e){let t=g(a),r=t.package.document;function n(i){for(let p of i.content)if(p.type==="text")for(let[l,u]of Object.entries(e.values)){let c=new RegExp(`\\{${l}\\}`,"g");p.text=p.text.replace(c,u);}}function o(i){for(let p of i.content)if(p.type==="run")n(p);else if(p.type==="hyperlink")for(let l of p.children)l.type==="run"&&n(l);}function s(i){if(i.type==="paragraph")o(i);else if(i.type==="table")for(let p of i.rows)for(let l of p.cells)for(let u of l.content)s(u);}for(let i of r.content)s(i);return t}function ee(a,e){let t=S.getCommandHandler(e.type);if(t)return t(a,e);switch(e.type){case "insertText":return H(a,e);case "replaceText":return A(a,e);case "deleteText":return N(a,e);case "formatText":return B(a,e);case "formatParagraph":return L(a,e);case "applyStyle":return M(a,e);case "insertTable":return z(a,e);case "insertImage":return U(a,e);case "insertHyperlink":return W(a,e);case "removeHyperlink":return X(a,e);case "insertParagraphBreak":return F(a,e);case "mergeParagraphs":return G(a,e);case "splitParagraph":return K(a,e);case "setVariable":return J(a,e);case "applyVariables":return q(a,e);default:{let r=e;throw new Error(`Unknown command type: ${r.type}`)}}}function Ve(a,e){return e.reduce((t,r)=>ee(t,r),a)}export{D as a,S as b,te as c,ne as d,se as e,pe as f,le as g,R as h,ue as i,ce as j,fe as k,de as l,ge as m,he as n,O as o,V as p,ee as q,Ve as r};