@lucio-ai/docxtor 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,21 +1,21 @@
1
1
  import{readFile as e}from"node:fs/promises";import t from"jszip";import{XMLBuilder as n,XMLParser as r}from"fast-xml-parser";async function i(e){let n=await t.loadAsync(e),r=await a(n,`word/document.xml`);if(r===void 0)throw Error(`Invalid DOCX: missing word/document.xml`);let i=await a(n,`word/_rels/document.xml.rels`),o=await a(n,`[Content_Types].xml`),s=await a(n,`word/styles.xml`),c=await a(n,`word/settings.xml`),l=await a(n,`word/numbering.xml`),u=await a(n,`word/footnotes.xml`),d=await a(n,`word/endnotes.xml`),f=await a(n,`word/comments.xml`),p=await a(n,`docProps/core.xml`),m=await a(n,`docProps/app.xml`),h=await a(n,`_rels/.rels`),g=new Map,_=new Map;for(let e of Object.keys(n.files))if(/^word\/header\d+\.xml$/i.test(e)){let t=await a(n,e);t!==void 0&&g.set(e,t)}else if(/^word\/footer\d+\.xml$/i.test(e)){let t=await a(n,e);t!==void 0&&_.set(e,t)}return{documentXml:r,headers:g,footers:_,documentRels:i,contentTypes:o,stylesXml:s,settingsXml:c,numberingXml:l,footnotesXml:u,endnotesXml:d,commentsXml:f,coreXml:p,appXml:m,rootRels:h,zip:n}}async function a(e,t){let n=e.file(t);if(n)return n.async(`string`)}const o=[[`documentRelsXml`,`word/_rels/document.xml.rels`],[`contentTypesXml`,`[Content_Types].xml`],[`settingsXml`,`word/settings.xml`],[`numberingXml`,`word/numbering.xml`],[`footnotesXml`,`word/footnotes.xml`],[`endnotesXml`,`word/endnotes.xml`],[`commentsXml`,`word/comments.xml`],[`coreXml`,`docProps/core.xml`],[`appXml`,`docProps/app.xml`],[`rootRelsXml`,`_rels/.rels`]];async function s(e){let{zip:t,documentXml:n,headers:r,footers:i,removedParts:a}=e;if(a)for(let e of a)t.remove(e);if(t.file(`word/document.xml`,n),r)for(let[e,n]of r)t.file(e,n);if(i)for(let[e,n]of i)t.file(e,n);for(let[n,r]of o){let i=e[n];typeof i==`string`&&t.file(r,i)}return await t.generateAsync({type:`nodebuffer`,compression:`DEFLATE`,compressionOptions:{level:6}})}const c=new n({preserveOrder:!0,ignoreAttributes:!1,attributeNamePrefix:`@_`,commentPropName:`#comment`,cdataPropName:`#cdata`,processEntities:!1,format:!1,suppressEmptyNode:!1,suppressBooleanAttributes:!1});function l(e){return c.build(e)}const u=new r({preserveOrder:!0,ignoreAttributes:!1,attributeNamePrefix:`@_`,commentPropName:`#comment`,cdataPropName:`#cdata`,processEntities:!1,trimValues:!1,parseTagValue:!1,parseAttributeValue:!1,ignoreDeclaration:!1,ignorePiTags:!1});function d(e){return u.parse(e)}function f(e){for(let t of Object.keys(e))if(t!==`:@`&&t!==`#text`)return t}function p(e,t){return t in e}function m(e){let t=f(e);if(!t)return[];let n=e[t];return Array.isArray(n)?n:[]}function h(e,t){return m(e).find(e=>t in e)}function g(e,t){return m(e).filter(e=>t in e)}function _(e,t){let n=e[`:@`];if(n)return n[`@_${t}`]}function v(e,t,n){e[`:@`]||={},e[`:@`][`@_${t}`]=n}function ee(e,t){let n=e[`:@`];n&&delete n[`@_${t}`]}function te(e){let t=e[`:@`];if(!t)return{};let n={};for(let[e,r]of Object.entries(t))e.startsWith(`@_`)&&(n[e.slice(2)]=r);return n}function y(e){return e.replace(/&/g,`&amp;`).replace(/</g,`&lt;`).replace(/>/g,`&gt;`)}function b(e){return e.replace(/&amp;/g,`&`).replace(/&lt;/g,`<`).replace(/&gt;/g,`>`).replace(/&quot;/g,`"`).replace(/&apos;/g,`'`)}function ne(e,t){e[`#text`]=y(t)}function re(e){let t=m(e),n=``;for(let e of t)if(`w:t`in e){let t=e[`w:t`];if(Array.isArray(t))for(let e of t)`#text`in e&&(n+=b(String(e[`#text`])))}else if(`w:br`in e){let t=_(e,`w:type`);(!t||t===`textWrapping`)&&(n+=`
2
2
  `)}return n}function ie(e){return m(e).find(e=>`w:t`in e)}function ae(e){let t=m(e);for(let e of t)if(`w:br`in e&&_(e,`w:type`)===`page`||`w:lastRenderedPageBreak`in e)return!0;return!1}function oe(e){let t=m(e),n=0;for(let e of t)if(`w:t`in e){let t=e[`w:t`];if(Array.isArray(t))for(let e of t)`#text`in e&&(n+=b(String(e[`#text`])).length)}else if(`w:br`in e){let t=_(e,`w:type`);if(!t||t===`textWrapping`)n+=1;else if(t===`page`)return n}else if(`w:lastRenderedPageBreak`in e)return n;return-1}function se(e){let t=h(e,`w:pPr`);if(t){let e=h(t,`w:pageBreakBefore`);if(e){let t=_(e,`w:val`);if(t!==`0`&&t!==`false`)return!0}}return ce(m(e))}function ce(e){for(let t of e){let e=f(t);if(e===`w:r`){if(ae(t))return!0}else if((e===`w:hyperlink`||e===`w:ins`)&&ce(m(t)))return!0}return!1}function x(e){return JSON.parse(JSON.stringify(e))}function le(e,t,n){let r=m(e),i=r.indexOf(t);if(i===-1)throw Error(`Reference child not found in parent`);return r.splice(i+1,0,n),i+1}function S(e,t){let n=m(e),r=n.indexOf(t);return r===-1?!1:(n.splice(r,1),!0)}function C(e,t=[],n){let r={[e]:t};if(n&&Object.keys(n).length>0){let e={};for(let[t,r]of Object.entries(n))e[`@_${t}`]=r;r[`:@`]=e}return r}function ue(e){return{"#text":y(e)}}function de(e){let t={"w:t":[ue(e)]};return w(e)&&v(t,`xml:space`,`preserve`),t}function w(e){return e.length>0&&(e!==e.trim()||e.includes(` `))}function fe(e,t){let n=[];if(t&&n.push(x(t)),e.includes(`
3
3
  `)){let t=e.split(`
4
- `);for(let e=0;e<t.length;e++){let r=t[e];(r.length>0||e===0)&&n.push(de(r)),e<t.length-1&&n.push({"w:br":[]})}}else n.push(de(e));return{"w:r":n}}function pe(e){let t=m(e),n=[];for(let e of t){let t=f(e);t===`w:r`?n.push(e):(t===`w:hyperlink`||t===`w:ins`)&&me(e,n)}return n}function me(e,t){let n=m(e);for(let e of n){let n=f(e);n===`w:r`?t.push(e):(n===`w:hyperlink`||n===`w:ins`)&&me(e,t)}}function T(e,t){let n=pe(e),r=[],i=0,a;for(let e of n){let t=re(e);if(a===void 0&&ae(e)){let t=oe(e);t>=0&&(a=i+t)}if(t.length===0)continue;let n=ie(e),o=he(e),s={text:t,startOffset:i,endOffset:i+t.length,xmlRef:e,formatting:o};n&&(s.textXmlRef=n),r.push(s),i+=t.length}let o=r.map(e=>e.text).join(``),s=ve(e,r),c={kind:`paragraph`,id:t,text:o,runs:r,xmlRef:e,style:ge(e),listInfo:_e(e)},l=_(e,`w14:paraId`);if(l&&(c.paraId=l.toUpperCase()),s.length>0&&(c.revisions=s),se(e)){c.hasPageBreak=!0;let t=h(e,`w:pPr`);if(t){let e=h(t,`w:pageBreakBefore`);if(e){let t=_(e,`w:val`);if(t!==`0`&&t!==`false`)return c.pageBreakOffset=0,c}}a!==void 0&&(c.pageBreakOffset=a)}return c}function he(e){let t=h(e,`w:rPr`);if(!t)return{};let n={},r=m(t);for(let e of r)if(p(e,`w:b`)){let t=_(e,`w:val`);n.bold=t!==`0`&&t!==`false`}else if(p(e,`w:i`)){let t=_(e,`w:val`);n.italic=t!==`0`&&t!==`false`}else if(p(e,`w:u`))n.underline=_(e,`w:val`)??`single`;else if(p(e,`w:strike`)){let t=_(e,`w:val`);n.strikethrough=t!==`0`&&t!==`false`}else if(p(e,`w:sz`)){let t=_(e,`w:val`);t&&(n.fontSize=parseInt(t,10))}else if(p(e,`w:rFonts`))n.fontFamily=_(e,`w:ascii`)??_(e,`w:hAnsi`);else if(p(e,`w:color`)){n.color=_(e,`w:val`);let t=_(e,`w:themeColor`);t&&(n.themeColor=t)}else p(e,`w:highlight`)&&(n.highlight=_(e,`w:val`));return n}function ge(e){let t=h(e,`w:pPr`);if(!t)return;let n=h(t,`w:pStyle`);if(n)return _(n,`w:val`)}function _e(e){let t=h(e,`w:pPr`);if(!t)return;let n=h(t,`w:numPr`);if(!n)return;let r=h(n,`w:numId`),i=h(n,`w:ilvl`),a=r?_(r,`w:val`):void 0,o=i?_(i,`w:val`):void 0;if(a&&a!==`0`)return{numId:a,level:o?parseInt(o,10):0}}function ve(e,t){let n=[],r=new Set;ye(e,r);for(let i of t)if(r.has(i.xmlRef)){let t=xe(e,i.xmlRef);n.push({type:`insert`,text:i.text,startOffset:i.startOffset,endOffset:i.endOffset,author:t?.author??``,date:t?.date??``})}return we(e,t,n),n}function ye(e,t){let n=m(e);for(let e of n){let n=f(e);n===`w:ins`?be(e,t):n===`w:hyperlink`&&ye(e,t)}}function be(e,t){let n=m(e);for(let e of n){let n=f(e);n===`w:r`?t.add(e):(n===`w:hyperlink`||n===`w:ins`)&&be(e,t)}}function xe(e,t){return Se(m(e),t)}function Se(e,t){for(let n of e){let e=f(n);if(e===`w:ins`){if(Ce(n,t))return{author:_(n,`w:author`)??``,date:_(n,`w:date`)??``}}else if(e===`w:hyperlink`){let e=Se(m(n),t);if(e)return e}}}function Ce(e,t){let n=m(e);for(let e of n){if(e===t)return!0;let n=f(e);if(n&&n!==`w:del`&&Ce(e,t))return!0}return!1}function we(e,t,n){let r=new Map;for(let e of t)r.set(e.xmlRef,e);Te(m(e),r,n,0)}function Te(e,t,n,r){for(let i of e){let e=f(i);if(e===`w:r`){let e=t.get(i);e&&(r=e.endOffset)}else if(e===`w:del`){let e=Ee(i);e.length>0&&n.push({type:`delete`,text:e,startOffset:r,endOffset:r,author:_(i,`w:author`)??``,date:_(i,`w:date`)??``})}else (e===`w:hyperlink`||e===`w:ins`)&&(r=Te(m(i),t,n,r))}return r}function Ee(e){let t=m(e),n=``;for(let e of t)if(f(e)===`w:r`){let t=m(e);for(let e of t)if(`w:br`in e){let t=_(e,`w:type`);(!t||t===`textWrapping`)&&(n+=`
5
- `)}else if(`w:delText`in e){let t=e[`w:delText`];if(Array.isArray(t))for(let e of t)`#text`in e&&(n+=b(String(e[`#text`])))}else if(`w:t`in e){let t=e[`w:t`];if(Array.isArray(t))for(let e of t)`#text`in e&&(n+=b(String(e[`#text`])))}}return n}function E(e,t){let n=[],r=g(e,`w:tr`);for(let e=0;e<r.length;e++){let i=r[e],a=[],o=g(i,`w:tc`);for(let n=0;n<o.length;n++){let r=o[n],i=g(r,`w:p`).map((r,i)=>T(r,`${t}.r${e}.c${n}.p${i}`)),s=g(r,`w:tbl`).map((r,i)=>E(r,`${t}.r${e}.c${n}.tbl${i}`));a.push({rowIndex:e,colIndex:n,paragraphs:i,nestedTables:s,xmlRef:r})}n.push({index:e,cells:a,xmlRef:i})}let i=h(e,`w:tblPr`),a=i?h(i,`w:tblStyle`):void 0;return{kind:`table`,id:t,rows:n,xmlRef:e,style:a?_(a,`w:val`):void 0}}function De(e,t,n,r){let i=t===`header`?`hdr`:`ftr`,a=t===`header`?`w:hdr`:`w:ftr`,o=e.find(e=>f(e)===a),s=[],c=[];if(o){let e=m(o),t=0,r=0;for(let a of e){let e=f(a);e===`w:p`?(s.push(T(a,`${i}.${n}.p${t}`)),t++):e===`w:tbl`&&(c.push(E(a,`${i}.${n}.tbl${r}`)),r++)}}return{type:t,variant:n,rId:r,paragraphs:s,tables:c,xmlRef:e}}function Oe(e){if(e.xmlParts.numbering)return e.xmlParts.numbering;let t=C(`w:numbering`,[],{"xmlns:w":`http://schemas.openxmlformats.org/wordprocessingml/2006/main`,"xmlns:r":`http://schemas.openxmlformats.org/officeDocument/2006/relationships`});if(e.xmlParts.numbering=[t],e.xmlParts.contentTypes){let t=e.xmlParts.contentTypes.find(e=>f(e)===`Types`);if(t){let e=m(t);e.some(e=>f(e)===`Override`&&_(e,`PartName`)===`/word/numbering.xml`)||e.push(C(`Override`,[],{PartName:`/word/numbering.xml`,ContentType:`application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml`}))}}if(e.xmlParts.documentRels){let t=e.xmlParts.documentRels.find(e=>f(e)===`Relationships`);if(t){let e=m(t);if(!e.some(e=>_(e,`Type`)===`http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering`)){let t=0;for(let n of e){let e=_(n,`Id`);if(e){let n=parseInt(e.replace(`rId`,``),10);!isNaN(n)&&n>t&&(t=n)}}e.push(C(`Relationship`,[],{Id:`rId${t+1}`,Type:`http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering`,Target:`numbering.xml`}))}}}return e.xmlParts.numbering}function ke(e){let t=e.find(e=>f(e)===`w:numbering`);if(!t)return 1;let n=0;for(let e of g(t,`w:num`)){let t=_(e,`w:numId`);if(t){let e=parseInt(t,10);!isNaN(e)&&e>n&&(n=e)}}return n+1}function Ae(e){let t=e.find(e=>f(e)===`w:numbering`);if(!t)return 0;let n=-1;for(let e of g(t,`w:abstractNum`)){let t=_(e,`w:abstractNumId`);if(t){let e=parseInt(t,10);!isNaN(e)&&e>n&&(n=e)}}return n+1}const je=[{char:``,font:`Symbol`},{char:`o`,font:`Courier New`},{char:``,font:`Wingdings`},{char:``,font:`Symbol`},{char:`o`,font:`Courier New`},{char:``,font:`Wingdings`},{char:``,font:`Symbol`},{char:`o`,font:`Courier New`},{char:``,font:`Wingdings`}],Me=[{fmt:`decimal`,textFn:e=>`%${e+1}.`},{fmt:`lowerLetter`,textFn:e=>`%${e+1}.`},{fmt:`lowerRoman`,textFn:e=>`%${e+1}.`}];function D(e,t,n=9){let r=Oe(e),i=m(r.find(e=>f(e)===`w:numbering`)),a=Ae(r),o=ke(r),s=[];for(let e=0;e<n;e++){let n,r;if(t===`bullet`){n=`bullet`;let t=je[e%je.length];r=t.char;let i=C(`w:rPr`,[C(`w:rFonts`,[],{"w:ascii":t.font,"w:hAnsi":t.font,"w:hint":`default`})]);s.push(C(`w:lvl`,[C(`w:start`,[],{"w:val":`1`}),C(`w:numFmt`,[],{"w:val":n}),C(`w:lvlText`,[],{"w:val":r}),C(`w:lvlJc`,[],{"w:val":`left`}),i],{"w:ilvl":String(e)}));continue}else if(t===`multilevel`)n=`decimal`,r=Array.from({length:e+1},(e,t)=>`%${t+1}`).join(`.`)+`.`;else{let t=Me[e%Me.length];n=t.fmt,r=t.textFn(e)}s.push(C(`w:lvl`,[C(`w:start`,[],{"w:val":`1`}),C(`w:numFmt`,[],{"w:val":n}),C(`w:lvlText`,[],{"w:val":r}),C(`w:lvlJc`,[],{"w:val":`left`})],{"w:ilvl":String(e)}))}let c=C(`w:abstractNum`,s,{"w:abstractNumId":String(a)}),l=C(`w:num`,[C(`w:abstractNumId`,[],{"w:val":String(a)})],{"w:numId":String(o)}),u=i.findIndex(e=>f(e)===`w:num`);return u>=0?i.splice(u,0,c):i.push(c),i.push(l),String(o)}function Ne(e,t){let n=e.find(e=>f(e)===`w:numbering`);if(!n)return;let r=g(n,`w:num`).find(e=>_(e,`w:numId`)===t);if(!r)return;let i=h(r,`w:abstractNumId`);if(!i)return;let a=_(i,`w:val`);if(!a)return;let o=g(n,`w:abstractNum`).find(e=>_(e,`w:abstractNumId`)===a);if(!o)return;let s=g(o,`w:lvl`),c=!1,l=!1;for(let e of s){let t=h(e,`w:numFmt`);t&&_(t,`w:val`)===`bullet`&&(c=!0);let n=h(e,`w:lvlText`);if(n){let e=(_(n,`w:val`)??``).match(/%\d+/g);e&&e.length>1&&(l=!0)}}return c?`bullet`:l?`multilevel`:`ordered`}function Pe(e,t,n){let r=e.find(e=>f(e)===`w:numbering`);if(!r)return null;let i=g(r,`w:num`).find(e=>_(e,`w:numId`)===t);if(!i)return null;let a=h(i,`w:abstractNumId`);if(!a)return null;let o=_(a,`w:val`);if(!o)return null;let s=g(r,`w:abstractNum`).find(e=>_(e,`w:abstractNumId`)===o);if(!s)return null;let c=g(s,`w:lvl`),l=c.find(e=>_(e,`w:ilvl`)===String(n));if(!l)return null;let u=h(l,`w:numFmt`),d=h(l,`w:lvlText`),p=u?_(u,`w:val`)??`decimal`:`decimal`,m=d?_(d,`w:val`)??``:``,v=new Map,ee=m.match(/%(\d+)/g);if(ee)for(let e of ee){let t=parseInt(e.slice(1),10)-1,n=c.find(e=>_(e,`w:ilvl`)===String(t));if(n){let e=h(n,`w:numFmt`);v.set(t,e?_(e,`w:val`)??`decimal`:`decimal`)}}return{pattern:m,format:p,ancestorFormats:v}}const Fe=new Set([`w:document`,`w:hdr`,`w:ftr`,`w:footnotes`,`w:endnotes`,`w:comments`]),Ie=new WeakMap;function Le(e){let t=Ie.get(e.xmlParts);if(!t){t=new Set;for(let n of Re(e.xmlParts))ze(n,t);Ie.set(e.xmlParts,t)}return t}function*Re(e){for(let t of Object.values(e))if(Array.isArray(t))yield t;else if(t instanceof Map)for(let e of t.values())yield e}function ze(e,t){for(let n of e){let e=f(n);if(!e)continue;if(e===`w:p`){let e=_(n,`w14:paraId`);e&&t.add(e.toUpperCase())}let r=m(n);r.length>0&&ze(r,t)}}function Be(e){for(;;){let t=(Math.floor(Math.random()*2147483646)+1).toString(16).toUpperCase().padStart(8,`0`);if(!e.has(t))return e.add(t),t}}function Ve(e){if(!/^[0-9A-Fa-f]{8}$/.test(e))return!1;let t=parseInt(e,16);return t>0&&t<2147483648}function He(e){return e?_(e,`xmlns:w14`)!==void 0:!1}function Ue(e){_(e,`xmlns:w14`)===void 0&&v(e,`xmlns:w14`,`http://schemas.microsoft.com/office/word/2010/wordml`),_(e,`xmlns:mc`)===void 0&&v(e,`xmlns:mc`,`http://schemas.openxmlformats.org/markup-compatibility/2006`);let t=_(e,`mc:Ignorable`);if(t===void 0)v(e,`mc:Ignorable`,`w14`);else{let n=t.split(/\s+/).filter(e=>e.length>0);n.includes(`w14`)||(n.push(`w14`),v(e,`mc:Ignorable`,n.join(` `)))}}function We(e){return e.find(e=>{let t=f(e);return t!==void 0&&Fe.has(t)})}function Ge(e,t){let n=We(e);if(!n)return 0;let r=0,i=e=>{for(let n of e){let e=f(n);if(!e)continue;if(e===`w:p`){let e=_(n,`w14:paraId`);if(!e||!Ve(e)){let e=Be(t);v(n,`w14:paraId`,e),r++,_(n,`w14:textId`)===void 0&&v(n,`w14:textId`,e)}else _(n,`w14:textId`)===void 0&&v(n,`w14:textId`,e.toUpperCase())}let a=m(n);a.length>0&&i(a)}};return i(m(n)),r>0&&Ue(n),r}function Ke(e){let t=new Set;for(let n of Re(e))ze(n,t);Ge(e.document,t);for(let n of e.headers.values())Ge(n,t);for(let n of e.footers.values())Ge(n,t);Ie.set(e,t)}function O(e,t,n,r=!1){if(!He(n))return;let i=Le(e),a=e=>{for(let t of e){let e=f(t);if(!e)continue;if(e===`w:p`&&(r||_(t,`w14:paraId`)===void 0)){let e=Be(i);v(t,`w14:paraId`,e),v(t,`w14:textId`,e)}let n=m(t);n.length>0&&a(n)}};a(t)}function k(e,t){let n=t.match(/^(hdr|ftr)\.([^.]+)\./);if(n){let t=n[1]===`hdr`?e.headers:e.footers,r=n[1]===`hdr`?`w:hdr`:`w:ftr`;for(let e of t.values())if(e.variant===n[2])return e.xmlRef.find(e=>f(e)===r);return}return e.xmlParts.document.find(e=>f(e)===`w:document`)}function qe(e,t){let n=d(e.documentXml),r=new Map;for(let[t,n]of e.headers)r.set(t,d(n));let i=new Map;for(let[t,n]of e.footers)i.set(t,d(n));let a=e.stylesXml?Ze(e.stylesXml):[],o=e=>e?d(e):void 0,s={document:n,headers:r,footers:i,documentRels:o(e.documentRels),contentTypes:o(e.contentTypes),settings:o(e.settingsXml),numbering:o(e.numberingXml),footnotes:o(e.footnotesXml),endnotes:o(e.endnotesXml),comments:o(e.commentsXml),core:o(e.coreXml),app:o(e.appXml),rootRels:o(e.rootRels)};t?.assignParaIds&&Ke(s);let c={body:[],headers:new Map,footers:new Map,xmlParts:s,styles:a,loadOptions:t};return A(c),c}function A(e){let t=e.loadOptions?.includeHeadersFooters??!0;if(e.body=Je(e.xmlParts.document),e.headers=new Map,e.footers=new Map,t){let t=Ye(e.xmlParts.documentRels),n=Xe(e.xmlParts.document);for(let r of n){let n=t.get(r.rId);if(!n)continue;let i=`word/${n}`;if(r.type===`header`){let t=e.xmlParts.headers.get(i);t&&e.headers.set(r.rId,De(t,`header`,r.variant,r.rId))}else{let t=e.xmlParts.footers.get(i);t&&e.footers.set(r.rId,De(t,`footer`,r.variant,r.rId))}}}Qe(e)}function Je(e){let t=[],n=e.find(e=>f(e)===`w:document`);if(!n)return t;let r=h(n,`w:body`);if(!r)return t;let i=m(r),a=0,o=0,s=0;for(let e of i){let n=f(e);if(n===`w:p`){let n=T(e,`p${a}`);if(n.hasPageBreak&&n.text.trim().length===0){let n={kind:`page_break`,id:`pb${s}`,xmlRef:e};t.push(n),s++;continue}t.push(n),a++}else n===`w:tbl`&&(t.push(E(e,`tbl${o}`)),o++)}return t}function Ye(e){let t=new Map;if(!e)return t;let n=e.find(e=>f(e)===`Relationships`);if(!n)return t;let r=g(n,`Relationship`);for(let e of r){let n=_(e,`Id`),r=_(e,`Target`);n&&r&&t.set(n,r)}return t}function Xe(e){let t=[],n=e.find(e=>f(e)===`w:document`);if(!n)return t;let r=h(n,`w:body`);if(!r)return t;let i=g(r,`w:sectPr`),a=m(r);for(let e of a)if(f(e)===`w:p`){let t=h(e,`w:pPr`);if(t){let e=h(t,`w:sectPr`);e&&i.push(e)}}for(let e of i){let n=m(e);for(let e of n){let n=f(e);if(n===`w:headerReference`){let n=_(e,`r:id`),r=_(e,`w:type`)??`default`;n&&t.push({type:`header`,variant:r,rId:n})}else if(n===`w:footerReference`){let n=_(e,`r:id`),r=_(e,`w:type`)??`default`;n&&t.push({type:`footer`,variant:r,rId:n})}}}return t}function Ze(e){let t=d(e),n=[],r=t.find(e=>f(e)===`w:styles`);if(!r)return n;let i=g(r,`w:style`);for(let e of i){let t=_(e,`w:styleId`),r=_(e,`w:type`);if(!t||!r||![`paragraph`,`character`,`table`,`numbering`].includes(r))continue;let i=h(e,`w:name`),a=i?_(i,`w:val`)??t:t;n.push({id:t,name:a,type:r})}return n}function Qe(e){let t=e.xmlParts.numbering;if(t){for(let n of e.body)if(n.kind===`paragraph`)j(n,t);else if(n.kind===`table`)for(let e of n.rows)for(let n of e.cells)for(let e of n.paragraphs)j(e,t);for(let n of e.headers.values())for(let e of n.paragraphs)j(e,t);for(let n of e.footers.values())for(let e of n.paragraphs)j(e,t)}}function j(e,t){if(!e.listInfo)return;let n=Ne(t,e.listInfo.numId);n&&(e.listInfo.listType=n)}function $e(e){let t=e.split(`
6
- `),n=[],r=0;for(;r<t.length;){if(r+1<t.length&&it(t[r])&&at(t[r+1])){let e=ot(t[r]);r+=2;let i=[];for(;r<t.length&&it(t[r]);){let n=ot(t[r]);for(;n.length<e.length;)n.push(``);n.length>e.length&&(n.length=e.length),i.push(n),r++}n.push({kind:`table`,headers:e,rows:i});continue}let e=rt(t[r]);if(e){let i=[],a=e.listType;for(i.push({text:e.text,level:e.level}),r++;r<t.length;){let e=t[r];if(e.trim().length===0){r++;continue}let n=rt(e);if(!n||n.listType!==a)break;i.push({text:n.text,level:n.level}),r++}n.push({kind:`list`,listType:a,items:i});continue}let i=n[n.length-1];i&&i.kind===`text`?i.lines.push(t[r]):n.push({kind:`text`,lines:[t[r]]}),r++}return n}function et(e){return $e(e).some(e=>e.kind!==`text`)}const tt=/^(\s*)[*+-]\s(.*)$/,nt=/^(\s*)\d+\.\s(.*)$/;function rt(e){let t=e.replace(/\t/g,` `),n=tt.exec(t);if(n){let e=n[1].length;return{listType:`bullet`,text:n[2],level:Math.floor(e/2)}}if(n=nt.exec(t),n){let e=n[1].length;return{listType:`ordered`,text:n[2],level:Math.floor(e/2)}}}function it(e){let t=e.trim();return t.startsWith(`|`)&&t.endsWith(`|`)&&t.length>1}function at(e){let t=e.trim();return!t.startsWith(`|`)||!t.endsWith(`|`)?!1:t.slice(1,-1).split(`|`).every(e=>/^\s*:?-+:?\s*$/.test(e))}function ot(e){let t=e.trim(),n=t.startsWith(`|`)?t.slice(1):t;return(n.endsWith(`|`)?n.slice(0,-1):n).split(`|`).map(e=>e.trim())}function st(e,t){switch(e.type){case`insert_paragraph`:return M(e.target,t)?!e.text||e.text.length===0?{valid:!1,error:`Empty paragraph text`}:[`before`,`after`].includes(e.position)?et(e.text)?{valid:!1,error:`Markdown tables and lists are not supported in insert_paragraph; use add_table or add_list with afterId`}:{valid:!0}:{valid:!1,error:`Invalid position: ${e.position}`}:{valid:!1,error:`Paragraph "${e.target}" not found`};case`delete_paragraph`:return M(e.target,t)?{valid:!0}:{valid:!1,error:`Paragraph "${e.target}" not found`};case`add_comment`:return M(e.target,t)?!e.text||e.text.length===0?{valid:!1,error:`Empty comment text`}:e.find!==void 0&&e.find.length===0?{valid:!1,error:`Empty find string`}:e.find!==void 0&&e.occurrence!==void 0&&(e.occurrence<1||!Number.isInteger(e.occurrence))?{valid:!1,error:`Invalid occurrence: ${e.occurrence} (must be a positive integer)`}:{valid:!0}:{valid:!1,error:`Paragraph "${e.target}" not found`};case`replace_text`:case`insert_text`:case`delete_text`:case`replace_cell_text`:return M(e.target,t)?`find`in e&&e.find.length===0?{valid:!1,error:`Empty find string`}:e.type===`insert_text`&&e.text.length===0?{valid:!1,error:`Empty insert text`}:`occurrence`in e&&e.occurrence!==void 0&&(e.occurrence<1||!Number.isInteger(e.occurrence))?{valid:!1,error:`Invalid occurrence: ${e.occurrence} (must be a positive integer)`}:{valid:!0}:{valid:!1,error:`Paragraph "${e.target}" not found`};case`add_table_row`:{let n=N(e.target,t);return n?e.cells.length===0?{valid:!1,error:`Empty cells array`}:e.afterRow<-1||e.afterRow>=n.rows.length?{valid:!1,error:`Invalid afterRow ${e.afterRow} for table "${e.target}" with ${n.rows.length} rows`}:{valid:!0}:{valid:!1,error:`Table "${e.target}" not found`}}case`delete_table_row`:{let n=N(e.target,t);return n?e.row<0||e.row>=n.rows.length?{valid:!1,error:`Invalid row index ${e.row} for table "${e.target}" with ${n.rows.length} rows`}:{valid:!0}:{valid:!1,error:`Table "${e.target}" not found`}}case`apply_formatting`:return M(e.target,t)?e.find!==void 0&&e.find.length===0?{valid:!1,error:`Empty find string`}:e.find!==void 0&&e.find.length>0&&e.occurrence!==void 0&&(e.occurrence<1||!Number.isInteger(e.occurrence))?{valid:!1,error:`Invalid occurrence: ${e.occurrence} (must be a positive integer)`}:!e.formatting||!Object.values(e.formatting).some(e=>e!==void 0)?{valid:!1,error:`Empty formatting object`}:{valid:!0}:{valid:!1,error:`Paragraph "${e.target}" not found`};case`apply_style`:{if(!M(e.target,t))return{valid:!1,error:`Paragraph "${e.target}" not found`};if(e.find!==void 0&&e.find.length===0)return{valid:!1,error:`Empty find string`};if(e.find!==void 0&&e.find.length>0&&e.occurrence!==void 0&&(e.occurrence<1||!Number.isInteger(e.occurrence)))return{valid:!1,error:`Invalid occurrence: ${e.occurrence} (must be a positive integer)`};if(!e.style||e.style.length===0)return{valid:!1,error:`Empty style ID`};let n=t.styles.find(t=>t.id===e.style);if(!n)return{valid:!1,error:`Style "${e.style}" not found in document styles`};if(e.find!==void 0&&e.find.length>0){if(n.type!==`character`)return{valid:!1,error:`Style "${e.style}" is type "${n.type}", expected "character" when "find" is specified`}}else if(n.type!==`paragraph`)return{valid:!1,error:`Style "${e.style}" is type "${n.type}", expected "paragraph" when "find" is omitted`};return{valid:!0}}case`append_text`:if(!e.text||e.text.length===0)return{valid:!1,error:`Empty append text`};if(e.container){let n=ct(e.container,t);if(!n.valid)return n}return{valid:!0};case`add_table`:{if((!e.rows||e.rows.length===0)&&(!e.headers||e.headers.length===0))return{valid:!1,error:`Table must have at least one row or headers`};let n=e.headers?.length??(e.rows.length>0?e.rows[0].length:0);if(n===0)return{valid:!1,error:`Table must have at least one column`};for(let t=0;t<e.rows.length;t++)if(e.rows[t].length!==n)return{valid:!1,error:`Row ${t} has ${e.rows[t].length} cells, expected ${n}`};if(e.headers&&e.rows.length>0&&e.headers.length!==e.rows[0].length)return{valid:!1,error:`Headers have ${e.headers.length} cells, but rows have ${e.rows[0].length}`};if(e.afterId){if(e.container)return{valid:!1,error:`"afterId" is not supported when "container" is set — container-targeted tables are appended to the end of the header/footer`};if(!mt(e.afterId,t))return{valid:!1,error:`Element "${e.afterId}" not found`}}if(e.container){let n=ct(e.container,t);if(!n.valid)return n}return{valid:!0}}case`add_header`:case`add_footer`:return[`default`,`first`,`even`].includes(e.variant)?!e.paragraphs||e.paragraphs.length===0?{valid:!1,error:`At least one paragraph is required`}:{valid:!0}:{valid:!1,error:`Invalid variant: ${e.variant}`};case`remove_header`:case`remove_footer`:return[`default`,`first`,`even`].includes(e.variant)?{valid:!0}:{valid:!1,error:`Invalid variant: ${e.variant}`};case`add_list`:if(![`bullet`,`ordered`,`multilevel`].includes(e.listType))return{valid:!1,error:`Invalid listType: ${e.listType}`};if(!e.items||e.items.length===0)return{valid:!1,error:`items must be non-empty`};if(e.afterId&&e.container)return{valid:!1,error:`"afterId" is not supported when "container" is set`};if(e.afterId&&!mt(e.afterId,t))return{valid:!1,error:`Element "${e.afterId}" not found`};if(e.container){let n=ct(e.container,t);if(!n.valid)return n}if(e.numId!==void 0){if(!t.xmlParts.numbering)return{valid:!1,error:`numId "${e.numId}" specified but document has no numbering.xml`};let n=t.xmlParts.numbering.find(e=>f(e)===`w:numbering`);if(!(n&&m(n).some(t=>f(t)===`w:num`&&_(t,`w:numId`)===e.numId)))return{valid:!1,error:`numId "${e.numId}" not found in numbering.xml`}}return{valid:!0};case`add_list_item`:{let n=M(e.target,t);return n?n.listInfo?!e.text&&e.text!==``?{valid:!1,error:`text is required`}:[`before`,`after`].includes(e.position)?e.level!==void 0&&e.level<0?{valid:!1,error:`level must be >= 0`}:{valid:!0}:{valid:!1,error:`Invalid position: ${e.position}`}:{valid:!1,error:`Paragraph "${e.target}" is not a list item`}:{valid:!1,error:`Paragraph "${e.target}" not found`}}case`remove_list_item`:{let n=M(e.target,t);return n?n.listInfo?{valid:!0}:{valid:!1,error:`Paragraph "${e.target}" is not a list item`}:{valid:!1,error:`Paragraph "${e.target}" not found`}}case`convert_to_list`:if(![`bullet`,`ordered`,`multilevel`].includes(e.listType))return{valid:!1,error:`Invalid listType: ${e.listType}`};if(!e.targets||e.targets.length===0)return{valid:!1,error:`targets must be non-empty`};for(let n of e.targets)if(!M(n,t))return{valid:!1,error:`Paragraph "${n}" not found`};return{valid:!0};case`remove_list`:if(!e.targets||e.targets.length===0)return{valid:!1,error:`targets must be non-empty`};for(let n of e.targets){let e=M(n,t);if(!e)return{valid:!1,error:`Paragraph "${n}" not found`};if(!e.listInfo)return{valid:!1,error:`Paragraph "${n}" is not a list item`}}return{valid:!0};case`set_list_properties`:{let n=M(e.target,t);return n?n.listInfo?e.listType===void 0&&e.level===void 0?{valid:!1,error:`At least one of listType or level must be provided`}:e.listType!==void 0&&![`bullet`,`ordered`,`multilevel`].includes(e.listType)?{valid:!1,error:`Invalid listType: ${e.listType}`}:e.level!==void 0&&e.level<0?{valid:!1,error:`level must be >= 0`}:{valid:!0}:{valid:!1,error:`Paragraph "${e.target}" is not a list item`}:{valid:!1,error:`Paragraph "${e.target}" not found`}}default:return{valid:!1,error:`Unknown patch type: ${e.type}`}}}function ct(e,t){return[`header`,`footer`].includes(e.type)?[`default`,`first`,`even`].includes(e.variant)?[...(e.type===`header`?t.headers:t.footers).values()].some(t=>t.variant===e.variant)?{valid:!0}:{valid:!1,error:`No ${e.type} with variant "${e.variant}" found`}:{valid:!1,error:`Invalid container variant: ${e.variant}`}:{valid:!1,error:`Invalid container type: ${e.type}`}}function M(e,t){return e.startsWith(`para:`)?lt(e.slice(5),t):ut(t,t=>t.id===e)}function lt(e,t){let n=e.toUpperCase();return ut(t,e=>e.paraId===n)}function ut(e,t){for(let n of e.body){let e=ft(t,n);if(e)return e}for(let n of e.headers.values()){for(let e of n.paragraphs)if(t(e))return e;for(let e of n.tables){let n=pt(t,e);if(n)return n}}for(let n of e.footers.values()){for(let e of n.paragraphs)if(t(e))return e;for(let e of n.tables){let n=pt(t,e);if(n)return n}}}function N(e,t){for(let n of t.body)if(n.kind===`table`){let t=dt(e,n);if(t)return t}for(let n of t.headers.values())for(let t of n.tables){let n=dt(e,t);if(n)return n}for(let n of t.footers.values())for(let t of n.tables){let n=dt(e,t);if(n)return n}}function dt(e,t){if(t.id===e)return t;for(let n of t.rows)for(let t of n.cells)for(let n of t.nestedTables){let t=dt(e,n);if(t)return t}}function ft(e,t){if(t.kind===`paragraph`)return e(t)?t:void 0;if(t.kind===`table`)return pt(e,t)}function pt(e,t){for(let n of t.rows)for(let t of n.cells){for(let n of t.paragraphs)if(e(n))return n;for(let n of t.nestedTables){let t=pt(e,n);if(t)return t}}}function mt(e,t){if(e.startsWith(`para:`)){let n=e.slice(5).toUpperCase();return t.body.find(e=>e.kind===`paragraph`&&e.paraId===n)}for(let n of t.body)if(n.kind===`paragraph`&&n.id===e||n.kind===`table`&&n.id===e)return n}function P(e,t,n=1){if(t.length===0)return-1;let r=0,i=0;for(;i<=e.length-t.length;){let a=e.indexOf(t,i);if(a===-1)return-1;if(r++,r===n)return a;i=a+1}return-1}function F(e,t,n){let r=[];for(let i of e){if(i.endOffset<=t||i.startOffset>=n)continue;let e=Math.max(0,t-i.startOffset),a=Math.min(i.text.length,n-i.startOffset);r.push({run:i,localStart:e,localEnd:a,isFullRun:e===0&&a===i.text.length,isStartAligned:e===0,isEndAligned:a===i.text.length})}return r}function ht(e){if(`w:t`in e)return!0;if(`w:br`in e){let t=_(e,`w:type`);return!t||t===`textWrapping`}return!1}function I(e){let t={text:``,anchored:[]};for(let n of m(e))if(`w:rPr`in n)t.rPr=n;else if(`w:t`in n){let e=n[`w:t`];if(Array.isArray(e))for(let n of e)`#text`in n&&(t.text+=b(String(n[`#text`])))}else ht(n)?t.text+=`
7
- `:t.anchored.push({node:n,offset:t.text.length});return t}function gt(e,t){let n=m(e);n.length=0,t.rPr&&n.push(t.rPr);let r=[...t.anchored].sort((e,t)=>e.offset-t.offset),i=t.text,a=0,o=``,s=()=>{o.length>0&&n.push(de(o)),o=``};for(let e=0;e<i.length;e++){for(;a<r.length&&r[a].offset<=e;)s(),n.push(r[a].node),a++;let t=i[e];t===`
8
- `?(s(),n.push({"w:br":[]})):o+=t}for(s();a<r.length;)n.push(r[a].node),a++;i.length===0&&r.length===0&&n.push(de(``))}function L(e){let t={"w:r":[]};return gt(t,{rPr:e.rPr?x(e.rPr):void 0,text:e.text,anchored:e.anchored}),t}function _t(e,t,n,r=`before`){let i={rPr:e.rPr,text:e.text.slice(0,t),anchored:[]},a={rPr:e.rPr,text:e.text.slice(t,n),anchored:[]},o={rPr:e.rPr,text:e.text.slice(n),anchored:[]};for(let s of e.anchored)(r===`before`?s.offset<=t:s.offset<t)?i.anchored.push(s):s.offset>=n?o.anchored.push({node:s.node,offset:s.offset-n}):a.anchored.push({node:s.node,offset:s.offset-t});return{before:i,matched:a,after:o}}function vt(e){return e.text.length>0||e.anchored.length>0}function R(e,t){let n=m(e);return n.includes(t)?n:yt(n,t)}function yt(e,t){for(let n of e){let e=f(n);if(e===`w:hyperlink`||e===`w:ins`||e===`w:del`){let e=m(n);if(e.includes(t))return e;let r=yt(e,t);if(r)return r}}}function z(e,t){if(t.isFullRun)return{matched:t.run.xmlRef};let n=t.run.xmlRef,r=R(e,n);if(!r)throw Error(`Run not found in paragraph children or hyperlink children`);let i=r.indexOf(n),a=_t(I(n),t.localStart,t.localEnd),o={matched:void 0},s=[];if(vt(a.before)){let e=L(a.before);o.before=e,s.push(e)}let c=L(a.matched);if(o.matched=c,s.push(c),vt(a.after)){let e=L(a.after);o.after=e,s.push(e)}return r.splice(i,1,...s),o}function bt(e,t,n,r=`before`){let i=R(e,t);if(!i)throw Error(`Run not found in paragraph children or hyperlink children`);let a=i.indexOf(t),o=_t(I(t),n,n,r),s=[],c={parentChildren:i,index:a};return vt(o.before)&&(c.before=L(o.before),s.push(c.before),c.index=a+1),vt(o.after)&&(c.after=L(o.after),s.push(c.after)),i.splice(a,1,...s),c}function B(e,t){if(t.length===0)return[];if(t.length===1)return[z(e,t[0]).matched];let n=[],r=[],i=t[t.length-1];if(!i.isEndAligned){let n=z(e,i);r.push({index:t.length-1,split:n})}let a=t[0];if(!a.isStartAligned){let t=z(e,a);r.push({index:0,split:t})}for(let i=0;i<t.length;i++){let a=t[i],o=r.find(e=>e.index===i);if(o)n.push(o.split.matched);else{let t=R(e,a.run.xmlRef);t&&t.includes(a.run.xmlRef)&&n.push(a.run.xmlRef)}}return n}function xt(e,t){return{nextId:Ct(e)+1,author:t.revisionAuthor??`docxtor`,date:t.revisionDate??new Date().toISOString()}}function V(e){let t=String(e.nextId);return e.nextId++,t}const St=new Set([`w:ins`,`w:del`,`w:rPrChange`,`w:pPrChange`,`w:trPrChange`,`w:tblPrChange`,`w:sectPrChange`,`w:cellIns`,`w:cellDel`,`w:cellMerge`]);function Ct(e){let t=0;t=Math.max(t,wt(e.xmlParts.document));for(let n of e.xmlParts.headers.values())t=Math.max(t,wt(n));for(let n of e.xmlParts.footers.values())t=Math.max(t,wt(n));for(let n of[`footnotes`,`endnotes`,`comments`]){let r=e.xmlParts[n];r&&(t=Math.max(t,wt(r)))}return t}function wt(e){let t=0;for(let n of e){let e=f(n);if(e&&St.has(e)){let e=_(n,`w:id`);if(e){let n=parseInt(e,10);!isNaN(n)&&n>t&&(t=n)}}if(e){let e=m(n);e.length>0&&(t=Math.max(t,wt(e)))}}return t}function H(e,t){return C(`w:ins`,t,{"w:id":V(e),"w:author":e.author,"w:date":e.date})}function Tt(e,t){return C(`w:del`,t,{"w:id":V(e),"w:author":e.author,"w:date":e.date})}function Et(e,t){let n=V(e);return C(`w:rPrChange`,[x(t)],{"w:id":n,"w:author":e.author,"w:date":e.date})}function U(e,t){let n=V(e);return C(`w:pPrChange`,[x(t)],{"w:id":n,"w:author":e.author,"w:date":e.date})}function Dt(e){return C(`w:ins`,[],{"w:id":V(e),"w:author":e.author,"w:date":e.date})}function Ot(e){return C(`w:del`,[],{"w:id":V(e),"w:author":e.author,"w:date":e.date})}function kt(e){let t=m(e);for(let e=0;e<t.length;e++){let n=t[e];if(`w:t`in n){let r=n[`w:t`],i=n[`:@`],a={"w:delText":r};i&&(a[`:@`]=i),t[e]=a}else if(`w:instrText`in n){let r={"w:delInstrText":n[`w:instrText`]};n[`:@`]&&(r[`:@`]=n[`:@`]),t[e]=r}}}function At(e,t){let n=[];if(t&&n.push(x(t)),e.includes(`
4
+ `);for(let e=0;e<t.length;e++){let r=t[e];(r.length>0||e===0)&&n.push(de(r)),e<t.length-1&&n.push({"w:br":[]})}}else n.push(de(e));return{"w:r":n}}function pe(e){let t=m(e),n=[];for(let e of t){let t=f(e);t===`w:r`?n.push(e):(t===`w:hyperlink`||t===`w:ins`)&&me(e,n)}return n}function me(e,t){let n=m(e);for(let e of n){let n=f(e);n===`w:r`?t.push(e):(n===`w:hyperlink`||n===`w:ins`)&&me(e,t)}}function T(e,t){let n=pe(e),r=[],i=0,a;for(let e of n){let t=re(e);if(a===void 0&&ae(e)){let t=oe(e);t>=0&&(a=i+t)}if(t.length===0)continue;let n=ie(e),o=he(e),s={text:t,startOffset:i,endOffset:i+t.length,xmlRef:e,formatting:o};n&&(s.textXmlRef=n),r.push(s),i+=t.length}let o=r.map(e=>e.text).join(``),s=ve(e,r),c={kind:`paragraph`,id:t,text:o,runs:r,xmlRef:e,style:ge(e),listInfo:_e(e)},l=_(e,`w14:paraId`);if(l&&(c.paraId=l.toUpperCase()),s.length>0&&(c.revisions=s),se(e)){c.hasPageBreak=!0;let t=h(e,`w:pPr`);if(t){let e=h(t,`w:pageBreakBefore`);if(e){let t=_(e,`w:val`);if(t!==`0`&&t!==`false`)return c.pageBreakOffset=0,c}}a!==void 0&&(c.pageBreakOffset=a)}return c}function he(e){let t=h(e,`w:rPr`);if(!t)return{};let n={},r=m(t);for(let e of r)if(p(e,`w:b`)){let t=_(e,`w:val`);n.bold=t!==`0`&&t!==`false`}else if(p(e,`w:i`)){let t=_(e,`w:val`);n.italic=t!==`0`&&t!==`false`}else if(p(e,`w:u`))n.underline=_(e,`w:val`)??`single`;else if(p(e,`w:strike`)){let t=_(e,`w:val`);n.strikethrough=t!==`0`&&t!==`false`}else if(p(e,`w:sz`)){let t=_(e,`w:val`);t&&(n.fontSize=parseInt(t,10))}else if(p(e,`w:rFonts`))n.fontFamily=_(e,`w:ascii`)??_(e,`w:hAnsi`);else if(p(e,`w:color`)){n.color=_(e,`w:val`);let t=_(e,`w:themeColor`);t&&(n.themeColor=t)}else p(e,`w:highlight`)&&(n.highlight=_(e,`w:val`));return n}function ge(e){let t=h(e,`w:pPr`);if(!t)return;let n=h(t,`w:pStyle`);if(n)return _(n,`w:val`)}function _e(e){let t=h(e,`w:pPr`);if(!t)return;let n=h(t,`w:numPr`);if(!n)return;let r=h(n,`w:numId`),i=h(n,`w:ilvl`),a=r?_(r,`w:val`):void 0,o=i?_(i,`w:val`):void 0;if(a&&a!==`0`)return{numId:a,level:o?parseInt(o,10):0}}function ve(e,t){let n=[],r=new Set;ye(e,r);for(let i of t)if(r.has(i.xmlRef)){let t=xe(e,i.xmlRef);n.push({type:`insert`,text:i.text,startOffset:i.startOffset,endOffset:i.endOffset,author:t?.author??``,date:t?.date??``,...t?.id===void 0?{}:{id:t.id}})}return we(e,t,n),n}function ye(e,t){let n=m(e);for(let e of n){let n=f(e);n===`w:ins`?be(e,t):n===`w:hyperlink`&&ye(e,t)}}function be(e,t){let n=m(e);for(let e of n){let n=f(e);n===`w:r`?t.add(e):(n===`w:hyperlink`||n===`w:ins`)&&be(e,t)}}function xe(e,t){return Se(m(e),t)}function Se(e,t){for(let n of e){let e=f(n);if(e===`w:ins`){if(Ce(n,t))return{author:_(n,`w:author`)??``,date:_(n,`w:date`)??``,id:_(n,`w:id`)}}else if(e===`w:hyperlink`){let e=Se(m(n),t);if(e)return e}}}function Ce(e,t){let n=m(e);for(let e of n){if(e===t)return!0;let n=f(e);if(n&&n!==`w:del`&&Ce(e,t))return!0}return!1}function we(e,t,n){let r=new Map;for(let e of t)r.set(e.xmlRef,e);Te(m(e),r,n,0)}function Te(e,t,n,r){for(let i of e){let e=f(i);if(e===`w:r`){let e=t.get(i);e&&(r=e.endOffset)}else if(e===`w:del`){let e=Ee(i);e.length>0&&n.push({type:`delete`,text:e,startOffset:r,endOffset:r,author:_(i,`w:author`)??``,date:_(i,`w:date`)??``,..._(i,`w:id`)===void 0?{}:{id:_(i,`w:id`)}})}else (e===`w:hyperlink`||e===`w:ins`)&&(r=Te(m(i),t,n,r))}return r}function Ee(e){let t=m(e),n=``;for(let e of t)if(f(e)===`w:r`){let t=m(e);for(let e of t)if(`w:br`in e){let t=_(e,`w:type`);(!t||t===`textWrapping`)&&(n+=`
5
+ `)}else if(`w:delText`in e){let t=e[`w:delText`];if(Array.isArray(t))for(let e of t)`#text`in e&&(n+=b(String(e[`#text`])))}else if(`w:t`in e){let t=e[`w:t`];if(Array.isArray(t))for(let e of t)`#text`in e&&(n+=b(String(e[`#text`])))}}return n}function E(e,t){let n=[],r=g(e,`w:tr`);for(let e=0;e<r.length;e++){let i=r[e],a=[],o=g(i,`w:tc`);for(let n=0;n<o.length;n++){let r=o[n],i=g(r,`w:p`).map((r,i)=>T(r,`${t}.r${e}.c${n}.p${i}`)),s=g(r,`w:tbl`).map((r,i)=>E(r,`${t}.r${e}.c${n}.tbl${i}`));a.push({rowIndex:e,colIndex:n,paragraphs:i,nestedTables:s,xmlRef:r})}n.push({index:e,cells:a,xmlRef:i})}let i=h(e,`w:tblPr`),a=i?h(i,`w:tblStyle`):void 0;return{kind:`table`,id:t,rows:n,xmlRef:e,style:a?_(a,`w:val`):void 0}}function De(e,t,n,r){let i=t===`header`?`hdr`:`ftr`,a=t===`header`?`w:hdr`:`w:ftr`,o=e.find(e=>f(e)===a),s=[],c=[];if(o){let e=m(o),t=0,r=0;for(let a of e){let e=f(a);e===`w:p`?(s.push(T(a,`${i}.${n}.p${t}`)),t++):e===`w:tbl`&&(c.push(E(a,`${i}.${n}.tbl${r}`)),r++)}}return{type:t,variant:n,rId:r,paragraphs:s,tables:c,xmlRef:e}}function Oe(e){if(e.xmlParts.numbering)return e.xmlParts.numbering;let t=C(`w:numbering`,[],{"xmlns:w":`http://schemas.openxmlformats.org/wordprocessingml/2006/main`,"xmlns:r":`http://schemas.openxmlformats.org/officeDocument/2006/relationships`});if(e.xmlParts.numbering=[t],e.xmlParts.contentTypes){let t=e.xmlParts.contentTypes.find(e=>f(e)===`Types`);if(t){let e=m(t);e.some(e=>f(e)===`Override`&&_(e,`PartName`)===`/word/numbering.xml`)||e.push(C(`Override`,[],{PartName:`/word/numbering.xml`,ContentType:`application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml`}))}}if(e.xmlParts.documentRels){let t=e.xmlParts.documentRels.find(e=>f(e)===`Relationships`);if(t){let e=m(t);if(!e.some(e=>_(e,`Type`)===`http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering`)){let t=0;for(let n of e){let e=_(n,`Id`);if(e){let n=parseInt(e.replace(`rId`,``),10);!isNaN(n)&&n>t&&(t=n)}}e.push(C(`Relationship`,[],{Id:`rId${t+1}`,Type:`http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering`,Target:`numbering.xml`}))}}}return e.xmlParts.numbering}function ke(e){let t=e.find(e=>f(e)===`w:numbering`);if(!t)return 1;let n=0;for(let e of g(t,`w:num`)){let t=_(e,`w:numId`);if(t){let e=parseInt(t,10);!isNaN(e)&&e>n&&(n=e)}}return n+1}function Ae(e){let t=e.find(e=>f(e)===`w:numbering`);if(!t)return 0;let n=-1;for(let e of g(t,`w:abstractNum`)){let t=_(e,`w:abstractNumId`);if(t){let e=parseInt(t,10);!isNaN(e)&&e>n&&(n=e)}}return n+1}const je=[{char:``,font:`Symbol`},{char:`o`,font:`Courier New`},{char:``,font:`Wingdings`},{char:``,font:`Symbol`},{char:`o`,font:`Courier New`},{char:``,font:`Wingdings`},{char:``,font:`Symbol`},{char:`o`,font:`Courier New`},{char:``,font:`Wingdings`}],Me=[{fmt:`decimal`,textFn:e=>`%${e+1}.`},{fmt:`lowerLetter`,textFn:e=>`%${e+1}.`},{fmt:`lowerRoman`,textFn:e=>`%${e+1}.`}];function Ne(e,t,n=9){let r=Oe(e),i=m(r.find(e=>f(e)===`w:numbering`)),a=Ae(r),o=ke(r),s=[];for(let e=0;e<n;e++){let n,r;if(t===`bullet`){n=`bullet`;let t=je[e%je.length];r=t.char;let i=C(`w:rPr`,[C(`w:rFonts`,[],{"w:ascii":t.font,"w:hAnsi":t.font,"w:hint":`default`})]);s.push(C(`w:lvl`,[C(`w:start`,[],{"w:val":`1`}),C(`w:numFmt`,[],{"w:val":n}),C(`w:lvlText`,[],{"w:val":r}),C(`w:lvlJc`,[],{"w:val":`left`}),i],{"w:ilvl":String(e)}));continue}else if(t===`multilevel`)n=`decimal`,r=Array.from({length:e+1},(e,t)=>`%${t+1}`).join(`.`)+`.`;else{let t=Me[e%Me.length];n=t.fmt,r=t.textFn(e)}s.push(C(`w:lvl`,[C(`w:start`,[],{"w:val":`1`}),C(`w:numFmt`,[],{"w:val":n}),C(`w:lvlText`,[],{"w:val":r}),C(`w:lvlJc`,[],{"w:val":`left`})],{"w:ilvl":String(e)}))}let c=C(`w:abstractNum`,s,{"w:abstractNumId":String(a)}),l=C(`w:num`,[C(`w:abstractNumId`,[],{"w:val":String(a)})],{"w:numId":String(o)}),u=i.findIndex(e=>f(e)===`w:num`);return u>=0?i.splice(u,0,c):i.push(c),i.push(l),String(o)}function Pe(e,t){let n=e.find(e=>f(e)===`w:numbering`);if(!n)return;let r=g(n,`w:num`).find(e=>_(e,`w:numId`)===t);if(!r)return;let i=h(r,`w:abstractNumId`);if(!i)return;let a=_(i,`w:val`);if(!a)return;let o=g(n,`w:abstractNum`).find(e=>_(e,`w:abstractNumId`)===a);if(!o)return;let s=g(o,`w:lvl`),c=!1,l=!1;for(let e of s){let t=h(e,`w:numFmt`);t&&_(t,`w:val`)===`bullet`&&(c=!0);let n=h(e,`w:lvlText`);if(n){let e=(_(n,`w:val`)??``).match(/%\d+/g);e&&e.length>1&&(l=!0)}}return c?`bullet`:l?`multilevel`:`ordered`}function Fe(e,t,n){let r=e.find(e=>f(e)===`w:numbering`);if(!r)return null;let i=g(r,`w:num`).find(e=>_(e,`w:numId`)===t);if(!i)return null;let a=h(i,`w:abstractNumId`);if(!a)return null;let o=_(a,`w:val`);if(!o)return null;let s=g(r,`w:abstractNum`).find(e=>_(e,`w:abstractNumId`)===o);if(!s)return null;let c=g(s,`w:lvl`),l=c.find(e=>_(e,`w:ilvl`)===String(n));if(!l)return null;let u=h(l,`w:numFmt`),d=h(l,`w:lvlText`),p=u?_(u,`w:val`)??`decimal`:`decimal`,m=d?_(d,`w:val`)??``:``,v=new Map,ee=m.match(/%(\d+)/g);if(ee)for(let e of ee){let t=parseInt(e.slice(1),10)-1,n=c.find(e=>_(e,`w:ilvl`)===String(t));if(n){let e=h(n,`w:numFmt`);v.set(t,e?_(e,`w:val`)??`decimal`:`decimal`)}}return{pattern:m,format:p,ancestorFormats:v}}const Ie=new Set([`w:document`,`w:hdr`,`w:ftr`,`w:footnotes`,`w:endnotes`,`w:comments`]),Le=new WeakMap;function Re(e){let t=Le.get(e.xmlParts);if(!t){t=new Set;for(let n of ze(e.xmlParts))Be(n,t);Le.set(e.xmlParts,t)}return t}function*ze(e){for(let t of Object.values(e))if(Array.isArray(t))yield t;else if(t instanceof Map)for(let e of t.values())yield e}function Be(e,t){for(let n of e){let e=f(n);if(!e)continue;if(e===`w:p`){let e=_(n,`w14:paraId`);e&&t.add(e.toUpperCase())}let r=m(n);r.length>0&&Be(r,t)}}function Ve(e){for(;;){let t=(Math.floor(Math.random()*2147483646)+1).toString(16).toUpperCase().padStart(8,`0`);if(!e.has(t))return e.add(t),t}}function He(e){if(!/^[0-9A-Fa-f]{8}$/.test(e))return!1;let t=parseInt(e,16);return t>0&&t<2147483648}function Ue(e){return e?_(e,`xmlns:w14`)!==void 0:!1}function We(e){_(e,`xmlns:w14`)===void 0&&v(e,`xmlns:w14`,`http://schemas.microsoft.com/office/word/2010/wordml`),_(e,`xmlns:mc`)===void 0&&v(e,`xmlns:mc`,`http://schemas.openxmlformats.org/markup-compatibility/2006`);let t=_(e,`mc:Ignorable`);if(t===void 0)v(e,`mc:Ignorable`,`w14`);else{let n=t.split(/\s+/).filter(e=>e.length>0);n.includes(`w14`)||(n.push(`w14`),v(e,`mc:Ignorable`,n.join(` `)))}}function Ge(e){return e.find(e=>{let t=f(e);return t!==void 0&&Ie.has(t)})}function Ke(e,t){let n=Ge(e);if(!n)return 0;let r=0,i=e=>{for(let n of e){let e=f(n);if(!e)continue;if(e===`w:p`){let e=_(n,`w14:paraId`);if(!e||!He(e)){let e=Ve(t);v(n,`w14:paraId`,e),r++,_(n,`w14:textId`)===void 0&&v(n,`w14:textId`,e)}else _(n,`w14:textId`)===void 0&&v(n,`w14:textId`,e.toUpperCase())}let a=m(n);a.length>0&&i(a)}};return i(m(n)),r>0&&We(n),r}function qe(e){let t=new Set;for(let n of ze(e))Be(n,t);Ke(e.document,t);for(let n of e.headers.values())Ke(n,t);for(let n of e.footers.values())Ke(n,t);Le.set(e,t)}function D(e,t,n,r=!1){if(!Ue(n))return;let i=Re(e),a=e=>{for(let t of e){let e=f(t);if(!e)continue;if(e===`w:p`&&(r||_(t,`w14:paraId`)===void 0)){let e=Ve(i);v(t,`w14:paraId`,e),v(t,`w14:textId`,e)}let n=m(t);n.length>0&&a(n)}};a(t)}function O(e,t){let n=t.match(/^(hdr|ftr)\.([^.]+)\./);if(n){let t=n[1]===`hdr`?e.headers:e.footers,r=n[1]===`hdr`?`w:hdr`:`w:ftr`;for(let e of t.values())if(e.variant===n[2])return e.xmlRef.find(e=>f(e)===r);return}return e.xmlParts.document.find(e=>f(e)===`w:document`)}function Je(e,t){let n=d(e.documentXml),r=new Map;for(let[t,n]of e.headers)r.set(t,d(n));let i=new Map;for(let[t,n]of e.footers)i.set(t,d(n));let a=e.stylesXml?Qe(e.stylesXml):[],o=e=>e?d(e):void 0,s={document:n,headers:r,footers:i,documentRels:o(e.documentRels),contentTypes:o(e.contentTypes),settings:o(e.settingsXml),numbering:o(e.numberingXml),footnotes:o(e.footnotesXml),endnotes:o(e.endnotesXml),comments:o(e.commentsXml),core:o(e.coreXml),app:o(e.appXml),rootRels:o(e.rootRels)};t?.assignParaIds&&qe(s);let c={body:[],headers:new Map,footers:new Map,xmlParts:s,styles:a,loadOptions:t};return k(c),c}function k(e){let t=e.loadOptions?.includeHeadersFooters??!0;if(e.body=Ye(e.xmlParts.document),e.headers=new Map,e.footers=new Map,t){let t=Xe(e.xmlParts.documentRels),n=Ze(e.xmlParts.document);for(let r of n){let n=t.get(r.rId);if(!n)continue;let i=`word/${n}`;if(r.type===`header`){let t=e.xmlParts.headers.get(i);t&&e.headers.set(r.rId,De(t,`header`,r.variant,r.rId))}else{let t=e.xmlParts.footers.get(i);t&&e.footers.set(r.rId,De(t,`footer`,r.variant,r.rId))}}}$e(e)}function Ye(e){let t=[],n=e.find(e=>f(e)===`w:document`);if(!n)return t;let r=h(n,`w:body`);if(!r)return t;let i=m(r),a=0,o=0,s=0;for(let e of i){let n=f(e);if(n===`w:p`){let n=T(e,`p${a}`);if(n.hasPageBreak&&n.text.trim().length===0){let n={kind:`page_break`,id:`pb${s}`,xmlRef:e};t.push(n),s++;continue}t.push(n),a++}else n===`w:tbl`&&(t.push(E(e,`tbl${o}`)),o++)}return t}function Xe(e){let t=new Map;if(!e)return t;let n=e.find(e=>f(e)===`Relationships`);if(!n)return t;let r=g(n,`Relationship`);for(let e of r){let n=_(e,`Id`),r=_(e,`Target`);n&&r&&t.set(n,r)}return t}function Ze(e){let t=[],n=e.find(e=>f(e)===`w:document`);if(!n)return t;let r=h(n,`w:body`);if(!r)return t;let i=g(r,`w:sectPr`),a=m(r);for(let e of a)if(f(e)===`w:p`){let t=h(e,`w:pPr`);if(t){let e=h(t,`w:sectPr`);e&&i.push(e)}}for(let e of i){let n=m(e);for(let e of n){let n=f(e);if(n===`w:headerReference`){let n=_(e,`r:id`),r=_(e,`w:type`)??`default`;n&&t.push({type:`header`,variant:r,rId:n})}else if(n===`w:footerReference`){let n=_(e,`r:id`),r=_(e,`w:type`)??`default`;n&&t.push({type:`footer`,variant:r,rId:n})}}}return t}function Qe(e){let t=d(e),n=[],r=t.find(e=>f(e)===`w:styles`);if(!r)return n;let i=g(r,`w:style`);for(let e of i){let t=_(e,`w:styleId`),r=_(e,`w:type`);if(!t||!r||![`paragraph`,`character`,`table`,`numbering`].includes(r))continue;let i=h(e,`w:name`),a=i?_(i,`w:val`)??t:t;n.push({id:t,name:a,type:r})}return n}function $e(e){let t=e.xmlParts.numbering;if(t){for(let n of e.body)if(n.kind===`paragraph`)A(n,t);else if(n.kind===`table`)for(let e of n.rows)for(let n of e.cells)for(let e of n.paragraphs)A(e,t);for(let n of e.headers.values())for(let e of n.paragraphs)A(e,t);for(let n of e.footers.values())for(let e of n.paragraphs)A(e,t)}}function A(e,t){if(!e.listInfo)return;let n=Pe(t,e.listInfo.numId);n&&(e.listInfo.listType=n)}function et(e){let t=e.split(`
6
+ `),n=[],r=0;for(;r<t.length;){if(r+1<t.length&&at(t[r])&&ot(t[r+1])){let e=st(t[r]);r+=2;let i=[];for(;r<t.length&&at(t[r]);){let n=st(t[r]);for(;n.length<e.length;)n.push(``);n.length>e.length&&(n.length=e.length),i.push(n),r++}n.push({kind:`table`,headers:e,rows:i});continue}let e=it(t[r]);if(e){let i=[],a=e.listType;for(i.push({text:e.text,level:e.level}),r++;r<t.length;){let e=t[r];if(e.trim().length===0){r++;continue}let n=it(e);if(!n||n.listType!==a)break;i.push({text:n.text,level:n.level}),r++}n.push({kind:`list`,listType:a,items:i});continue}let i=n[n.length-1];i&&i.kind===`text`?i.lines.push(t[r]):n.push({kind:`text`,lines:[t[r]]}),r++}return n}function tt(e){return et(e).some(e=>e.kind!==`text`)}const nt=/^(\s*)[*+-]\s(.*)$/,rt=/^(\s*)\d+\.\s(.*)$/;function it(e){let t=e.replace(/\t/g,` `),n=nt.exec(t);if(n){let e=n[1].length;return{listType:`bullet`,text:n[2],level:Math.floor(e/2)}}if(n=rt.exec(t),n){let e=n[1].length;return{listType:`ordered`,text:n[2],level:Math.floor(e/2)}}}function at(e){let t=e.trim();return t.startsWith(`|`)&&t.endsWith(`|`)&&t.length>1}function ot(e){let t=e.trim();return!t.startsWith(`|`)||!t.endsWith(`|`)?!1:t.slice(1,-1).split(`|`).every(e=>/^\s*:?-+:?\s*$/.test(e))}function st(e){let t=e.trim(),n=t.startsWith(`|`)?t.slice(1):t;return(n.endsWith(`|`)?n.slice(0,-1):n).split(`|`).map(e=>e.trim())}function ct(e,t){switch(e.type){case`insert_paragraph`:return j(e.target,t)?!e.text||e.text.length===0?{valid:!1,error:`Empty paragraph text`}:[`before`,`after`].includes(e.position)?tt(e.text)?{valid:!1,error:`Markdown tables and lists are not supported in insert_paragraph; use add_table or add_list with afterId`}:{valid:!0}:{valid:!1,error:`Invalid position: ${e.position}`}:{valid:!1,error:`Paragraph "${e.target}" not found`};case`delete_paragraph`:return j(e.target,t)?{valid:!0}:{valid:!1,error:`Paragraph "${e.target}" not found`};case`add_comment`:return j(e.target,t)?!e.text||e.text.length===0?{valid:!1,error:`Empty comment text`}:e.find!==void 0&&e.find.length===0?{valid:!1,error:`Empty find string`}:e.find!==void 0&&e.occurrence!==void 0&&(e.occurrence<1||!Number.isInteger(e.occurrence))?{valid:!1,error:`Invalid occurrence: ${e.occurrence} (must be a positive integer)`}:{valid:!0}:{valid:!1,error:`Paragraph "${e.target}" not found`};case`replace_text`:case`insert_text`:case`delete_text`:case`replace_cell_text`:return j(e.target,t)?`find`in e&&e.find.length===0?{valid:!1,error:`Empty find string`}:e.type===`insert_text`&&e.text.length===0?{valid:!1,error:`Empty insert text`}:`occurrence`in e&&e.occurrence!==void 0&&(e.occurrence<1||!Number.isInteger(e.occurrence))?{valid:!1,error:`Invalid occurrence: ${e.occurrence} (must be a positive integer)`}:{valid:!0}:{valid:!1,error:`Paragraph "${e.target}" not found`};case`add_table_row`:{let n=M(e.target,t);return n?e.cells.length===0?{valid:!1,error:`Empty cells array`}:e.afterRow<-1||e.afterRow>=n.rows.length?{valid:!1,error:`Invalid afterRow ${e.afterRow} for table "${e.target}" with ${n.rows.length} rows`}:{valid:!0}:{valid:!1,error:`Table "${e.target}" not found`}}case`delete_table_row`:{let n=M(e.target,t);return n?e.row<0||e.row>=n.rows.length?{valid:!1,error:`Invalid row index ${e.row} for table "${e.target}" with ${n.rows.length} rows`}:{valid:!0}:{valid:!1,error:`Table "${e.target}" not found`}}case`apply_formatting`:return j(e.target,t)?e.find!==void 0&&e.find.length===0?{valid:!1,error:`Empty find string`}:e.find!==void 0&&e.find.length>0&&e.occurrence!==void 0&&(e.occurrence<1||!Number.isInteger(e.occurrence))?{valid:!1,error:`Invalid occurrence: ${e.occurrence} (must be a positive integer)`}:!e.formatting||!Object.values(e.formatting).some(e=>e!==void 0)?{valid:!1,error:`Empty formatting object`}:{valid:!0}:{valid:!1,error:`Paragraph "${e.target}" not found`};case`apply_style`:{if(!j(e.target,t))return{valid:!1,error:`Paragraph "${e.target}" not found`};if(e.find!==void 0&&e.find.length===0)return{valid:!1,error:`Empty find string`};if(e.find!==void 0&&e.find.length>0&&e.occurrence!==void 0&&(e.occurrence<1||!Number.isInteger(e.occurrence)))return{valid:!1,error:`Invalid occurrence: ${e.occurrence} (must be a positive integer)`};if(!e.style||e.style.length===0)return{valid:!1,error:`Empty style ID`};let n=t.styles.find(t=>t.id===e.style);if(!n)return{valid:!1,error:`Style "${e.style}" not found in document styles`};if(e.find!==void 0&&e.find.length>0){if(n.type!==`character`)return{valid:!1,error:`Style "${e.style}" is type "${n.type}", expected "character" when "find" is specified`}}else if(n.type!==`paragraph`)return{valid:!1,error:`Style "${e.style}" is type "${n.type}", expected "paragraph" when "find" is omitted`};return{valid:!0}}case`append_text`:if(!e.text||e.text.length===0)return{valid:!1,error:`Empty append text`};if(e.container){let n=lt(e.container,t);if(!n.valid)return n}return{valid:!0};case`add_table`:{if((!e.rows||e.rows.length===0)&&(!e.headers||e.headers.length===0))return{valid:!1,error:`Table must have at least one row or headers`};let n=e.headers?.length??(e.rows.length>0?e.rows[0].length:0);if(n===0)return{valid:!1,error:`Table must have at least one column`};for(let t=0;t<e.rows.length;t++)if(e.rows[t].length!==n)return{valid:!1,error:`Row ${t} has ${e.rows[t].length} cells, expected ${n}`};if(e.headers&&e.rows.length>0&&e.headers.length!==e.rows[0].length)return{valid:!1,error:`Headers have ${e.headers.length} cells, but rows have ${e.rows[0].length}`};if(e.afterId){if(e.container)return{valid:!1,error:`"afterId" is not supported when "container" is set — container-targeted tables are appended to the end of the header/footer`};if(!ht(e.afterId,t))return{valid:!1,error:`Element "${e.afterId}" not found`}}if(e.container){let n=lt(e.container,t);if(!n.valid)return n}return{valid:!0}}case`add_header`:case`add_footer`:return[`default`,`first`,`even`].includes(e.variant)?!e.paragraphs||e.paragraphs.length===0?{valid:!1,error:`At least one paragraph is required`}:{valid:!0}:{valid:!1,error:`Invalid variant: ${e.variant}`};case`remove_header`:case`remove_footer`:return[`default`,`first`,`even`].includes(e.variant)?{valid:!0}:{valid:!1,error:`Invalid variant: ${e.variant}`};case`add_list`:if(![`bullet`,`ordered`,`multilevel`].includes(e.listType))return{valid:!1,error:`Invalid listType: ${e.listType}`};if(!e.items||e.items.length===0)return{valid:!1,error:`items must be non-empty`};if(e.afterId&&e.container)return{valid:!1,error:`"afterId" is not supported when "container" is set`};if(e.afterId&&!ht(e.afterId,t))return{valid:!1,error:`Element "${e.afterId}" not found`};if(e.container){let n=lt(e.container,t);if(!n.valid)return n}if(e.numId!==void 0){if(!t.xmlParts.numbering)return{valid:!1,error:`numId "${e.numId}" specified but document has no numbering.xml`};let n=t.xmlParts.numbering.find(e=>f(e)===`w:numbering`);if(!(n&&m(n).some(t=>f(t)===`w:num`&&_(t,`w:numId`)===e.numId)))return{valid:!1,error:`numId "${e.numId}" not found in numbering.xml`}}return{valid:!0};case`add_list_item`:{let n=j(e.target,t);return n?n.listInfo?!e.text&&e.text!==``?{valid:!1,error:`text is required`}:[`before`,`after`].includes(e.position)?e.level!==void 0&&e.level<0?{valid:!1,error:`level must be >= 0`}:{valid:!0}:{valid:!1,error:`Invalid position: ${e.position}`}:{valid:!1,error:`Paragraph "${e.target}" is not a list item`}:{valid:!1,error:`Paragraph "${e.target}" not found`}}case`remove_list_item`:{let n=j(e.target,t);return n?n.listInfo?{valid:!0}:{valid:!1,error:`Paragraph "${e.target}" is not a list item`}:{valid:!1,error:`Paragraph "${e.target}" not found`}}case`convert_to_list`:if(![`bullet`,`ordered`,`multilevel`].includes(e.listType))return{valid:!1,error:`Invalid listType: ${e.listType}`};if(!e.targets||e.targets.length===0)return{valid:!1,error:`targets must be non-empty`};for(let n of e.targets)if(!j(n,t))return{valid:!1,error:`Paragraph "${n}" not found`};return{valid:!0};case`remove_list`:if(!e.targets||e.targets.length===0)return{valid:!1,error:`targets must be non-empty`};for(let n of e.targets){let e=j(n,t);if(!e)return{valid:!1,error:`Paragraph "${n}" not found`};if(!e.listInfo)return{valid:!1,error:`Paragraph "${n}" is not a list item`}}return{valid:!0};case`set_list_properties`:{let n=j(e.target,t);return n?n.listInfo?e.listType===void 0&&e.level===void 0?{valid:!1,error:`At least one of listType or level must be provided`}:e.listType!==void 0&&![`bullet`,`ordered`,`multilevel`].includes(e.listType)?{valid:!1,error:`Invalid listType: ${e.listType}`}:e.level!==void 0&&e.level<0?{valid:!1,error:`level must be >= 0`}:{valid:!0}:{valid:!1,error:`Paragraph "${e.target}" is not a list item`}:{valid:!1,error:`Paragraph "${e.target}" not found`}}default:return{valid:!1,error:`Unknown patch type: ${e.type}`}}}function lt(e,t){return[`header`,`footer`].includes(e.type)?[`default`,`first`,`even`].includes(e.variant)?[...(e.type===`header`?t.headers:t.footers).values()].some(t=>t.variant===e.variant)?{valid:!0}:{valid:!1,error:`No ${e.type} with variant "${e.variant}" found`}:{valid:!1,error:`Invalid container variant: ${e.variant}`}:{valid:!1,error:`Invalid container type: ${e.type}`}}function j(e,t){return e.startsWith(`para:`)?ut(e.slice(5),t):dt(t,t=>t.id===e)}function ut(e,t){let n=e.toUpperCase();return dt(t,e=>e.paraId===n)}function dt(e,t){for(let n of e.body){let e=pt(t,n);if(e)return e}for(let n of e.headers.values()){for(let e of n.paragraphs)if(t(e))return e;for(let e of n.tables){let n=mt(t,e);if(n)return n}}for(let n of e.footers.values()){for(let e of n.paragraphs)if(t(e))return e;for(let e of n.tables){let n=mt(t,e);if(n)return n}}}function M(e,t){for(let n of t.body)if(n.kind===`table`){let t=ft(e,n);if(t)return t}for(let n of t.headers.values())for(let t of n.tables){let n=ft(e,t);if(n)return n}for(let n of t.footers.values())for(let t of n.tables){let n=ft(e,t);if(n)return n}}function ft(e,t){if(t.id===e)return t;for(let n of t.rows)for(let t of n.cells)for(let n of t.nestedTables){let t=ft(e,n);if(t)return t}}function pt(e,t){if(t.kind===`paragraph`)return e(t)?t:void 0;if(t.kind===`table`)return mt(e,t)}function mt(e,t){for(let n of t.rows)for(let t of n.cells){for(let n of t.paragraphs)if(e(n))return n;for(let n of t.nestedTables){let t=mt(e,n);if(t)return t}}}function ht(e,t){if(e.startsWith(`para:`)){let n=e.slice(5).toUpperCase();return t.body.find(e=>e.kind===`paragraph`&&e.paraId===n)}for(let n of t.body)if(n.kind===`paragraph`&&n.id===e||n.kind===`table`&&n.id===e)return n}function N(e,t,n=1){if(t.length===0)return-1;let r=0,i=0;for(;i<=e.length-t.length;){let a=e.indexOf(t,i);if(a===-1)return-1;if(r++,r===n)return a;i=a+1}return-1}function P(e,t,n){let r=[];for(let i of e){if(i.endOffset<=t||i.startOffset>=n)continue;let e=Math.max(0,t-i.startOffset),a=Math.min(i.text.length,n-i.startOffset);r.push({run:i,localStart:e,localEnd:a,isFullRun:e===0&&a===i.text.length,isStartAligned:e===0,isEndAligned:a===i.text.length})}return r}function gt(e){if(`w:t`in e)return!0;if(`w:br`in e){let t=_(e,`w:type`);return!t||t===`textWrapping`}return!1}function F(e){let t={text:``,anchored:[]};for(let n of m(e))if(`w:rPr`in n)t.rPr=n;else if(`w:t`in n){let e=n[`w:t`];if(Array.isArray(e))for(let n of e)`#text`in n&&(t.text+=b(String(n[`#text`])))}else gt(n)?t.text+=`
7
+ `:t.anchored.push({node:n,offset:t.text.length});return t}function _t(e,t){let n=m(e);n.length=0,t.rPr&&n.push(t.rPr);let r=[...t.anchored].sort((e,t)=>e.offset-t.offset),i=t.text,a=0,o=``,s=()=>{o.length>0&&n.push(de(o)),o=``};for(let e=0;e<i.length;e++){for(;a<r.length&&r[a].offset<=e;)s(),n.push(r[a].node),a++;let t=i[e];t===`
8
+ `?(s(),n.push({"w:br":[]})):o+=t}for(s();a<r.length;)n.push(r[a].node),a++;i.length===0&&r.length===0&&n.push(de(``))}function vt(e){let t={"w:r":[]};return _t(t,{rPr:e.rPr?x(e.rPr):void 0,text:e.text,anchored:e.anchored}),t}function yt(e,t,n,r=`before`){let i={rPr:e.rPr,text:e.text.slice(0,t),anchored:[]},a={rPr:e.rPr,text:e.text.slice(t,n),anchored:[]},o={rPr:e.rPr,text:e.text.slice(n),anchored:[]};for(let s of e.anchored)(r===`before`?s.offset<=t:s.offset<t)?i.anchored.push(s):s.offset>=n?o.anchored.push({node:s.node,offset:s.offset-n}):a.anchored.push({node:s.node,offset:s.offset-t});return{before:i,matched:a,after:o}}function bt(e){return e.text.length>0||e.anchored.length>0}function I(e,t){let n=m(e);return n.includes(t)?n:xt(n,t)}function xt(e,t){for(let n of e){let e=f(n);if(e===`w:hyperlink`||e===`w:ins`||e===`w:del`){let e=m(n);if(e.includes(t))return e;let r=xt(e,t);if(r)return r}}}function L(e,t){if(t.isFullRun)return{matched:t.run.xmlRef};let n=t.run.xmlRef,r=I(e,n);if(!r)throw Error(`Run not found in paragraph children or hyperlink children`);let i=r.indexOf(n),a=yt(F(n),t.localStart,t.localEnd),o={matched:void 0},s=[];if(bt(a.before)){let e=vt(a.before);o.before=e,s.push(e)}let c=vt(a.matched);if(o.matched=c,s.push(c),bt(a.after)){let e=vt(a.after);o.after=e,s.push(e)}return r.splice(i,1,...s),o}function St(e,t,n,r=`before`){let i=I(e,t);if(!i)throw Error(`Run not found in paragraph children or hyperlink children`);let a=i.indexOf(t),o=yt(F(t),n,n,r),s=[],c={parentChildren:i,index:a};return bt(o.before)&&(c.before=vt(o.before),s.push(c.before),c.index=a+1),bt(o.after)&&(c.after=vt(o.after),s.push(c.after)),i.splice(a,1,...s),c}function R(e,t){if(t.length===0)return[];if(t.length===1)return[L(e,t[0]).matched];let n=[],r=[],i=t[t.length-1];if(!i.isEndAligned){let n=L(e,i);r.push({index:t.length-1,split:n})}let a=t[0];if(!a.isStartAligned){let t=L(e,a);r.push({index:0,split:t})}for(let i=0;i<t.length;i++){let a=t[i],o=r.find(e=>e.index===i);if(o)n.push(o.split.matched);else{let t=I(e,a.run.xmlRef);t&&t.includes(a.run.xmlRef)&&n.push(a.run.xmlRef)}}return n}function Ct(e,t){return{nextId:Tt(e)+1,author:t.revisionAuthor??`docxtor`,date:t.revisionDate??new Date().toISOString()}}function z(e){let t=String(e.nextId);return e.nextId++,t}const wt=new Set([`w:ins`,`w:del`,`w:rPrChange`,`w:pPrChange`,`w:trPrChange`,`w:tblPrChange`,`w:sectPrChange`,`w:cellIns`,`w:cellDel`,`w:cellMerge`]);function Tt(e){let t=0;t=Math.max(t,B(e.xmlParts.document));for(let n of e.xmlParts.headers.values())t=Math.max(t,B(n));for(let n of e.xmlParts.footers.values())t=Math.max(t,B(n));for(let n of[`footnotes`,`endnotes`,`comments`]){let r=e.xmlParts[n];r&&(t=Math.max(t,B(r)))}return t}function B(e){let t=0;for(let n of e){let e=f(n);if(e&&wt.has(e)){let e=_(n,`w:id`);if(e){let n=parseInt(e,10);!isNaN(n)&&n>t&&(t=n)}}if(e){let e=m(n);e.length>0&&(t=Math.max(t,B(e)))}}return t}function V(e,t){return C(`w:ins`,t,{"w:id":z(e),"w:author":e.author,"w:date":e.date})}function Et(e,t){return C(`w:del`,t,{"w:id":z(e),"w:author":e.author,"w:date":e.date})}function Dt(e,t){let n=z(e);return C(`w:rPrChange`,[x(t)],{"w:id":n,"w:author":e.author,"w:date":e.date})}function H(e,t){let n=z(e);return C(`w:pPrChange`,[x(t)],{"w:id":n,"w:author":e.author,"w:date":e.date})}function Ot(e){return C(`w:ins`,[],{"w:id":z(e),"w:author":e.author,"w:date":e.date})}function kt(e){return C(`w:del`,[],{"w:id":z(e),"w:author":e.author,"w:date":e.date})}function At(e){let t=m(e);for(let e=0;e<t.length;e++){let n=t[e];if(`w:t`in n){let r=n[`w:t`],i=n[`:@`],a={"w:delText":r};i&&(a[`:@`]=i),t[e]=a}else if(`w:instrText`in n){let r={"w:delInstrText":n[`w:instrText`]};n[`:@`]&&(r[`:@`]=n[`:@`]),t[e]=r}}}function jt(e,t){let n=[];if(t&&n.push(x(t)),e.includes(`
9
9
  `)){let t=e.split(`
10
- `);for(let e=0;e<t.length;e++){let r=t[e];if(r.length>0||e===0){let e={"w:t":[{"#text":y(r)}]};w(r)&&v(e,`xml:space`,`preserve`),n.push(e)}e<t.length-1&&n.push({"w:br":[]})}}else{let t={"w:t":[{"#text":y(e)}]};w(e)&&v(t,`xml:space`,`preserve`),n.push(t)}return{"w:r":n}}const jt=new Set([`*`,`_`,`~`,`<`,`\\`]);function W(e,t=!0){return t?G(e).map(e=>e.text).join(``):e}function G(e,t=!0){if(!t)return e.length>0?[{text:e}]:[];let n=[],r=0,i=``,a=!1,o=!1,s=!1,c=!1;function l(){if(i.length>0){let e={text:i};a&&(e.bold=!0),o&&(e.italic=!0),s&&(e.strikethrough=!0),c&&(e.underline=!0),n.push(e),i=``}}for(;r<e.length;){if(e[r]===`\\`&&r+1<e.length&&jt.has(e[r+1])){i+=e[r+1],r+=2;continue}if(e.slice(r,r+3)===`<u>`){l(),c=!0,r+=3;continue}if(e.slice(r,r+4)===`</u>`){l(),c=!1,r+=4;continue}if(e[r]===`~`&&e[r+1]===`~`){l(),s=!s,r+=2;continue}if(e[r]===`*`&&e[r+1]===`*`){l(),a=!a,r+=2;continue}if(e[r]===`_`&&Mt(e,r,o)){l(),o=!o,r+=1;continue}i+=e[r],r++}return l(),Nt(n)}function Mt(e,t,n){let r=t>0?e[t-1]:``,i=t+1<e.length?e[t+1]:``;return r===`_`||i===`_`?!1:n?r!==``&&!/\s/.test(r):t===0||/\W/.test(r)?i!==``&&!/\s/.test(i):t===e.length-1||/\W/.test(i)}function Nt(e){if(e.length<=1)return e;let t=[e[0]];for(let n=1;n<e.length;n++){let r=t[t.length-1],i=e[n];!!r.bold==!!i.bold&&!!r.italic==!!i.italic&&!!r.strikethrough==!!i.strikethrough&&!!r.underline==!!i.underline?r.text+=i.text:t.push(i)}return t}function Pt(e,t){if(S(e,t))return;let n=m(e);for(let e of n){let n=f(e);if(n===`w:hyperlink`||n===`w:ins`||n===`w:del`){if(S(e,t))return;let n=m(e);for(let e of n){let n=f(e);if((n===`w:hyperlink`||n===`w:ins`||n===`w:del`)&&S(e,t))return}}}}function Ft(e,t){let n=m(e);if(!n.includes(t))for(let e of n){let n=f(e);if(n===`w:hyperlink`||n===`w:ins`||n===`w:del`){let n=m(e);if(n.includes(t))return e;for(let e of n){let n=f(e);if((n===`w:hyperlink`||n===`w:ins`||n===`w:del`)&&m(e).includes(t))return e}}}}function It(e,t,n,r=!0){let i=t.occurrence??1,a=W(t.find,r),o=P(e.text,a,i);if(o===-1)throw Error(`Text "${a}" not found in paragraph "${e.id}" (occurrence ${i})`);let s=o+a.length,c=F(e.runs,o,s);if(c.length===0)throw Error(`No runs found for text range [${o}, ${s}] in paragraph "${e.id}"`);n?Rt(e,t,c,n,r):Lt(e,t,c,r)}function Lt(e,t,n,r){let i=G(t.replace,r),a=i.some(e=>e.bold||e.italic||e.strikethrough||e.underline),o=W(t.replace,r);if(n.length===1){let t=n[0],r=z(e.xmlRef,t);a?Jt(e.xmlRef,r.matched,i):q(r.matched,o)}else{let t=B(e.xmlRef,n);if(t.length===0)throw Error(`Failed to isolate matched runs in paragraph "${e.id}"`);a?Jt(e.xmlRef,t[0],i):q(t[0],o);for(let n=1;n<t.length;n++){let r=I(t[n]);r.anchored.length>0?gt(t[n],{...r,text:``}):Pt(e.xmlRef,t[n])}}}function Rt(e,t,n,r,i){let a;if(n.length===1){let t=n[0];a=[z(e.xmlRef,t).matched]}else a=B(e.xmlRef,n);if(a.length===0)throw Error(`Failed to isolate matched runs in paragraph "${e.id}"`);let o=h(a[0],`w:rPr`),s=G(t.replace,i),c=s.some(e=>e.bold||e.italic||e.strikethrough||e.underline),l=W(t.replace,i),u=[];s.length>0&&l.length>0&&(u=c?K(s,o):[At(l,o)]);let d=zt(e.xmlRef,a,r);u.length!==0&&(d.container?Bt(e.xmlRef,d.container,d.index,u,r):d.parentChildren.splice(d.index,0,H(r,u)))}function zt(e,t,n){let r,i=[];for(let n of t){let t=R(e,n);if(!t)throw Error(`Matched run not found in paragraph`);let a=Ft(e,n),o=a!=null&&f(a)===`w:ins`,s=t.indexOf(n);if(t.splice(s,1),o){let n=!0,i,o=-1;t.length===0&&(i=R(e,a),i&&(o=i.indexOf(a),i.splice(o,1),n=!1)),r||=n?{parentChildren:t,index:s,container:a}:{parentChildren:i,index:o};continue}let c=i[i.length-1];c&&c.parent===t&&c.index===s?c.runs.push(n):i.push({parent:t,index:s,runs:[n]})}for(let e=i.length-1;e>=0;e--){let t=i[e],r=t.runs.map(e=>{let t=x(e);return kt(t),t});t.parent.splice(t.index,0,Tt(n,r))}if(!r){let e=i[0];r={parentChildren:e.parent,index:e.index+1}}return r}function Bt(e,t,n,r,i){let a=m(t),o=R(e,t);if(!o)throw Error(`Insertion container not found in paragraph`);let s=o.indexOf(t);if(r.length===0){a.length===0&&o.splice(s,1);return}if(_(t,`w:author`)===i.author){a.splice(n,0,...r);return}let c=a.splice(n),l=[];if(a.length>0&&l.push(t),l.push(H(i,r)),c.length>0){let e=C(`w:ins`,c,{"w:id":V(i),"w:author":_(t,`w:author`)??``,"w:date":_(t,`w:date`)??``});l.push(e)}o.splice(s,1,...l)}function Vt(e,t,n,r=!0){let i=t.occurrence??1,a=W(t.anchor,r),o=P(e.text,a,i);if(o===-1)throw Error(`Anchor text "${a}" not found in paragraph "${e.id}" (occurrence ${i})`);let s=t.position===`before`?o:o+a.length;n?Ut(e,t,s,n,r):Ht(e,t,s,r)}function Ht(e,t,n,r){let i;for(let t of e.runs)if(n>=t.startOffset&&n<=t.endOffset){i=t;break}if(!i){let t=e.runs[e.runs.length-1];if(!t)throw Error(`No runs found in paragraph "${e.id}" for insertion`);i=t}let a=G(t.text,r);if(a.some(e=>e.bold||e.italic||e.strikethrough||e.underline)){let r=n-i.startOffset,o=K(a,h(i.xmlRef,`w:rPr`)),s=bt(e.xmlRef,i.xmlRef,r,Wt(t));s.parentChildren.splice(s.index,0,...o)}else{let e=W(t.text,r),a=n-i.startOffset,o=re(i.xmlRef),s=o.slice(0,a)+e+o.slice(a),c=Wt(t);q(i.xmlRef,s,t=>t<a||t===a&&c===`before`?t:t+e.length)}}function Ut(e,t,n,r,i){let a;for(let t of e.runs)if(n>=t.startOffset&&n<=t.endOffset){a=t;break}if(!a){let t=e.runs[e.runs.length-1];if(!t)throw Error(`No runs found in paragraph "${e.id}" for insertion`);a=t}let o=n-a.startOffset,s=h(a.xmlRef,`w:rPr`),c=G(t.text,i),l=c.some(e=>e.bold||e.italic||e.strikethrough||e.underline),u;u=l?K(c,s):[At(W(t.text,i),s)];let d=H(r,u),f=R(e.xmlRef,a.xmlRef);if(!f)throw Error(`Target run not found in paragraph`);let p=f.indexOf(a.xmlRef),m=o===0,g=o===a.text.length;if(m)f.splice(p,0,d);else if(g)f.splice(p+1,0,d);else{let n=bt(e.xmlRef,a.xmlRef,o,Wt(t));n.parentChildren.splice(n.index,0,d)}}function Wt(e){return e.position===`after`?`after`:`before`}function Gt(e,t){let n=[];if(t&&n.push(x(t)),e.includes(`
10
+ `);for(let e=0;e<t.length;e++){let r=t[e];if(r.length>0||e===0){let e={"w:t":[{"#text":y(r)}]};w(r)&&v(e,`xml:space`,`preserve`),n.push(e)}e<t.length-1&&n.push({"w:br":[]})}}else{let t={"w:t":[{"#text":y(e)}]};w(e)&&v(t,`xml:space`,`preserve`),n.push(t)}return{"w:r":n}}const Mt=new Set([`*`,`_`,`~`,`<`,`\\`]);function U(e,t=!0){return t?W(e).map(e=>e.text).join(``):e}function W(e,t=!0){if(!t)return e.length>0?[{text:e}]:[];let n=[],r=0,i=``,a=!1,o=!1,s=!1,c=!1;function l(){if(i.length>0){let e={text:i};a&&(e.bold=!0),o&&(e.italic=!0),s&&(e.strikethrough=!0),c&&(e.underline=!0),n.push(e),i=``}}for(;r<e.length;){if(e[r]===`\\`&&r+1<e.length&&Mt.has(e[r+1])){i+=e[r+1],r+=2;continue}if(e.slice(r,r+3)===`<u>`){l(),c=!0,r+=3;continue}if(e.slice(r,r+4)===`</u>`){l(),c=!1,r+=4;continue}if(e[r]===`~`&&e[r+1]===`~`){l(),s=!s,r+=2;continue}if(e[r]===`*`&&e[r+1]===`*`){l(),a=!a,r+=2;continue}if(e[r]===`_`&&Nt(e,r,o)){l(),o=!o,r+=1;continue}i+=e[r],r++}return l(),Pt(n)}function Nt(e,t,n){let r=t>0?e[t-1]:``,i=t+1<e.length?e[t+1]:``;return r===`_`||i===`_`?!1:n?r!==``&&!/\s/.test(r):t===0||/\W/.test(r)?i!==``&&!/\s/.test(i):t===e.length-1||/\W/.test(i)}function Pt(e){if(e.length<=1)return e;let t=[e[0]];for(let n=1;n<e.length;n++){let r=t[t.length-1],i=e[n];!!r.bold==!!i.bold&&!!r.italic==!!i.italic&&!!r.strikethrough==!!i.strikethrough&&!!r.underline==!!i.underline?r.text+=i.text:t.push(i)}return t}function Ft(e,t){if(S(e,t))return;let n=m(e);for(let e of n){let n=f(e);if(n===`w:hyperlink`||n===`w:ins`||n===`w:del`){if(S(e,t))return;let n=m(e);for(let e of n){let n=f(e);if((n===`w:hyperlink`||n===`w:ins`||n===`w:del`)&&S(e,t))return}}}}function It(e,t){let n=m(e);if(!n.includes(t))for(let e of n){let n=f(e);if(n===`w:hyperlink`||n===`w:ins`||n===`w:del`){let n=m(e);if(n.includes(t))return e;for(let e of n){let n=f(e);if((n===`w:hyperlink`||n===`w:ins`||n===`w:del`)&&m(e).includes(t))return e}}}}function Lt(e,t,n,r=!0){let i=t.occurrence??1,a=U(t.find,r),o=N(e.text,a,i);if(o===-1)throw Error(`Text "${a}" not found in paragraph "${e.id}" (occurrence ${i})`);let s=o+a.length,c=P(e.runs,o,s);if(c.length===0)throw Error(`No runs found for text range [${o}, ${s}] in paragraph "${e.id}"`);n?zt(e,t,c,n,r):Rt(e,t,c,r)}function Rt(e,t,n,r){let i=W(t.replace,r),a=i.some(e=>e.bold||e.italic||e.strikethrough||e.underline),o=U(t.replace,r);if(n.length===1){let t=n[0],r=L(e.xmlRef,t);a?Yt(e.xmlRef,r.matched,i):K(r.matched,o)}else{let t=R(e.xmlRef,n);if(t.length===0)throw Error(`Failed to isolate matched runs in paragraph "${e.id}"`);a?Yt(e.xmlRef,t[0],i):K(t[0],o);for(let n=1;n<t.length;n++){let r=F(t[n]);r.anchored.length>0?_t(t[n],{...r,text:``}):Ft(e.xmlRef,t[n])}}}function zt(e,t,n,r,i){let a;if(n.length===1){let t=n[0];a=[L(e.xmlRef,t).matched]}else a=R(e.xmlRef,n);if(a.length===0)throw Error(`Failed to isolate matched runs in paragraph "${e.id}"`);let o=h(a[0],`w:rPr`),s=W(t.replace,i),c=s.some(e=>e.bold||e.italic||e.strikethrough||e.underline),l=U(t.replace,i),u=[];s.length>0&&l.length>0&&(u=c?G(s,o):[jt(l,o)]);let d=Bt(e.xmlRef,a,r);u.length!==0&&(d.container?Vt(e.xmlRef,d.container,d.index,u,r):d.parentChildren.splice(d.index,0,V(r,u)))}function Bt(e,t,n){let r,i=[];for(let n of t){let t=I(e,n);if(!t)throw Error(`Matched run not found in paragraph`);let a=It(e,n),o=a!=null&&f(a)===`w:ins`,s=t.indexOf(n);if(t.splice(s,1),o){let n=!0,i,o=-1;t.length===0&&(i=I(e,a),i&&(o=i.indexOf(a),i.splice(o,1),n=!1)),r||=n?{parentChildren:t,index:s,container:a}:{parentChildren:i,index:o};continue}let c=i[i.length-1];c&&c.parent===t&&c.index===s?c.runs.push(n):i.push({parent:t,index:s,runs:[n]})}for(let e=i.length-1;e>=0;e--){let t=i[e],r=t.runs.map(e=>{let t=x(e);return At(t),t});t.parent.splice(t.index,0,Et(n,r))}if(!r){let e=i[0];r={parentChildren:e.parent,index:e.index+1}}return r}function Vt(e,t,n,r,i){let a=m(t),o=I(e,t);if(!o)throw Error(`Insertion container not found in paragraph`);let s=o.indexOf(t);if(r.length===0){a.length===0&&o.splice(s,1);return}if(_(t,`w:author`)===i.author){a.splice(n,0,...r);return}let c=a.splice(n),l=[];if(a.length>0&&l.push(t),l.push(V(i,r)),c.length>0){let e=C(`w:ins`,c,{"w:id":z(i),"w:author":_(t,`w:author`)??``,"w:date":_(t,`w:date`)??``});l.push(e)}o.splice(s,1,...l)}function Ht(e,t,n,r=!0){let i=t.occurrence??1,a=U(t.anchor,r),o=N(e.text,a,i);if(o===-1)throw Error(`Anchor text "${a}" not found in paragraph "${e.id}" (occurrence ${i})`);let s=t.position===`before`?o:o+a.length;n?Wt(e,t,s,n,r):Ut(e,t,s,r)}function Ut(e,t,n,r){let i;for(let t of e.runs)if(n>=t.startOffset&&n<=t.endOffset){i=t;break}if(!i){let t=e.runs[e.runs.length-1];if(!t)throw Error(`No runs found in paragraph "${e.id}" for insertion`);i=t}let a=W(t.text,r);if(a.some(e=>e.bold||e.italic||e.strikethrough||e.underline)){let r=n-i.startOffset,o=G(a,h(i.xmlRef,`w:rPr`)),s=St(e.xmlRef,i.xmlRef,r,Gt(t));s.parentChildren.splice(s.index,0,...o)}else{let e=U(t.text,r),a=n-i.startOffset,o=re(i.xmlRef),s=o.slice(0,a)+e+o.slice(a),c=Gt(t);K(i.xmlRef,s,t=>t<a||t===a&&c===`before`?t:t+e.length)}}function Wt(e,t,n,r,i){let a;for(let t of e.runs)if(n>=t.startOffset&&n<=t.endOffset){a=t;break}if(!a){let t=e.runs[e.runs.length-1];if(!t)throw Error(`No runs found in paragraph "${e.id}" for insertion`);a=t}let o=n-a.startOffset,s=h(a.xmlRef,`w:rPr`),c=W(t.text,i),l=c.some(e=>e.bold||e.italic||e.strikethrough||e.underline),u;u=l?G(c,s):[jt(U(t.text,i),s)];let d=V(r,u),f=I(e.xmlRef,a.xmlRef);if(!f)throw Error(`Target run not found in paragraph`);let p=f.indexOf(a.xmlRef),m=o===0,g=o===a.text.length;if(m)f.splice(p,0,d);else if(g)f.splice(p+1,0,d);else{let n=St(e.xmlRef,a.xmlRef,o,Gt(t));n.parentChildren.splice(n.index,0,d)}}function Gt(e){return e.position===`after`?`after`:`before`}function Kt(e,t){let n=[];if(t&&n.push(x(t)),e.includes(`
11
11
  `)){let t=e.split(`
12
- `);for(let e=0;e<t.length;e++){let r=t[e];if(r.length>0||e===0){let e={"w:t":[{"#text":y(r)}]};w(r)&&v(e,`xml:space`,`preserve`),n.push(e)}e<t.length-1&&n.push({"w:br":[]})}}else{let t={"w:t":[{"#text":y(e)}]};w(e)&&v(t,`xml:space`,`preserve`),n.push(t)}return{"w:r":n}}function Kt(e,t,n,r=!0){n?qt(e,t,n,r):It(e,{type:`replace_text`,target:t.target,find:t.find,replace:``,occurrence:t.occurrence},void 0,r)}function qt(e,t,n,r){let i=t.occurrence??1,a=W(t.find,r),o=P(e.text,a,i);if(o===-1)throw Error(`Text "${a}" not found in paragraph "${e.id}" (occurrence ${i})`);let s=o+a.length,c=F(e.runs,o,s);if(c.length===0)throw Error(`No runs found for text range [${o}, ${s}] in paragraph "${e.id}"`);let l;if(c.length===1){let t=c[0];l=[z(e.xmlRef,t).matched]}else l=B(e.xmlRef,c);if(l.length===0)throw Error(`Failed to isolate matched runs in paragraph "${e.id}"`);zt(e.xmlRef,l,n)}function K(e,t){let n=[];for(let r of e){if(r.text.length===0)continue;let e=Gt(r.text,t);if(r.bold||r.italic||r.strikethrough||r.underline){let t=h(e,`w:rPr`);t||(t=C(`w:rPr`,[]),m(e).unshift(t));let n=m(t);r.bold&&!h(t,`w:b`)&&n.push(C(`w:b`,[])),r.italic&&!h(t,`w:i`)&&n.push(C(`w:i`,[])),r.strikethrough&&!h(t,`w:strike`)&&n.push(C(`w:strike`,[])),r.underline&&!h(t,`w:u`)&&n.push(C(`w:u`,[],{"w:val":`single`}))}n.push(e)}return n}function Jt(e,t,n){let r=I(t),i=K(n,r.rPr);if(i.length===0){q(t,``);return}let a=r.anchored.filter(e=>e.offset<=0).map(e=>e.node),o=r.anchored.filter(e=>e.offset>0).map(e=>e.node);if(a.length>0){let e=m(i[0]),t=+(e.length>0&&`w:rPr`in e[0]);e.splice(t,0,...a)}o.length>0&&m(i[i.length-1]).push(...o);let s=R(e,t);if(s){let e=s.indexOf(t);if(e!==-1){s.splice(e,1,...i);return}}q(t,n.map(e=>e.text).join(``))}function q(e,t,n){let r=I(e),i=ie(e);if(i!==void 0&&r.anchored.length===0&&!t.includes(`
12
+ `);for(let e=0;e<t.length;e++){let r=t[e];if(r.length>0||e===0){let e={"w:t":[{"#text":y(r)}]};w(r)&&v(e,`xml:space`,`preserve`),n.push(e)}e<t.length-1&&n.push({"w:br":[]})}}else{let t={"w:t":[{"#text":y(e)}]};w(e)&&v(t,`xml:space`,`preserve`),n.push(t)}return{"w:r":n}}function qt(e,t,n,r=!0){n?Jt(e,t,n,r):Lt(e,{type:`replace_text`,target:t.target,find:t.find,replace:``,occurrence:t.occurrence},void 0,r)}function Jt(e,t,n,r){let i=t.occurrence??1,a=U(t.find,r),o=N(e.text,a,i);if(o===-1)throw Error(`Text "${a}" not found in paragraph "${e.id}" (occurrence ${i})`);let s=o+a.length,c=P(e.runs,o,s);if(c.length===0)throw Error(`No runs found for text range [${o}, ${s}] in paragraph "${e.id}"`);let l;if(c.length===1){let t=c[0];l=[L(e.xmlRef,t).matched]}else l=R(e.xmlRef,c);if(l.length===0)throw Error(`Failed to isolate matched runs in paragraph "${e.id}"`);Bt(e.xmlRef,l,n)}function G(e,t){let n=[];for(let r of e){if(r.text.length===0)continue;let e=Kt(r.text,t);if(r.bold||r.italic||r.strikethrough||r.underline){let t=h(e,`w:rPr`);t||(t=C(`w:rPr`,[]),m(e).unshift(t));let n=m(t);r.bold&&!h(t,`w:b`)&&n.push(C(`w:b`,[])),r.italic&&!h(t,`w:i`)&&n.push(C(`w:i`,[])),r.strikethrough&&!h(t,`w:strike`)&&n.push(C(`w:strike`,[])),r.underline&&!h(t,`w:u`)&&n.push(C(`w:u`,[],{"w:val":`single`}))}n.push(e)}return n}function Yt(e,t,n){let r=F(t),i=G(n,r.rPr);if(i.length===0){K(t,``);return}let a=r.anchored.filter(e=>e.offset<=0).map(e=>e.node),o=r.anchored.filter(e=>e.offset>0).map(e=>e.node);if(a.length>0){let e=m(i[0]),t=+(e.length>0&&`w:rPr`in e[0]);e.splice(t,0,...a)}o.length>0&&m(i[i.length-1]).push(...o);let s=I(e,t);if(s){let e=s.indexOf(t);if(e!==-1){s.splice(e,1,...i);return}}K(t,n.map(e=>e.text).join(``))}function K(e,t,n){let r=F(e),i=ie(e);if(i!==void 0&&r.anchored.length===0&&!t.includes(`
13
13
  `)&&!r.text.includes(`
14
- `)&&m(e).filter(e=>`w:t`in e).length===1){let e=i[`w:t`];if(Array.isArray(e)){let n=e.find(e=>`#text`in e);n?ne(n,t):e.push({"#text":y(t)})}w(t)&&v(i,`xml:space`,`preserve`);return}let a=n??(e=>e<=0?0:t.length);gt(e,{rPr:r.rPr,text:t,anchored:r.anchored.map(e=>({node:e.node,offset:a(e.offset)}))})}function Yt(e,t,n){if(e.rows.length===0)throw Error(`Table "${e.id}" has no rows to clone from`);if(t.afterRow<-1||t.afterRow>=e.rows.length)throw Error(`Invalid afterRow ${t.afterRow} for table "${e.id}" with ${e.rows.length} rows`);let r=t.afterRow>=0?t.afterRow:0,i=e.rows[r];if(t.cells.length!==i.cells.length)throw Error(`Cell count mismatch: patch has ${t.cells.length} cells, but row has ${i.cells.length} cells`);let a=x(i.xmlRef),o=g(a,`w:tc`);for(let e=0;e<o.length;e++){let n=o[e],r=t.cells[e],i=h(n,`w:p`);if(i){let e=m(i),t=e.filter(e=>`w:r`in e||`w:ins`in e||`w:del`in e);for(let n of t){let t=e.indexOf(n);t!==-1&&e.splice(t,1)}let n={"w:t":[{"#text":y(r)}]};w(r)&&(n[`:@`]||={},n[`:@`][`@_xml:space`]=`preserve`);let a={"w:r":[n]};e.push(a)}}let s=m(e.xmlRef);if(t.afterRow===-1){let e=s.find(e=>`w:tr`in e);if(e){let t=s.indexOf(e);s.splice(t,0,a)}else s.push(a)}else le(e.xmlRef,i.xmlRef,a);if(n){let e=m(a),t=e.find(e=>`w:trPr`in e);t||(t=C(`w:trPr`,[]),e.unshift(t)),m(t).push(Dt(n))}return a}function Xt(e,t,n){if(t.row<0||t.row>=e.rows.length)throw Error(`Invalid row index ${t.row} for table "${e.id}" with ${e.rows.length} rows`);let r=e.rows[t.row];if(n){let e=m(r.xmlRef),t=e.find(e=>`w:trPr`in e);t||(t=C(`w:trPr`,[]),e.unshift(t)),m(t).push(Ot(n))}else S(e.xmlRef,r.xmlRef)}function Zt(e,t,n,r=!0){It(e,{type:`replace_text`,target:t.target,find:t.find,replace:t.replace,occurrence:t.occurrence},n,r)}function Qt(e,t){let n=e?.length??(t.length>0?t[0].length:0),r={"w:val":`single`,"w:sz":`4`,"w:space":`0`,"w:color":`auto`},i=C(`w:tblBorders`,[C(`w:top`,[],r),C(`w:left`,[],r),C(`w:bottom`,[],r),C(`w:right`,[],r),C(`w:insideH`,[],r),C(`w:insideV`,[],r)]),a=C(`w:tblPr`,[C(`w:tblStyle`,[],{"w:val":`TableGrid`}),C(`w:tblW`,[],{"w:w":`0`,"w:type":`auto`}),i,C(`w:tblLook`,[],{"w:val":`04A0`,"w:firstRow":`1`,"w:lastRow":`0`,"w:firstColumn":`1`,"w:lastColumn":`0`,"w:noHBand":`0`,"w:noVBand":`1`})]),o=[];for(let e=0;e<n;e++)o.push(C(`w:gridCol`,[]));let s=C(`w:tblGrid`,o),c=[];e&&c.push($t(e));for(let e of t)c.push($t(e));return C(`w:tbl`,[a,s,...c])}function $t(e){let t=[];for(let n of e){let e={"w:t":[{"#text":y(n)}]};w(n)&&(e[`:@`]||={},e[`:@`][`@_xml:space`]=`preserve`);let r={"w:p":[{"w:r":[e]}]},i=C(`w:tc`,[C(`w:tcPr`,[C(`w:tcW`,[],{"w:w":`0`,"w:type":`auto`})]),r]);t.push(i)}return C(`w:tr`,t)}function en(e,t,n){t.find!==void 0&&t.find.length>0?tn(e,t.find,t.occurrence??1,t.formatting,n):nn(e,t.formatting,n)}function tn(e,t,n,r,i){let a=P(e.text,t,n);if(a===-1)throw Error(`Text "${t}" not found in paragraph "${e.id}" (occurrence ${n})`);let o=a+t.length,s=F(e.runs,a,o);if(s.length===0)throw Error(`No runs found for text range [${a}, ${o}] in paragraph "${e.id}"`);let c=B(e.xmlRef,s);for(let e of c)rn(e,r,i)}function nn(e,t,n){for(let r of e.runs)rn(r.xmlRef,t,n)}function rn(e,t,n){let r=Object.entries(t).some(([,e])=>e!==null&&e!==!1&&e!==void 0),i=h(e,`w:rPr`);if(!i&&!r)return;let a=n&&i?x(i):void 0;if(i||(i=C(`w:rPr`),m(e).unshift(i)),t.bold!==void 0&&an(i,`w:b`,t.bold),t.italic!==void 0&&an(i,`w:i`,t.italic),t.strikethrough!==void 0&&an(i,`w:strike`,t.strikethrough),t.underline!==void 0&&on(i,`w:u`,`w:val`,t.underline),t.fontSize!==void 0&&on(i,`w:sz`,`w:val`,t.fontSize===null?null:String(t.fontSize)),t.fontFamily!==void 0&&sn(i,t.fontFamily),t.color!==void 0&&on(i,`w:color`,`w:val`,t.color),t.highlight!==void 0&&on(i,`w:highlight`,`w:val`,t.highlight),n){let e=h(i,`w:rPrChange`);e&&S(i,e);let t=Et(n,a??C(`w:rPr`));m(i).push(t)}}function an(e,t,n){let r=h(e,t);n===!0?r||m(e).push(C(t)):r&&S(e,r)}function on(e,t,n,r){let i=h(e,t);if(r!==null)if(i)v(i,n,r);else{let i=C(t,[],{[n]:r});m(e).push(i)}else i&&S(e,i)}function sn(e,t){let n=h(e,`w:rFonts`);if(t!==null)if(n)v(n,`w:ascii`,t),v(n,`w:hAnsi`,t);else{let n=C(`w:rFonts`,[],{"w:ascii":t,"w:hAnsi":t});m(e).push(n)}else n&&(ee(n,`w:ascii`),ee(n,`w:hAnsi`),Object.keys(te(n)).length===0&&S(e,n))}function cn(e,t,n,r){let i=n.styles.find(e=>e.id===t.style);if(!i)throw Error(`Style "${t.style}" not found in document styles.xml`);if(t.find!==void 0&&t.find.length>0){if(i.type!==`character`)throw Error(`Style "${t.style}" is type "${i.type}", but character style is required when "find" is specified`);un(e,t.find,t.occurrence??1,t.style,r)}else{if(i.type!==`paragraph`)throw Error(`Style "${t.style}" is type "${i.type}", but paragraph style is required when "find" is omitted`);ln(e,t.style,r)}}function ln(e,t,n){let r=e.xmlRef,i=h(r,`w:pPr`),a=n&&i?x(i):void 0;i||(i=C(`w:pPr`),m(r).unshift(i));let o=h(i,`w:pStyle`);if(o?v(o,`w:val`,t):(o=C(`w:pStyle`,[],{"w:val":t}),m(i).unshift(o)),n){let e=h(i,`w:pPrChange`);e&&S(i,e);let t=U(n,a??C(`w:pPr`));m(i).push(t)}}function un(e,t,n,r,i){let a=P(e.text,t,n);if(a===-1)throw Error(`Text "${t}" not found in paragraph "${e.id}" (occurrence ${n})`);let o=a+t.length,s=F(e.runs,a,o);if(s.length===0)throw Error(`No runs found for text range [${a}, ${o}] in paragraph "${e.id}"`);let c=B(e.xmlRef,s);for(let e of c)dn(e,r,i)}function dn(e,t,n){let r=h(e,`w:rPr`),i=n&&r?x(r):void 0;r||(r=C(`w:rPr`),m(e).unshift(r));let a=h(r,`w:rStyle`);a&&S(r,a);let o=C(`w:rStyle`,[],{"w:val":t});if(m(r).unshift(o),n){let e=h(r,`w:rPrChange`);e&&S(r,e);let t=Et(n,i??C(`w:rPr`));m(r).push(t)}}const fn=new Set([`PAGE`,`NUMPAGES`,`DATE`,`TIME`,`FILENAME`]);function pn(e){let t=[],n=/\{\{(\w+)\}\}/g,r=0,i;for(;(i=n.exec(e))!==null;){let n=i[1];fn.has(n)&&(i.index>r&&t.push({kind:`text`,text:e.slice(r,i.index)}),t.push({kind:`field`,fieldCode:n}),r=i.index+i[0].length)}return r<e.length&&t.push({kind:`text`,text:e.slice(r)}),t.length===0&&t.push({kind:`text`,text:e}),t}function mn(e){if(!e.includes(``))return``;for(let t=57344;t<=63743;t++){let n=String.fromCharCode(t);if(!e.includes(n))return n}throw Error(`Cannot find a placeholder character for field codes`)}function J(e,t,n=!0){let r=pn(e);if(r.length===1&&r[0].kind===`text`)return hn(G(r[0].text,n),t);let i=r.filter(e=>e.kind===`field`).map(e=>e.fieldCode),a=mn(e),o=G(r.map(e=>e.kind===`text`?e.text:a).join(``),n),s=[],c=[],l=0,u=()=>{c.length>0&&(s.push(...hn(c,t)),c=[])};for(let e of o){let n=e.text.split(a);for(let r=0;r<n.length;r++){let a=n[r];if(a.length>0&&c.push({...e,text:a}),r<n.length-1){u();let n=_n(t,{bold:!!e.bold,italic:!!e.italic,strikethrough:!!e.strikethrough,underline:!!e.underline});s.push(...gn(i[l++],n))}}}return u(),s}function hn(e,t){if(e.some(e=>e.bold||e.italic||e.strikethrough||e.underline))return K(e,t);let n=e.map(e=>e.text).join(``);return n.length>0?[fe(n,t)]:[]}function gn(e,t){let n=()=>t?[JSON.parse(JSON.stringify(t))]:[],r={"w:r":[...n(),C(`w:fldChar`,[],{"w:fldCharType":`begin`})]},i={"w:instrText":[{"#text":` ${e} `}]};return v(i,`xml:space`,`preserve`),[r,{"w:r":[...n(),i]},{"w:r":[...n(),C(`w:fldChar`,[],{"w:fldCharType":`separate`})]},{"w:r":[...n(),C(`w:fldChar`,[],{"w:fldCharType":`end`})]}]}function _n(e,t){let n=t.bold||t.italic||t.strikethrough||t.underline;if(!e&&!n)return;let r=e?JSON.parse(JSON.stringify(e)):C(`w:rPr`,[]);if(!n)return r;let i=m(r);return t.bold&&!i.some(e=>`w:b`in e)&&i.push(C(`w:b`,[])),t.italic&&!i.some(e=>`w:i`in e)&&i.push(C(`w:i`,[])),t.strikethrough&&!i.some(e=>`w:strike`in e)&&i.push(C(`w:strike`,[])),t.underline&&!i.some(e=>`w:u`in e)&&i.push(C(`w:u`,[],{"w:val":`single`})),r}const vn={default:`default`,first:`first`,even:`even`};function yn(e,t,n,r=!0){Cn(e,`header`,t.variant,t.paragraphs,n,r)}function bn(e,t,n,r=!0){Cn(e,`footer`,t.variant,t.paragraphs,n,r)}function xn(e,t){wn(e,`header`,t.variant)}function Sn(e,t){wn(e,`footer`,t.variant)}function Cn(e,t,n,r,i,a=!0){let o=t===`header`?e.xmlParts.headers:e.xmlParts.footers,s=`${t}${Tn(o,t)}.xml`,c=`word/${s}`,l=t===`header`?`w:hdr`:`w:ftr`,u=[];for(let e of r){let t=J(e,void 0,a);if(i){let e=H(i,t.map(e=>e));u.push({"w:p":[e]})}else u.push({"w:p":t})}let d=[C(l,u,{"xmlns:w":`http://schemas.openxmlformats.org/wordprocessingml/2006/main`,"xmlns:r":`http://schemas.openxmlformats.org/officeDocument/2006/relationships`})],f=En(e);Dn(e,f,s,t),An(e,c,t),Nn(e,t,n,f),o.set(c,d);let p=De(d,t,n,f);t===`header`?e.headers.set(f,p):e.footers.set(f,p)}function wn(e,t,n){let r=t===`header`?`w:headerReference`:`w:footerReference`,i=Mn(e);if(!i)throw Error(`No sectPr found in document`);let a=m(i),o=a.find(e=>f(e)===r?(_(e,`w:type`)??`default`)===vn[n]:!1);if(!o)throw Error(`No ${t} with variant "${n}" found in sectPr`);let s=_(o,`r:id`);if(!s)throw Error(`${t} reference has no r:id`);let c=a.indexOf(o);c!==-1&&a.splice(c,1);let l=On(e,s),u=l?`word/${l}`:void 0;kn(e,s),u&&jn(e,u);let d=t===`header`?e.xmlParts.headers:e.xmlParts.footers;u&&d.delete(u),t===`header`?e.headers.delete(s):e.footers.delete(s)}function Tn(e,t){let n=0,r=RegExp(`${t}(\\d+)\\.xml$`,`i`);for(let t of e.keys()){let e=t.match(r);if(e){let t=parseInt(e[1],10);t>n&&(n=t)}}return n+1}function En(e){let t=e.xmlParts.documentRels;if(!t)return`rId1`;let n=0,r=t.find(e=>f(e)===`Relationships`);if(r){let e=g(r,`Relationship`);for(let t of e){let e=_(t,`Id`);if(e){let t=parseInt(e.replace(/^rId/,``),10);!isNaN(t)&&t>n&&(n=t)}}}return`rId${n+1}`}function Dn(e,t,n,r){e.xmlParts.documentRels||(e.xmlParts.documentRels=[C(`Relationships`,[],{xmlns:`http://schemas.openxmlformats.org/package/2006/relationships`})]);let i=C(`Relationship`,[],{Id:t,Type:r===`header`?`http://schemas.openxmlformats.org/officeDocument/2006/relationships/header`:`http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer`,Target:n}),a=e.xmlParts.documentRels.find(e=>f(e)===`Relationships`);a&&m(a).push(i)}function On(e,t){let n=e.xmlParts.documentRels;if(!n)return;let r=n.find(e=>f(e)===`Relationships`);if(!r)return;let i=g(r,`Relationship`);for(let e of i)if(_(e,`Id`)===t)return _(e,`Target`)}function kn(e,t){let n=e.xmlParts.documentRels;if(!n)return;let r=n.find(e=>f(e)===`Relationships`);if(!r)return;let i=m(r),a=i.findIndex(e=>f(e)===`Relationship`&&_(e,`Id`)===t);a!==-1&&i.splice(a,1)}function An(e,t,n){if(!e.xmlParts.contentTypes)return;let r=n===`header`?`application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml`:`application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml`,i=C(`Override`,[],{PartName:`/${t}`,ContentType:r}),a=e.xmlParts.contentTypes.find(e=>f(e)===`Types`);a&&m(a).push(i)}function jn(e,t){if(!e.xmlParts.contentTypes)return;let n=e.xmlParts.contentTypes.find(e=>f(e)===`Types`);if(!n)return;let r=m(n),i=r.findIndex(e=>{if(f(e)!==`Override`)return!1;let n=_(e,`PartName`);return n===`/${t}`||n===t});i!==-1&&r.splice(i,1)}function Mn(e){let t=e.xmlParts.document.find(e=>f(e)===`w:document`);if(!t)return;let n=h(t,`w:body`);if(!n)return;let r=m(n);for(let e=r.length-1;e>=0;e--)if(f(r[e])===`w:sectPr`)return r[e]}function Nn(e,t,n,r){let i=Mn(e);if(!i){i=C(`w:sectPr`,[]);let t=e.xmlParts.document.find(e=>f(e)===`w:document`);if(!t)throw Error(`Cannot find w:document`);let n=h(t,`w:body`);if(!n)throw Error(`Cannot find w:body`);m(n).push(i)}let a=C(t===`header`?`w:headerReference`:`w:footerReference`,[],{"w:type":vn[n],"r:id":r});if(m(i).unshift(a),n===`first`){let e=m(i);e.some(e=>f(e)===`w:titlePg`)||e.push(C(`w:titlePg`,[]))}n===`even`&&Pn(e)}function Pn(e){e.xmlParts.settings||(e.xmlParts.settings=[C(`w:settings`,[C(`w:evenAndOddHeaders`,[])],{"xmlns:w":`http://schemas.openxmlformats.org/wordprocessingml/2006/main`})]);let t=e.xmlParts.settings.find(e=>f(e)===`w:settings`);if(!t)return;let n=m(t);n.some(e=>f(e)===`w:evenAndOddHeaders`)||n.push(C(`w:evenAndOddHeaders`,[])),Fn(e),In(e)}function Fn(e){e.xmlParts.documentRels||(e.xmlParts.documentRels=[C(`Relationships`,[],{xmlns:`http://schemas.openxmlformats.org/package/2006/relationships`})]);let t=e.xmlParts.documentRels.find(e=>f(e)===`Relationships`);if(!t)return;let n=`http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings`,r=m(t);r.some(e=>f(e)===`Relationship`?_(e,`Type`)===n&&_(e,`Target`)===`settings.xml`:!1)||r.push(C(`Relationship`,[],{Id:En(e),Type:n,Target:`settings.xml`}))}function In(e){e.xmlParts.contentTypes||(e.xmlParts.contentTypes=[C(`Types`,[],{xmlns:`http://schemas.openxmlformats.org/package/2006/content-types`})]);let t=e.xmlParts.contentTypes.find(e=>f(e)===`Types`);if(!t)return;let n=`/word/settings.xml`,r=m(t);r.some(e=>f(e)===`Override`?_(e,`PartName`)===n:!1)||r.push(C(`Override`,[],{PartName:n,ContentType:`application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml`}))}function Ln(e){let t=e.xmlParts.document.find(e=>f(e)===`w:document`);if(!t)throw Error(`Cannot find w:document in FXP tree`);let n=h(t,`w:body`);if(!n)throw Error(`Cannot find w:body in FXP tree`);return m(n)}function Rn(e,t,n){if(n){let r=e.indexOf(n);if(r!==-1){e.splice(r+1,0,...t);return}}let r=e.findIndex(e=>f(e)===`w:sectPr`);r===-1?e.push(...t):e.splice(r,0,...t)}function zn(e){let t=-1;for(let n of e.body)if(n.kind===`paragraph`){let e=parseInt(n.id.replace(`p`,``),10);!isNaN(e)&&e>t&&(t=e)}return t+1}function Bn(e,t){let n=Ln(e);if(n.includes(t))return n;for(let e of n)if(f(e)===`w:tbl`){let n=Vn(e,t);if(n)return n}for(let n of[...e.headers.values(),...e.footers.values()]){let e=n.type===`header`?`w:hdr`:`w:ftr`,r=n.xmlRef.find(t=>f(t)===e);if(r){let e=m(r);if(e.includes(t))return e;for(let n of e)if(f(n)===`w:tbl`){let e=Vn(n,t);if(e)return e}}}}function Vn(e,t){for(let n of m(e).filter(e=>f(e)===`w:tr`))for(let e of m(n).filter(e=>f(e)===`w:tc`)){let n=m(e);if(n.includes(t))return n;for(let e of n)if(f(e)===`w:tbl`){let n=Vn(e,t);if(n)return n}}}function Hn(e,t){let n=e.body.findIndex(e=>e.id===t);if(n!==-1)return{array:e.body,index:n};for(let n of[...e.headers.values(),...e.footers.values()]){let e=n.paragraphs.findIndex(e=>e.id===t);if(e!==-1)return{array:n.paragraphs,index:e}}let r=[];for(let t of e.body)t.kind===`table`&&r.push(t);for(let t of[...e.headers.values(),...e.footers.values()])r.push(...t.tables);for(let e of r){let n=Un(e,t);if(n)return n}}function Un(e,t){for(let n of e.rows)for(let e of n.cells){let n=e.paragraphs.findIndex(e=>e.id===t);if(n!==-1)return{array:e.paragraphs,index:n};for(let n of e.nestedTables){let e=Un(n,t);if(e)return e}}}function Wn(e,t){let n=e.match(/^(.*p)(\d+)$/);if(!n)return`p${Date.now()}`;let r=n[1],i=-1;for(let e of t)if(e.id.startsWith(r)){let t=e.id.slice(r.length),n=parseInt(t,10);!isNaN(n)&&n>i&&(i=n)}return`${r}${i+1}`}function Y(e,t){let n=T(t.xmlRef,t.id);t.text=n.text,t.runs=n.runs,t.style=n.style,t.listInfo=n.listInfo,t.revisions=n.revisions,t.paraId=n.paraId,t.hasPageBreak=n.hasPageBreak,t.pageBreakOffset=n.pageBreakOffset,e.xmlParts.numbering&&j(t,e.xmlParts.numbering)}function Gn(e,t,n,r,i=!0){let a=C(`w:pPr`,[C(`w:numPr`,[C(`w:ilvl`,[],{"w:val":String(n)}),C(`w:numId`,[],{"w:val":t})])]),o=J(e,void 0,i),s;return s=r?[a,H(r,o)]:[a,...o],{"w:p":s}}function Kn(e,t,n,r=!0){let i=t.numId??D(e,t.listType),a=t.level??0,o=[];for(let e of t.items){let t=typeof e==`string`?e:e.text,s=typeof e==`string`?a:e.level??a;o.push(Gn(t,i,s,n,r))}if(t.container){let n=t.container.type===`header`?e.headers:e.footers;for(let r of n.values())if(r.variant===t.container.variant){let n=r.type===`header`?`w:hdr`:`w:ftr`,i=r.xmlRef.find(e=>f(e)===n);if(i){let n=m(i);O(e,o,i),n.push(...o);let a=t.container.type===`header`?`hdr`:`ftr`,s=r.paragraphs.length;for(let n of o){let i=T(n,`${a}.${t.container.variant}.p${s++}`);e.xmlParts.numbering&&j(i,e.xmlParts.numbering),r.paragraphs.push(i)}}return}return}let s=Ln(e),c,l;if(t.afterId){let n=mt(t.afterId,e);n&&(c=n.xmlRef,l=n.id)}O(e,o,k(e,`body`)),Rn(s,o,c);let u=zn(e),d=l===void 0?-1:e.body.findIndex(e=>e.id===l);for(let t=0;t<o.length;t++){let n=`p${u++}`,r=T(o[t],n);e.xmlParts.numbering&&j(r,e.xmlParts.numbering),d===-1?e.body.push(r):e.body.splice(d+1+t,0,r)}}function qn(e,t,n,r=!0){let i=M(t.target,e),a=i.listInfo.numId,o=t.level??i.listInfo.level,s=Gn(t.text,a,o,n,r);O(e,[s],k(e,i.id));let c=Bn(e,i.xmlRef);if(!c)throw Error(`Cannot find parent container for target paragraph`);let l=c.indexOf(i.xmlRef);if(l===-1)throw Error(`Target xmlRef not found in parent`);t.position===`after`?c.splice(l+1,0,s):c.splice(l,0,s);let u=Hn(e,i.id),d=u?u.array:e.body,f=T(s,Wn(i.id,d));e.xmlParts.numbering&&j(f,e.xmlParts.numbering),u?t.position===`after`?u.array.splice(u.index+1,0,f):u.array.splice(u.index,0,f):e.body.push(f)}function Jn(e,t,n){let r=M(t.target,e);if(n){let t=m(r.xmlRef),i=h(r.xmlRef,`w:pPr`);if(i){let e=U(n,i);m(i).push(e)}for(let e=t.length-1;e>=0;e--){let r=t[e],i=f(r);if(i===`w:r`){let i=x(r);kt(i),t[e]=Tt(n,[i])}else i===`w:ins`&&t.splice(e,1)}Y(e,r);return}let i=Bn(e,r.xmlRef);if(i){let e=i.indexOf(r.xmlRef);e!==-1&&i.splice(e,1)}let a=Hn(e,r.id);a&&a.array.splice(a.index,1)}function Yn(e,t,n){let r=D(e,t.listType),i=t.level??0;for(let a of t.targets){let t=M(a,e),o=t.xmlRef,s=h(o,`w:pPr`),c=m(o);if(n&&s){let e=U(n,s);m(s).push(e)}s||(s=C(`w:pPr`,[]),c.unshift(s));let l=m(s),u=l.findIndex(e=>f(e)===`w:numPr`);u!==-1&&l.splice(u,1);let d=C(`w:numPr`,[C(`w:ilvl`,[],{"w:val":String(i)}),C(`w:numId`,[],{"w:val":r})]);l.unshift(d),Y(e,t)}}function Xn(e,t,n){for(let r of t.targets){let t=M(r,e),i=h(t.xmlRef,`w:pPr`);if(!i)continue;if(n){let e=U(n,i);m(i).push(e)}let a=m(i),o=a.findIndex(e=>f(e)===`w:numPr`);o!==-1&&a.splice(o,1),Y(e,t)}}function Zn(e,t,n){let r=M(t.target,e),i=h(r.xmlRef,`w:pPr`);if(!i)throw Error(`No w:pPr found on target paragraph`);if(n){let e=U(n,i);m(i).push(e)}let a=h(i,`w:numPr`);if(!a)throw Error(`No w:numPr found on target paragraph`);if(t.level!==void 0){let e=h(a,`w:ilvl`);e?v(e,`w:val`,String(t.level)):m(a).unshift(C(`w:ilvl`,[],{"w:val":String(t.level)}))}if(t.listType!==void 0){let n=D(e,t.listType),r=h(a,`w:numId`);r?v(r,`w:val`,n):m(a).push(C(`w:numId`,[],{"w:val":n}))}Y(e,r)}const Qn=new Set([`w:sectPr`,`w:pPrChange`,`w:rPr`]),$n=new Set([`w:ins`,`w:del`,`w:moveFrom`,`w:moveTo`,`w:rPrChange`]);function er(e){let t=h(e,`w:pPr`);if(!t)return;let n=[],r;for(let e of m(t)){let t=f(e);if(t){if(t===`w:rPr`){let t=m(e).filter(e=>{let t=f(e);return t!==void 0&&!$n.has(t)}).map(x);t.length>0&&(r=C(`w:rPr`,t));continue}Qn.has(t)||n.push(x(e))}}if(r&&n.push(r),n.length!==0)return C(`w:pPr`,n)}function tr(e){let t=e.runs[e.runs.length-1];if(!t)return;let n=h(t.xmlRef,`w:rPr`);if(!n)return;let r=m(n).filter(e=>f(e)!==`w:rPrChange`).map(x);if(r.length!==0)return C(`w:rPr`,r)}function nr(e,t,n){let r=h(e,`w:pPr`);r||(r=C(`w:pPr`,[]),m(e).unshift(r));let i=m(r),a=h(r,`w:rPr`);if(!a){a=C(`w:rPr`,[]);let e=i.findIndex(e=>{let t=f(e);return t===`w:sectPr`||t===`w:pPrChange`});e===-1?i.push(a):i.splice(e,0,a)}let o=C(`w:${t}`,[],{"w:id":V(n),"w:author":n.author,"w:date":n.date});m(a).unshift(o)}function rr(e,t){let n=h(e,`w:pPr`);if(!n)return;let r=h(n,`w:rPr`);if(r)return h(r,`w:${t}`)}function ir(e,t,n,r){let i=t.inheritStyle??!0,a=i?er(e.xmlRef):void 0,o=i?tr(e):void 0,s=t.text.split(`
15
- `),c=[];for(let e of s){let t=[];a&&t.push(x(a));let i=J(e,o,r);n?i.length>0&&t.push(H(n,i)):t.push(...i);let s={"w:p":t};n&&nr(s,`ins`,n),c.push(s)}return c}function ar(e,t,n,r,i=!0){let a=Bn(e,t.xmlRef);if(!a)throw Error(`Cannot find parent container for paragraph "${t.id}"`);let o=a.indexOf(t.xmlRef);if(o===-1)throw Error(`Paragraph "${t.id}" not found in its parent`);let s=ir(t,n,r,i);O(e,s,k(e,t.id));let c=n.position===`after`?o+1:o;a.splice(c,0,...s);let l=Hn(e,t.id);if(!l)throw Error(`Paragraph "${t.id}" not found in normalized document`);let u=n.position===`after`?l.index+1:l.index,d=[];for(let n=0;n<s.length;n++){let r=Wn(t.id,l.array),i=T(s[n],r);e.xmlParts.numbering&&j(i,e.xmlParts.numbering),l.array.splice(u+n,0,i),d.push(i)}return d}const or=new Set([`w:hyperlink`,`w:smartTag`,`w:customXml`,`w:dir`,`w:bdo`,`w:moveTo`,`w:sdtContent`,`w:fldSimple`]);function sr(e,t){let n=0;for(;n<e.length;){let r=e[n],i=f(r);if(i===`w:r`){let r=n;for(;r<e.length&&f(e[r])===`w:r`;)r++;let i=e.slice(n,r);for(let e of i)kt(e);e.splice(n,r-n,Tt(t,i)),n++;continue}if(i===`w:ins`){if(_(r,`w:author`)===t.author){e.splice(n,1);continue}sr(m(r),t),n++;continue}if(i===`w:sdt`){let e=h(r,`w:sdtContent`);e&&sr(m(e),t),n++;continue}i&&or.has(i)&&sr(m(r),t),n++}}function cr(e,t){let n=rr(e,`ins`);if(!n||_(n,`w:author`)!==t)return!1;let r=e=>{for(let n of e){let e=f(n);if(e===`w:r`)return!1;if(e===`w:ins`){if(_(n,`w:author`)!==t)return!1;continue}if(e===`w:del`||e===`w:moveFrom`)return!1;if(e===`w:sdt`){let e=h(n,`w:sdtContent`);if(e&&!r(m(e)))return!1;continue}if(e&&or.has(e)&&!r(m(n)))return!1}return!0};return r(m(e))}function lr(e,t){let n=Bn(e,t.xmlRef);if(!n)throw Error(`Cannot find parent container for paragraph "${t.id}"`);let r=Hn(e,t.id);if(!r)throw Error(`Paragraph "${t.id}" not found in normalized document`);let i=r.array===e.body;if(n.filter(e=>{let t=f(e);return t===`w:p`||i&&t===`w:tbl`}).length===1){let n=m(t.xmlRef),r=h(t.xmlRef,`w:pPr`);n.length=0,r&&n.push(r),Y(e,t);return}let a=n.indexOf(t.xmlRef);a!==-1&&n.splice(a,1),r.array.splice(r.index,1)}function ur(e,t,n,r){if(!r){lr(e,t);return}if(cr(t.xmlRef,r.author)){lr(e,t);return}sr(m(t.xmlRef),r),rr(t.xmlRef,`del`)||nr(t.xmlRef,`del`,r),Y(e,t)}function dr(){return[{"?xml":[{"#text":``}],":@":{"@_version":`1.0`,"@_encoding":`UTF-8`,"@_standalone":`yes`}},C(`Relationships`,[],{xmlns:`http://schemas.openxmlformats.org/package/2006/relationships`})]}function fr(){return[C(`Types`,[C(`Default`,[],{Extension:`rels`,ContentType:`application/vnd.openxmlformats-package.relationships+xml`}),C(`Default`,[],{Extension:`xml`,ContentType:`application/xml`})],{xmlns:`http://schemas.openxmlformats.org/package/2006/content-types`})]}function pr(e){return e.find(e=>f(e)===`Relationships`)}function mr(e){return e.find(e=>f(e)===`Types`)}function hr(e){let t=0,n=pr(e);if(n)for(let e of g(n,`Relationship`)){let n=_(e,`Id`);if(n){let e=parseInt(n.replace(/^rId/,``),10);!isNaN(e)&&e>t&&(t=e)}}return`rId${t+1}`}function gr(e,t,n){let r=pr(e);if(!r)throw Error(`Relationships root not found`);for(let e of g(r,`Relationship`))if(_(e,`Type`)===t&&_(e,`Target`)===n)return _(e,`Id`)??``;let i=hr(e);return m(r).push(C(`Relationship`,[],{Id:i,Type:t,Target:n})),i}function _r(e,t){if(!e)return[];let n=pr(e);if(!n)return[];let r=m(n),i=[];for(let e=r.length-1;e>=0;e--){let n=r[e];f(n)===`Relationship`&&_(n,`Type`)===t&&(i.push(_(n,`Target`)??``),r.splice(e,1))}return i}function vr(e,t,n){let r=mr(e);if(!r)return;let i=t.startsWith(`/`)?t:`/${t}`;g(r,`Override`).some(e=>_(e,`PartName`)===i)||m(r).push(C(`Override`,[],{PartName:i,ContentType:n}))}function yr(e,t){if(!e)return;let n=mr(e);if(!n)return;let r=m(n),i=t.startsWith(`/`)?t:`/${t}`,a=r.findIndex(e=>f(e)===`Override`&&_(e,`PartName`)===i);a!==-1&&r.splice(a,1)}function br(e){e.xmlParts.comments||(e.xmlParts.comments=[{"?xml":[{"#text":``}],":@":{"@_version":`1.0`,"@_encoding":`UTF-8`,"@_standalone":`yes`}},C(`w:comments`,[],{"xmlns:w":`http://schemas.openxmlformats.org/wordprocessingml/2006/main`,"xmlns:r":`http://schemas.openxmlformats.org/officeDocument/2006/relationships`})],e.xmlParts.documentRels||(e.xmlParts.documentRels=dr()),gr(e.xmlParts.documentRels,`http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments`,`comments.xml`),e.xmlParts.contentTypes||(e.xmlParts.contentTypes=fr()),vr(e.xmlParts.contentTypes,`/word/comments.xml`,`application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml`));let t=e.xmlParts.comments.find(e=>f(e)===`w:comments`);if(!t)throw Error(`word/comments.xml has no w:comments root`);return t}function xr(e){let t=-1;for(let n of g(e,`w:comment`)){let e=parseInt(_(n,`w:id`)??``,10);!isNaN(e)&&e>t&&(t=e)}return String(t+1)}function Sr(e){return e.split(/\s+/).filter(e=>e.length>0).map(e=>e[0].toUpperCase()).join(``).slice(0,3)||`A`}function Cr(e,t,n,r,i,a,o){let s=a.styles.some(e=>e.id===`CommentText`),c=a.styles.some(e=>e.id===`CommentReference`);return C(`w:comment`,i.split(`
16
- `).map((e,t)=>{let n=[];if(s&&n.push(C(`w:pPr`,[C(`w:pStyle`,[],{"w:val":`CommentText`})])),t===0){let e=[];c&&e.push(C(`w:rPr`,[C(`w:rStyle`,[],{"w:val":`CommentReference`})])),e.push(C(`w:annotationRef`,[])),n.push({"w:r":e})}return n.push(...J(e,void 0,o)),{"w:p":n}}),{"w:id":e,"w:author":t,"w:date":r,"w:initials":n})}function wr(e,t){let n=[];return t.styles.some(e=>e.id===`CommentReference`)&&n.push(C(`w:rPr`,[C(`w:rStyle`,[],{"w:val":`CommentReference`})])),n.push(C(`w:commentReference`,[],{"w:id":e})),{"w:r":n}}function Tr(e,t,n,r){let i=n.author&&n.author.length>0?n.author:r.defaultAuthor,a=n.initials??Sr(i),o;if(n.find!==void 0&&n.find.length>0){let e=n.occurrence??1,i=W(n.find,r.markers??!0),a=P(t.text,i,e);if(a===-1)throw Error(`Text "${i}" not found in paragraph "${t.id}" (occurrence ${e})`);let s=F(t.runs,a,a+i.length);if(s.length===0)throw Error(`No runs found for "${i}" in paragraph "${t.id}"`);if(o=B(t.xmlRef,s),o.length===0)throw Error(`Failed to isolate matched runs in paragraph "${t.id}"`)}let s=br(e),c=xr(s),l=C(`w:commentRangeStart`,[],{"w:id":c}),u=C(`w:commentRangeEnd`,[],{"w:id":c}),d=wr(c,e);if(o){let e=o[0],n=o[o.length-1],r=R(t.xmlRef,e);if(!r)throw Error(`Matched run not found in paragraph`);r.splice(r.indexOf(e),0,l);let i=R(t.xmlRef,n);if(!i)throw Error(`Matched run not found in paragraph`);i.splice(i.indexOf(n)+1,0,u,d)}else{let e=m(t.xmlRef),n=h(t.xmlRef,`w:pPr`),r=n?e.indexOf(n)+1:0;e.splice(r,0,l),e.push(u,d)}m(s).push(Cr(c,i,a,r.date,n.text,e,r.markers??!0))}function Er(e,t,n){return n?.atomic?Or(e,t,n):Dr(e,t,n)}function Dr(e,t,n){let r=[],i=n?.trackRevisions?xt(e,n):void 0;for(let a of t){let t=jr(e,a,i,n);r.push(t)}return r}function Or(e,t,n){let r=kr(e),i=Dr(r,t,{...n,atomic:!1});return i.every(e=>e.success)&&(e.body=r.body,e.headers=r.headers,e.footers=r.footers,e.xmlParts=r.xmlParts),i}function kr(e){let t={};for(let[n,r]of Object.entries(e.xmlParts))if(r!==void 0)if(r instanceof Map){let e=new Map;for(let[t,n]of r)e.set(t,JSON.parse(JSON.stringify(n)));t[n]=e}else t[n]=JSON.parse(JSON.stringify(r));let n={body:[],headers:new Map,footers:new Map,xmlParts:t,styles:e.styles,loadOptions:e.loadOptions};return A(n),n}function Ar(e,t){return e.markers??t?.inlineMarkers??!0}function jr(e,t,n,r){let i=st(t,e);if(!i.valid)return{success:!1,patch:t,error:i.error};let a=Ar(t,r);try{switch(t.type){case`replace_text`:{let r=M(t.target,e);It(r,t,n,a),Y(e,r);break}case`insert_text`:{let r=M(t.target,e);Vt(r,t,n,a),Y(e,r);break}case`delete_text`:{let r=M(t.target,e);Kt(r,t,n,a),Y(e,r);break}case`insert_paragraph`:ar(e,M(t.target,e),t,n,a);break;case`delete_paragraph`:ur(e,M(t.target,e),t,n);break;case`add_comment`:{let n=M(t.target,e);Tr(e,n,t,{defaultAuthor:r?.revisionAuthor??`docxtor`,date:r?.revisionDate??new Date().toISOString(),markers:a}),Y(e,n);break}case`replace_cell_text`:{let r=M(t.target,e);Zt(r,t,n,a),Y(e,r);break}case`add_table_row`:{let r=N(t.target,e);O(e,[Yt(r,t,n)],k(e,r.id),!0),Mr(e,r);break}case`delete_table_row`:{let r=N(t.target,e);Xt(r,t,n),Mr(e,r);break}case`apply_formatting`:{let r=M(t.target,e);en(r,t,n),Y(e,r);break}case`apply_style`:{let r=M(t.target,e);cn(r,t,e,n),Y(e,r);break}case`append_text`:Fr(e,t,n,a);break;case`add_table`:Ir(e,t,n);break;case`add_header`:yn(e,t,n,a);break;case`add_footer`:bn(e,t,n,a);break;case`remove_header`:xn(e,t);break;case`remove_footer`:Sn(e,t);break;case`add_list`:Kn(e,t,n,a);break;case`add_list_item`:qn(e,t,n,a);break;case`remove_list_item`:Jn(e,t,n);break;case`convert_to_list`:Yn(e,t,n);break;case`remove_list`:Xn(e,t,n);break;case`set_list_properties`:Zn(e,t,n);break}return{success:!0,patch:t}}catch(e){return{success:!1,patch:t,error:e instanceof Error?e.message:String(e)}}}function Mr(e,t){let n=E(t.xmlRef,t.id);if(t.rows=n.rows,t.style=n.style,e.xmlParts.numbering)for(let n of t.rows)for(let t of n.cells)for(let n of t.paragraphs)j(n,e.xmlParts.numbering)}function Nr(e){let t=-1,n=-1;for(let r of e.body)if(r.kind===`paragraph`){let e=parseInt(r.id.replace(`p`,``),10);!isNaN(e)&&e>t&&(t=e)}else if(r.kind===`table`){let e=parseInt(r.id.replace(`tbl`,``),10);!isNaN(e)&&e>n&&(n=e)}return{nextPIdx:t+1,nextTblIdx:n+1}}function Pr(e,t){let n=g(e,`w:tr`);for(let e of n){let n=m(e),r=n.find(e=>`w:trPr`in e);r||(r=C(`w:trPr`,[]),n.unshift(r)),m(r).push(Dt(t))}}function Fr(e,t,n,r){if(t.container){Rr(e,t,n,r);return}let i=Ln(e),a=$e(t.text),o=[],s,c;for(let t of a)if(t.kind===`text`){t.lines.some(e=>e.trim().length>0)&&(s=void 0,c=void 0);for(let e of t.lines){let t=J(e,void 0,r);if(n){let e=H(n,t);o.push({"w:p":[e]})}else o.push({"w:p":t})}}else if(t.kind===`table`){let e=Qt(t.headers,t.rows);n&&Pr(e,n),o.push(e)}else{let i;i=s&&c===t.listType?s:D(e,t.listType),s=i,c=t.listType;for(let e of t.items){let t=C(`w:pPr`,[C(`w:numPr`,[C(`w:ilvl`,[],{"w:val":String(e.level)}),C(`w:numId`,[],{"w:val":i})])]),a=J(e.text,void 0,r);if(n){let e=H(n,a);o.push({"w:p":[t,e]})}else o.push({"w:p":[t,...a]})}}O(e,o,k(e,`body`)),Rn(i,o);let{nextPIdx:l,nextTblIdx:u}=Nr(e);for(let t of o)if(`w:tbl`in t){let n=E(t,`tbl${u++}`);e.body.push(n)}else{let n=T(t,`p${l++}`);e.xmlParts.numbering&&j(n,e.xmlParts.numbering),e.body.push(n)}}function Ir(e,t,n){if(t.container){if(t.afterId)throw Error(`"afterId" is not supported when "container" is set`);zr(e,t,n);return}let r=Ln(e),i=Qt(t.headers,t.rows);n&&Pr(i,n),O(e,[i],k(e,`body`));let a,o;if(t.afterId){let n=mt(t.afterId,e);n&&(a=n.xmlRef,o=n.id)}Rn(r,[i],a);let{nextTblIdx:s}=Nr(e),c=E(i,`tbl${s}`);if(o!==void 0){let t=e.body.findIndex(e=>e.id===o);if(t!==-1){e.body.splice(t+1,0,c);return}}e.body.push(c)}function Lr(e,t,n){let r=t===`header`?e.headers:e.footers;for(let e of r.values())if(e.variant===n){let n=t===`header`?`w:hdr`:`w:ftr`,r=e.xmlRef.find(e=>f(e)===n);if(r)return{rootNode:r,hf:e}}}function Rr(e,t,n,r){let i=t.container,a=Lr(e,i.type,i.variant);if(!a)throw Error(`${i.type} with variant "${i.variant}" not found`);let{rootNode:o,hf:s}=a,c=m(o),l=i.type===`header`?`hdr`:`ftr`,u=$e(t.text),d=s.paragraphs.length,f,p;for(let t of u)if(t.kind===`text`){t.lines.some(e=>e.trim().length>0)&&(f=void 0,p=void 0);for(let a of t.lines){let t,u=J(a,void 0,r);t=n?{"w:p":[H(n,u)]}:{"w:p":u},O(e,[t],o),c.push(t);let f=T(t,`${l}.${i.variant}.p${d++}`);s.paragraphs.push(f)}}else if(t.kind===`table`){let r=Qt(t.headers,t.rows);n&&Pr(r,n),O(e,[r],o),c.push(r);let a=s.tables.length,u=E(r,`${l}.${i.variant}.tbl${a}`);s.tables.push(u)}else{let a;a=f&&p===t.listType?f:D(e,t.listType),f=a,p=t.listType;for(let u of t.items){let t=C(`w:pPr`,[C(`w:numPr`,[C(`w:ilvl`,[],{"w:val":String(u.level)}),C(`w:numId`,[],{"w:val":a})])]),f=J(u.text,void 0,r),p;p=n?{"w:p":[t,H(n,f)]}:{"w:p":[t,...f]},O(e,[p],o),c.push(p);let m=T(p,`${l}.${i.variant}.p${d++}`);e.xmlParts.numbering&&j(m,e.xmlParts.numbering),s.paragraphs.push(m)}}}function zr(e,t,n){let r=t.container,i=Lr(e,r.type,r.variant);if(!i)throw Error(`${r.type} with variant "${r.variant}" not found`);let{rootNode:a,hf:o}=i,s=m(a),c=Qt(t.headers,t.rows);n&&Pr(c,n),O(e,[c],a),s.push(c);let l=r.type===`header`?`hdr`:`ftr`,u=o.tables.length,d=E(c,`${l}.${r.variant}.tbl${u}`);o.tables.push(d)}function Br(e,t={}){let n=t.runFilter,r=(t.includeFormatting??!1)||n!==void 0,i=t.includeHeadersFooters??!0,a=t.includeTables??!0,o=t.includeRevisions??!1,s=t.includePageBreaks??!1,c=[];for(let t of e.body)t.kind===`paragraph`?c.push(Hr(t,r,o,s,n)):t.kind===`table`&&a?c.push(Ur(t,r,o,n)):t.kind===`page_break`&&s&&c.push({id:t.id,type:`page_break`});let l={body:c};if(e.styles.length>0&&(l.styles=e.styles),i){if(e.headers.size>0){l.headers=[];for(let t of e.headers.values()){let e={type:`header`,variant:t.variant,paragraphs:t.paragraphs.map(e=>Hr(e,r,o,!1,n))};a&&t.tables.length>0&&(e.tables=t.tables.map(e=>Ur(e,r,o,n))),l.headers.push(e)}}if(e.footers.size>0){l.footers=[];for(let t of e.footers.values()){let e={type:`footer`,variant:t.variant,paragraphs:t.paragraphs.map(e=>Hr(e,r,o,!1,n))};a&&t.tables.length>0&&(e.tables=t.tables.map(e=>Ur(e,r,o,n))),l.footers.push(e)}}}return l}function Vr(e,t){let n=(e,t)=>e!==void 0&&t!==void 0&&e.toUpperCase()===t.toUpperCase();return!(t.bold!==void 0&&(e.bold??!1)!==t.bold||t.italic!==void 0&&(e.italic??!1)!==t.italic||t.strikethrough!==void 0&&(e.strikethrough??!1)!==t.strikethrough||t.underline!==void 0&&!n(e.underline,t.underline)||t.color!==void 0&&!n(e.color,t.color)||t.highlight!==void 0&&!n(e.highlight,t.highlight))}function Hr(e,t,n=!1,r=!1,i){let a={id:e.id,text:e.text};if(e.paraId&&(a.paraId=e.paraId),e.style&&(a.style=e.style),e.listInfo&&(a.listLevel=e.listInfo.level,e.listInfo.listType&&(a.listType=e.listInfo.listType),a.numId=e.listInfo.numId),t&&e.runs.length>0){let t=[];for(let n of e.runs){if(i){if(!Vr(n.formatting,i))continue}else if(!(n.formatting.bold||n.formatting.italic||n.formatting.underline||n.formatting.strikethrough||n.formatting.fontSize||n.formatting.fontFamily||n.formatting.color||n.formatting.highlight))continue;let e={text:n.text,startOffset:n.startOffset,endOffset:n.endOffset};n.formatting.bold&&(e.bold=!0),n.formatting.italic&&(e.italic=!0),n.formatting.underline&&(e.underline=n.formatting.underline),n.formatting.strikethrough&&(e.strikethrough=!0),n.formatting.fontSize&&(e.fontSize=n.formatting.fontSize),n.formatting.fontFamily&&(e.fontFamily=n.formatting.fontFamily),n.formatting.color&&(e.color=n.formatting.color),n.formatting.themeColor&&(e.themeColor=n.formatting.themeColor),n.formatting.highlight&&(e.highlight=n.formatting.highlight),t.push(e)}t.length>0&&(a.formatting=t)}return n&&e.revisions&&e.revisions.length>0&&(a.revisions=e.revisions),r&&e.hasPageBreak&&(a.hasPageBreak=!0,e.pageBreakOffset!==void 0&&(a.pageBreakOffset=e.pageBreakOffset)),a}function Ur(e,t=!1,n=!1,r){let i={id:e.id,style:e.style,rows:e.rows.map(i=>i.cells.map(a=>{let o={id:a.paragraphs[0]?.id??`${e.id}.r${i.index}.c${a.colIndex}.p0`,text:a.paragraphs.map(e=>e.text).join(`
17
- `)};return a.paragraphs[0]?.paraId&&(o.paraId=a.paragraphs[0].paraId),(a.paragraphs.length>1||r!==void 0)&&(o.paragraphs=a.paragraphs.map(e=>Hr(e,t,n,!1,r))),o}))},a=[];for(let i of e.rows)for(let e of i.cells)for(let i of e.nestedTables)a.push(Ur(i,t,n,r));return a.length>0&&(i.nestedTables=a),i}function Wr(e,t){switch(t){case`decimal`:return String(e);case`lowerLetter`:{let t=``,n=e;for(;n>0;)n--,t=String.fromCharCode(97+n%26)+t,n=Math.floor(n/26);return t}case`upperLetter`:{let t=``,n=e;for(;n>0;)n--,t=String.fromCharCode(65+n%26)+t,n=Math.floor(n/26);return t}case`lowerRoman`:return Gr(e).toLowerCase();case`upperRoman`:return Gr(e);default:return String(e)}}function Gr(e){let t=[1e3,900,500,400,100,90,50,40,10,9,5,4,1],n=[`M`,`CM`,`D`,`CD`,`C`,`XC`,`L`,`XL`,`X`,`IX`,`V`,`IV`,`I`],r=``,i=e;for(let e=0;e<t.length;e++)for(;i>=t[e];)r+=n[e],i-=t[e];return r}const Kr=[`decimal`,`lowerLetter`,`lowerRoman`];function qr(e,t,n){if(!e.listInfo)return``;let{numId:r,level:i,listType:a}=e.listInfo,o=` `.repeat(i);for(let e of t.keys()){let[n,a]=e.split(`:`);n===r&&Number(a)>i&&t.delete(e)}let s=`${r}:${i}`,c=(t.get(s)??0)+1;if(t.set(s,c),a===`bullet`||!a)return`${o}- `;if(a===`multilevel`&&n){let e=Pe(n,r,i);if(e){let n=e.pattern,i=n.match(/%(\d+)/g);if(i)for(let a of i){let i=parseInt(a.slice(1),10)-1,o=t.get(`${r}:${i}`)??1,s=e.ancestorFormats.get(i)??`decimal`;n=n.replaceAll(a,Wr(o,s))}return`${o}${n} `}}let l=Kr[i%Kr.length]??`decimal`;if(n){let e=Pe(n,r,i);e&&(l=e.format)}return`${o}${Wr(c,l)}. `}function Jr(e,t={}){let n=t.includeHeadersFooters??!0,r=t.includeTables??!0,i=t.includeRevisions??!1,a=t.includePageBreaks??!1,o=t.includeFormatting??!1,s=[],c=new Map,l=e.xmlParts.numbering;for(let t of e.body)if(t.kind===`paragraph`){let e=qr(t,c,l);s.push(Yr(t,i,a,o,e))}else t.kind===`table`&&r?(s.push(``),s.push(...ei(t,i,o)),s.push(``)):t.kind===`page_break`&&a&&(s.push(``),s.push(`[${t.id}] --- PAGE BREAK ---`),s.push(``));if(n){for(let t of e.headers.values()){s.push(``),s.push(`--- Header (${t.variant}) ---`);let e=new Map;for(let n of t.paragraphs){let t=qr(n,e,l);s.push(Yr(n,i,!1,o,t))}if(r)for(let e of t.tables)s.push(``),s.push(...ei(e,i))}for(let t of e.footers.values()){s.push(``),s.push(`--- Footer (${t.variant}) ---`);let e=new Map;for(let n of t.paragraphs){let t=qr(n,e,l);s.push(Yr(n,i,!1,o,t))}if(r)for(let e of t.tables)s.push(``),s.push(...ei(e,i))}}if(e.styles.length>0){s.push(``),s.push(`--- Available Styles ---`);for(let t of e.styles)s.push(`[${t.type}] ${t.id} — ${t.name}`)}return s.join(`
18
- `).trim()}function Yr(e,t=!1,n=!1,r=!1,i=``){let a=e.style?` (${e.style})`:``,o=r?Zr(e.text,e.runs):e.text,s=t&&e.revisions&&e.revisions.length>0;if(s)if(r){let t=e.revisions.map(t=>({...t,startOffset:X(t.startOffset,e.runs),endOffset:X(t.endOffset,e.runs)}));o=$r(o,t)}else o=$r(e.text,e.revisions);if(n&&e.hasPageBreak){let t=e.pageBreakOffset??0;if(r&&(t=X(t,e.runs)),s)if(r){let n=e.revisions.map(t=>({...t,startOffset:X(t.startOffset,e.runs),endOffset:X(t.endOffset,e.runs)}));t=Qr(t,n)}else t=Qr(t,e.revisions);let n=o.slice(0,t),i=o.slice(t);o=n.length===0?`[PAGE BREAK] ${i}`:i.length===0?`${n} [PAGE BREAK]`:`${n} [PAGE BREAK] ${i}`}let c=e.listInfo?` {list:${e.listInfo.numId} L${e.listInfo.level}}`:``;return`[${Xr(e)}]${a}${c} ${i}${o}`}function Xr(e){return e.paraId?`${e.id} ¶${e.paraId}`:e.id}function Zr(e,t){if(t.length===0)return e;let n=[],r=0;for(let i of t){i.startOffset>r&&n.push(e.slice(r,i.startOffset));let t=e.slice(i.startOffset,i.endOffset),{bold:a,italic:o,strikethrough:s,underline:c}=i.formatting;if(!a&&!o&&!s&&!c)n.push(t);else{let e=t;c&&(e=`<u>${e}</u>`),o&&(e=`_${e}_`),a&&(e=`**${e}**`),s&&(e=`~~${e}~~`),n.push(e)}r=i.endOffset}return r<e.length&&n.push(e.slice(r)),n.join(``)}function X(e,t){let n=0;for(let r of t){let{bold:t,italic:i,strikethrough:a,underline:o}=r.formatting;if(!t&&!i&&!a&&!o)continue;let s=0,c=0;a&&(s+=2,c+=2),t&&(s+=2,c+=2),i&&(s+=1,c+=1),o&&(s+=3,c+=4),e>=r.startOffset&&(n+=s),e>=r.endOffset&&(n+=c)}return e+n}function Qr(e,t){let n=[...t].sort((e,t)=>e.startOffset-t.startOffset),r=0;for(let t of n)t.type===`insert`?(e>=t.startOffset&&(r+=2),e>=t.endOffset&&(r+=2)):t.type===`delete`&&e>=t.startOffset&&(r+=t.text.length+4);return e+r}function $r(e,t){let n=[...t].sort((e,t)=>t.startOffset-e.startOffset),r=e;for(let e of n)if(e.type===`insert`)r=`${r.slice(0,e.startOffset)}{+${r.slice(e.startOffset,e.endOffset)}+}${r.slice(e.endOffset)}`;else if(e.type===`delete`){let t=r.slice(0,e.startOffset),n=r.slice(e.startOffset);r=`${t}{-${e.text}-}${n}`}return r}function ei(e,t=!1,n=!1){let r=[];for(let i of e.rows){let a=i.cells.map(r=>{let a;a=n?r.paragraphs.map(e=>{let n=Zr(e.text,e.runs);if(t&&e.revisions&&e.revisions.length>0){let t=e.revisions.map(t=>({...t,startOffset:X(t.startOffset,e.runs),endOffset:X(t.endOffset,e.runs)}));n=$r(n,t)}return n}).join(` `):t?r.paragraphs.map(e=>e.revisions&&e.revisions.length>0?$r(e.text,e.revisions):e.text).join(` `):r.paragraphs.map(e=>e.text).join(` `);let o=r.paragraphs[0];return`[${o?Xr(o):`${e.id}.r${i.index}.c${r.colIndex}.p0`}] ${a}`});r.push(`| ${a.join(` | `)} |`)}return r}function ti(e,t=`json`,n={}){switch(t){case`json`:return Br(e,n);case`markdown`:return Jr(e,n);default:throw Error(`Unknown projection format: ${t}`)}}const ni={"w:rPrChange":new Set([`w:ins`,`w:del`,`w:moveFrom`,`w:moveTo`]),"w:pPrChange":new Set([`w:rPr`,`w:sectPr`]),"w:trPrChange":new Set([`w:ins`,`w:del`]),"w:tcPrChange":new Set([`w:cellIns`,`w:cellDel`,`w:cellMerge`]),"w:tblPrChange":new Set,"w:tblPrExChange":new Set,"w:tblGridChange":new Set,"w:sectPrChange":new Set},ri=new Set([`w:rPr`,`w:trPr`,`w:tcPr`]),ii=new Set([`w:moveFromRangeStart`,`w:moveToRangeStart`]),ai=new Set([`w:moveFromRangeEnd`,`w:moveToRangeEnd`]),oi=new Set([`w:bookmarkStart`,`w:bookmarkEnd`,`w:proofErr`,`w:permStart`,`w:permEnd`,`w:commentRangeStart`,`w:commentRangeEnd`,`w:customXmlInsRangeStart`,`w:customXmlInsRangeEnd`,`w:customXmlDelRangeStart`,`w:customXmlDelRangeEnd`]);function si(e,t){let n=li(`accept`,t);return ui(e,n),{accepted:n.count}}function ci(e,t){let n=li(`reject`,t);return ui(e,n),{rejected:n.count}}function li(e,t){return{mode:e,authors:t?.authors?new Set(t.authors):void 0,count:0,moveRangeIds:new Set}}function ui(e,t){let n=[e.xmlParts.document,...e.xmlParts.headers.values(),...e.xmlParts.footers.values()];for(let t of[`footnotes`,`endnotes`,`comments`]){let r=e.xmlParts[t];r&&n.push(r)}for(let e of n)t.moveRangeIds=di(e,t),Q(e,void 0,t)}function Z(e,t){if(!t.authors)return!0;let n=_(e,`w:author`);return n!==void 0&&t.authors.has(n)}function di(e,t){let n=new Set,r=e=>{for(let i of e){let e=f(i);if(!e)continue;if(ii.has(e)&&Z(i,t)){let e=_(i,`w:id`);e!==void 0&&n.add(e)}let a=m(i);a.length>0&&r(a)}};return r(e),n}function Q(e,t,n){let r=0;for(;r<e.length;){let i=e[r],a=f(i);if(!a){r++;continue}let o=t!==void 0&&ri.has(t);if((a===`w:ins`||a===`w:moveTo`)&&!o){if(Z(i,n)){n.count++,n.mode===`accept`?e.splice(r,1,...m(i)):e.splice(r,1);continue}Q(m(i),a,n),r++;continue}if((a===`w:del`||a===`w:moveFrom`)&&!o){if(Z(i,n)){if(n.count++,n.mode===`accept`)e.splice(r,1);else{let t=m(i);for(let e of t)fi(e);e.splice(r,1,...t)}continue}Q(m(i),a,n),r++;continue}if(ii.has(a)||ai.has(a)){let t=_(i,`w:id`);if(ii.has(a)?Z(i,n):t!==void 0&&n.moveRangeIds.has(t)){e.splice(r,1);continue}r++;continue}if(a in ni){if(Z(i,n)){n.count++,n.mode===`accept`?e.splice(r,1):mi(e,i,a),r=0;continue}r++;continue}if(a===`w:p`){if(Q(m(i),a,n),hi(e,r,i,n))continue;r++;continue}if(a===`w:tr`){if(_i(e,r,i,n))continue;Q(m(i),a,n),r++;continue}if(a===`w:tc`){if(vi(e,r,i,n))continue;Q(m(i),a,n),r++;continue}let s=m(i);s.length>0&&Q(s,a,n),r++}}function fi(e){let t=f(e);if(t){if(t===`w:r`){let t=m(e);for(let e=0;e<t.length;e++){let n=t[e];if(`w:delText`in n){let r={"w:t":n[`w:delText`]};n[`:@`]&&(r[`:@`]=n[`:@`]),w(pi(r[`w:t`]))&&v(r,`xml:space`,`preserve`),t[e]=r}else if(`w:delInstrText`in n){let r={"w:instrText":n[`w:delInstrText`]};n[`:@`]&&(r[`:@`]=n[`:@`]),t[e]=r}}return}for(let t of m(e))fi(t)}}function pi(e){let t=``;for(let n of e)`#text`in n&&(t+=b(String(n[`#text`])));return t}function mi(e,t,n){let r=ni[n]??new Set,i=m(t).find(e=>f(e)!==void 0),a=i?m(i).map(x):[],o=e.filter(e=>{let n=f(e);return n!==void 0&&r.has(n)&&e!==t});e.length=0,n===`w:rPrChange`?e.push(...o,...a):e.push(...a,...o)}function hi(e,t,n,r){let i=h(n,`w:pPr`);if(!i)return!1;let a=h(i,`w:rPr`);if(!a)return!1;let o=m(a),s=!1,c=o.findIndex(e=>f(e)===`w:ins`);c!==-1&&Z(o[c],r)&&(r.count++,o.splice(c,1),r.mode===`reject`&&(s=!0));let l=o.findIndex(e=>f(e)===`w:del`);if(l!==-1&&Z(o[l],r)&&(r.count++,o.splice(l,1),r.mode===`accept`&&(s=!0)),o.length===0){let e=m(i);if(e.splice(e.indexOf(a),1),e.length===0){let e=m(n);e.splice(e.indexOf(i),1)}}return s?gi(e,t,n):!1}function gi(e,t,n){let r=h(n,`w:pPr`);if(r&&h(r,`w:sectPr`))return!1;let i=t+1;for(;i<e.length;){let t=f(e[i]);if(t===`w:p`)break;if(t===void 0||oi.has(t)){i++;continue}return!1}if(i>=e.length)return!1;let a=e[i],o=m(a),s=h(a,`w:pPr`),c=s?o.indexOf(s)+1:0,l=m(n).filter(e=>f(e)!==`w:pPr`);return o.splice(c,0,...l),e.splice(t,1),!0}function _i(e,t,n,r){let i=h(n,`w:trPr`);if(!i)return!1;let a=m(i),o=a.findIndex(e=>f(e)===`w:ins`);if(o!==-1&&Z(a[o],r)){if(r.count++,r.mode===`reject`)return e.splice(t,1),!0;a.splice(o,1)}let s=a.findIndex(e=>f(e)===`w:del`);if(s!==-1&&Z(a[s],r)){if(r.count++,r.mode===`accept`)return e.splice(t,1),!0;a.splice(s,1)}return!1}function vi(e,t,n,r){let i=h(n,`w:tcPr`);if(!i)return!1;let a=m(i),o=a.findIndex(e=>f(e)===`w:cellIns`);if(o!==-1&&Z(a[o],r)){if(r.count++,r.mode===`reject`)return e.splice(t,1),!0;a.splice(o,1)}let s=a.findIndex(e=>f(e)===`w:cellDel`);if(s!==-1&&Z(a[s],r)){if(r.count++,r.mode===`accept`)return e.splice(t,1),!0;a.splice(s,1)}let c=a.find(e=>f(e)===`w:cellMerge`);return c&&Z(c,r)&&(r.count++,r.mode===`reject`&&yi(a,c),a.splice(a.indexOf(c),1)),!1}function yi(e,t){let n=_(t,`w:vMergeOrig`),r=e.findIndex(e=>f(e)===`w:vMerge`);if(n===void 0){r!==-1&&e.splice(r,1);return}let i={"w:vMerge":[]};if(n===`rest`&&v(i,`w:val`,`restart`),r!==-1)e[r]=i;else{let t=e.findIndex(e=>{let t=f(e);return t===`w:tcBorders`||t===`w:shd`||t===`w:noWrap`||t===`w:tcMar`||t===`w:textDirection`||t===`w:tcFitText`||t===`w:vAlign`||t===`w:hideMark`||t===`w:cellIns`||t===`w:cellDel`||t===`w:cellMerge`||t===`w:tcPrChange`});t===-1?e.push(i):e.splice(t,0,i)}}const bi=`http://schemas.openxmlformats.org/package/2006/metadata/core-properties`,xi=`http://purl.org/dc/elements/1.1/`,Si=`http://purl.org/dc/terms/`,Ci=`http://purl.org/dc/dcmitype/`,wi=`http://www.w3.org/2001/XMLSchema-instance`,Ti={creator:`dc:creator`,lastModifiedBy:`cp:lastModifiedBy`,title:`dc:title`,subject:`dc:subject`,description:`dc:description`,keywords:`cp:keywords`,category:`cp:category`,contentStatus:`cp:contentStatus`,revision:`cp:revision`,created:`dcterms:created`,modified:`dcterms:modified`},Ei=[`creator`,`lastModifiedBy`,`description`,`keywords`,`category`,`contentStatus`],Di=new Set([`Company`,`Manager`,`HyperlinkBase`]);function $(e){let t=e.indexOf(`:`);return t===-1?e:e.slice(t+1)}function Oi(e,t){if(e)return e.find(e=>{let n=f(e);return n!==void 0&&$(n)===t})}function ki(e,t){return m(e).find(e=>{let n=f(e);return n!==void 0&&$(n)===t})}function Ai(e){let t=``;for(let n of m(e))`#text`in n&&(t+=b(String(n[`#text`])));return t}function ji(e,t){let n=f(e);e[n]=t.length>0?[{"#text":y(t)}]:[]}function Mi(e,t){let n=m(e),r=n.findIndex(e=>{let n=f(e);return n!==void 0&&$(n)===t});return r===-1?!1:(n.splice(r,1),!0)}function Ni(){return[{"?xml":[{"#text":``}],":@":{"@_version":`1.0`,"@_encoding":`UTF-8`,"@_standalone":`yes`}},C(`cp:coreProperties`,[],{"xmlns:cp":bi,"xmlns:dc":xi,"xmlns:dcterms":Si,"xmlns:dcmitype":Ci,"xmlns:xsi":wi})]}const Pi={"xmlns:cp":bi,"xmlns:dc":xi,"xmlns:dcterms":Si,"xmlns:dcmitype":Ci,"xmlns:xsi":wi};function Fi(e){e.xmlParts.core||(e.xmlParts.core=Ni(),e.xmlParts.rootRels||(e.xmlParts.rootRels=dr()),gr(e.xmlParts.rootRels,`http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties`,`docProps/core.xml`),e.xmlParts.contentTypes||(e.xmlParts.contentTypes=fr()),vr(e.xmlParts.contentTypes,`/docProps/core.xml`,`application/vnd.openxmlformats-package.core-properties+xml`));let t=Oi(e.xmlParts.core,`coreProperties`);if(!t)throw Error(`docProps/core.xml has no coreProperties root`);for(let[e,n]of Object.entries(Pi))_(t,e)===void 0&&v(t,e,n);return t}function Ii(e){let t={},n=Oi(e.xmlParts.core,`coreProperties`);if(!n)return t;for(let e of Object.keys(Ti)){let r=ki(n,$(Ti[e]));r&&(t[e]=Ai(r))}return t}function Li(e,t){let n=Fi(e);for(let e of Object.keys(t)){let r=t[e];if(r===void 0||!(e in Ti))continue;let i=Ti[e],a=ki(n,$(i));a||(a=C(i,[]),m(n).push(a)),ji(a,r),(e===`created`||e===`modified`)&&v(a,`xsi:type`,`dcterms:W3CDTF`)}}function Ri(e){let t=[],n=Oi(e.xmlParts.core,`coreProperties`);if(n){for(let e of Ei)Mi(n,$(Ti[e]));Li(e,{revision:`1`})}let r=Oi(e.xmlParts.app,`Properties`);if(r){let e=m(r);for(let t=e.length-1;t>=0;t--){let n=f(e[t]);n!==void 0&&Di.has($(n))&&e.splice(t,1)}}let i=_r(e.xmlParts.rootRels,`http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties`);yr(e.xmlParts.contentTypes,`/docProps/custom.xml`),t.push(`docProps/custom.xml`);for(let e of i){let n=e.replace(/^\//,``);n&&!t.includes(n)&&t.push(n)}return{removedParts:t}}var zi=class n{zipEntries;doc;removedParts=new Set;constructor(e,t){this.zipEntries=e,this.doc=t}static async load(t,r){let a;a=typeof t==`string`?await e(t):t;let o=await i(a);return new n(o,qe(o,r))}static async create(e,r){if(e!==void 0)return n.load(e,r);let i=new t;i.file(`[Content_Types].xml`,`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
14
+ `)&&m(e).filter(e=>`w:t`in e).length===1){let e=i[`w:t`];if(Array.isArray(e)){let n=e.find(e=>`#text`in e);n?ne(n,t):e.push({"#text":y(t)})}w(t)&&v(i,`xml:space`,`preserve`);return}let a=n??(e=>e<=0?0:t.length);_t(e,{rPr:r.rPr,text:t,anchored:r.anchored.map(e=>({node:e.node,offset:a(e.offset)}))})}function Xt(e,t,n){if(e.rows.length===0)throw Error(`Table "${e.id}" has no rows to clone from`);if(t.afterRow<-1||t.afterRow>=e.rows.length)throw Error(`Invalid afterRow ${t.afterRow} for table "${e.id}" with ${e.rows.length} rows`);let r=t.afterRow>=0?t.afterRow:0,i=e.rows[r];if(t.cells.length!==i.cells.length)throw Error(`Cell count mismatch: patch has ${t.cells.length} cells, but row has ${i.cells.length} cells`);let a=x(i.xmlRef),o=g(a,`w:tc`);for(let e=0;e<o.length;e++){let n=o[e],r=t.cells[e],i=h(n,`w:p`);if(i){let e=m(i),t=e.filter(e=>`w:r`in e||`w:ins`in e||`w:del`in e);for(let n of t){let t=e.indexOf(n);t!==-1&&e.splice(t,1)}let n={"w:t":[{"#text":y(r)}]};w(r)&&(n[`:@`]||={},n[`:@`][`@_xml:space`]=`preserve`);let a={"w:r":[n]};e.push(a)}}let s=m(e.xmlRef);if(t.afterRow===-1){let e=s.find(e=>`w:tr`in e);if(e){let t=s.indexOf(e);s.splice(t,0,a)}else s.push(a)}else le(e.xmlRef,i.xmlRef,a);if(n){let e=m(a),t=e.find(e=>`w:trPr`in e);t||(t=C(`w:trPr`,[]),e.unshift(t)),m(t).push(Ot(n))}return a}function Zt(e,t,n){if(t.row<0||t.row>=e.rows.length)throw Error(`Invalid row index ${t.row} for table "${e.id}" with ${e.rows.length} rows`);let r=e.rows[t.row];if(n){let e=m(r.xmlRef),t=e.find(e=>`w:trPr`in e);t||(t=C(`w:trPr`,[]),e.unshift(t)),m(t).push(kt(n))}else S(e.xmlRef,r.xmlRef)}function Qt(e,t,n,r=!0){Lt(e,{type:`replace_text`,target:t.target,find:t.find,replace:t.replace,occurrence:t.occurrence},n,r)}function $t(e,t){let n=e?.length??(t.length>0?t[0].length:0),r={"w:val":`single`,"w:sz":`4`,"w:space":`0`,"w:color":`auto`},i=C(`w:tblBorders`,[C(`w:top`,[],r),C(`w:left`,[],r),C(`w:bottom`,[],r),C(`w:right`,[],r),C(`w:insideH`,[],r),C(`w:insideV`,[],r)]),a=C(`w:tblPr`,[C(`w:tblStyle`,[],{"w:val":`TableGrid`}),C(`w:tblW`,[],{"w:w":`0`,"w:type":`auto`}),i,C(`w:tblLook`,[],{"w:val":`04A0`,"w:firstRow":`1`,"w:lastRow":`0`,"w:firstColumn":`1`,"w:lastColumn":`0`,"w:noHBand":`0`,"w:noVBand":`1`})]),o=[];for(let e=0;e<n;e++)o.push(C(`w:gridCol`,[]));let s=C(`w:tblGrid`,o),c=[];e&&c.push(en(e));for(let e of t)c.push(en(e));return C(`w:tbl`,[a,s,...c])}function en(e){let t=[];for(let n of e){let e={"w:t":[{"#text":y(n)}]};w(n)&&(e[`:@`]||={},e[`:@`][`@_xml:space`]=`preserve`);let r={"w:p":[{"w:r":[e]}]},i=C(`w:tc`,[C(`w:tcPr`,[C(`w:tcW`,[],{"w:w":`0`,"w:type":`auto`})]),r]);t.push(i)}return C(`w:tr`,t)}function tn(e,t,n){t.find!==void 0&&t.find.length>0?nn(e,t.find,t.occurrence??1,t.formatting,n):rn(e,t.formatting,n)}function nn(e,t,n,r,i){let a=N(e.text,t,n);if(a===-1)throw Error(`Text "${t}" not found in paragraph "${e.id}" (occurrence ${n})`);let o=a+t.length,s=P(e.runs,a,o);if(s.length===0)throw Error(`No runs found for text range [${a}, ${o}] in paragraph "${e.id}"`);let c=R(e.xmlRef,s);for(let e of c)an(e,r,i)}function rn(e,t,n){for(let r of e.runs)an(r.xmlRef,t,n)}function an(e,t,n){let r=Object.entries(t).some(([,e])=>e!==null&&e!==!1&&e!==void 0),i=h(e,`w:rPr`);if(!i&&!r)return;let a=n&&i?x(i):void 0;if(i||(i=C(`w:rPr`),m(e).unshift(i)),t.bold!==void 0&&on(i,`w:b`,t.bold),t.italic!==void 0&&on(i,`w:i`,t.italic),t.strikethrough!==void 0&&on(i,`w:strike`,t.strikethrough),t.underline!==void 0&&sn(i,`w:u`,`w:val`,t.underline),t.fontSize!==void 0&&sn(i,`w:sz`,`w:val`,t.fontSize===null?null:String(t.fontSize)),t.fontFamily!==void 0&&cn(i,t.fontFamily),t.color!==void 0&&sn(i,`w:color`,`w:val`,t.color),t.highlight!==void 0&&sn(i,`w:highlight`,`w:val`,t.highlight),n){let e=h(i,`w:rPrChange`);e&&S(i,e);let t=Dt(n,a??C(`w:rPr`));m(i).push(t)}}function on(e,t,n){let r=h(e,t);n===!0?r||m(e).push(C(t)):r&&S(e,r)}function sn(e,t,n,r){let i=h(e,t);if(r!==null)if(i)v(i,n,r);else{let i=C(t,[],{[n]:r});m(e).push(i)}else i&&S(e,i)}function cn(e,t){let n=h(e,`w:rFonts`);if(t!==null)if(n)v(n,`w:ascii`,t),v(n,`w:hAnsi`,t);else{let n=C(`w:rFonts`,[],{"w:ascii":t,"w:hAnsi":t});m(e).push(n)}else n&&(ee(n,`w:ascii`),ee(n,`w:hAnsi`),Object.keys(te(n)).length===0&&S(e,n))}function ln(e,t,n,r){let i=n.styles.find(e=>e.id===t.style);if(!i)throw Error(`Style "${t.style}" not found in document styles.xml`);if(t.find!==void 0&&t.find.length>0){if(i.type!==`character`)throw Error(`Style "${t.style}" is type "${i.type}", but character style is required when "find" is specified`);dn(e,t.find,t.occurrence??1,t.style,r)}else{if(i.type!==`paragraph`)throw Error(`Style "${t.style}" is type "${i.type}", but paragraph style is required when "find" is omitted`);un(e,t.style,r)}}function un(e,t,n){let r=e.xmlRef,i=h(r,`w:pPr`),a=n&&i?x(i):void 0;i||(i=C(`w:pPr`),m(r).unshift(i));let o=h(i,`w:pStyle`);if(o?v(o,`w:val`,t):(o=C(`w:pStyle`,[],{"w:val":t}),m(i).unshift(o)),n){let e=h(i,`w:pPrChange`);e&&S(i,e);let t=H(n,a??C(`w:pPr`));m(i).push(t)}}function dn(e,t,n,r,i){let a=N(e.text,t,n);if(a===-1)throw Error(`Text "${t}" not found in paragraph "${e.id}" (occurrence ${n})`);let o=a+t.length,s=P(e.runs,a,o);if(s.length===0)throw Error(`No runs found for text range [${a}, ${o}] in paragraph "${e.id}"`);let c=R(e.xmlRef,s);for(let e of c)fn(e,r,i)}function fn(e,t,n){let r=h(e,`w:rPr`),i=n&&r?x(r):void 0;r||(r=C(`w:rPr`),m(e).unshift(r));let a=h(r,`w:rStyle`);a&&S(r,a);let o=C(`w:rStyle`,[],{"w:val":t});if(m(r).unshift(o),n){let e=h(r,`w:rPrChange`);e&&S(r,e);let t=Dt(n,i??C(`w:rPr`));m(r).push(t)}}const pn=new Set([`PAGE`,`NUMPAGES`,`DATE`,`TIME`,`FILENAME`]);function mn(e){let t=[],n=/\{\{(\w+)\}\}/g,r=0,i;for(;(i=n.exec(e))!==null;){let n=i[1];pn.has(n)&&(i.index>r&&t.push({kind:`text`,text:e.slice(r,i.index)}),t.push({kind:`field`,fieldCode:n}),r=i.index+i[0].length)}return r<e.length&&t.push({kind:`text`,text:e.slice(r)}),t.length===0&&t.push({kind:`text`,text:e}),t}function hn(e){if(!e.includes(``))return``;for(let t=57344;t<=63743;t++){let n=String.fromCharCode(t);if(!e.includes(n))return n}throw Error(`Cannot find a placeholder character for field codes`)}function q(e,t,n=!0){let r=mn(e);if(r.length===1&&r[0].kind===`text`)return gn(W(r[0].text,n),t);let i=r.filter(e=>e.kind===`field`).map(e=>e.fieldCode),a=hn(e),o=W(r.map(e=>e.kind===`text`?e.text:a).join(``),n),s=[],c=[],l=0,u=()=>{c.length>0&&(s.push(...gn(c,t)),c=[])};for(let e of o){let n=e.text.split(a);for(let r=0;r<n.length;r++){let a=n[r];if(a.length>0&&c.push({...e,text:a}),r<n.length-1){u();let n=vn(t,{bold:!!e.bold,italic:!!e.italic,strikethrough:!!e.strikethrough,underline:!!e.underline});s.push(..._n(i[l++],n))}}}return u(),s}function gn(e,t){if(e.some(e=>e.bold||e.italic||e.strikethrough||e.underline))return G(e,t);let n=e.map(e=>e.text).join(``);return n.length>0?[fe(n,t)]:[]}function _n(e,t){let n=()=>t?[JSON.parse(JSON.stringify(t))]:[],r={"w:r":[...n(),C(`w:fldChar`,[],{"w:fldCharType":`begin`})]},i={"w:instrText":[{"#text":` ${e} `}]};return v(i,`xml:space`,`preserve`),[r,{"w:r":[...n(),i]},{"w:r":[...n(),C(`w:fldChar`,[],{"w:fldCharType":`separate`})]},{"w:r":[...n(),C(`w:fldChar`,[],{"w:fldCharType":`end`})]}]}function vn(e,t){let n=t.bold||t.italic||t.strikethrough||t.underline;if(!e&&!n)return;let r=e?JSON.parse(JSON.stringify(e)):C(`w:rPr`,[]);if(!n)return r;let i=m(r);return t.bold&&!i.some(e=>`w:b`in e)&&i.push(C(`w:b`,[])),t.italic&&!i.some(e=>`w:i`in e)&&i.push(C(`w:i`,[])),t.strikethrough&&!i.some(e=>`w:strike`in e)&&i.push(C(`w:strike`,[])),t.underline&&!i.some(e=>`w:u`in e)&&i.push(C(`w:u`,[],{"w:val":`single`})),r}const yn={default:`default`,first:`first`,even:`even`};function bn(e,t,n,r=!0){wn(e,`header`,t.variant,t.paragraphs,n,r)}function xn(e,t,n,r=!0){wn(e,`footer`,t.variant,t.paragraphs,n,r)}function Sn(e,t){Tn(e,`header`,t.variant)}function Cn(e,t){Tn(e,`footer`,t.variant)}function wn(e,t,n,r,i,a=!0){let o=t===`header`?e.xmlParts.headers:e.xmlParts.footers,s=`${t}${En(o,t)}.xml`,c=`word/${s}`,l=t===`header`?`w:hdr`:`w:ftr`,u=[];for(let e of r){let t=q(e,void 0,a);if(i){let e=V(i,t.map(e=>e));u.push({"w:p":[e]})}else u.push({"w:p":t})}let d=[C(l,u,{"xmlns:w":`http://schemas.openxmlformats.org/wordprocessingml/2006/main`,"xmlns:r":`http://schemas.openxmlformats.org/officeDocument/2006/relationships`})],f=Dn(e);On(e,f,s,t),jn(e,c,t),Pn(e,t,n,f),o.set(c,d);let p=De(d,t,n,f);t===`header`?e.headers.set(f,p):e.footers.set(f,p)}function Tn(e,t,n){let r=t===`header`?`w:headerReference`:`w:footerReference`,i=Nn(e);if(!i)throw Error(`No sectPr found in document`);let a=m(i),o=a.find(e=>f(e)===r?(_(e,`w:type`)??`default`)===yn[n]:!1);if(!o)throw Error(`No ${t} with variant "${n}" found in sectPr`);let s=_(o,`r:id`);if(!s)throw Error(`${t} reference has no r:id`);let c=a.indexOf(o);c!==-1&&a.splice(c,1);let l=kn(e,s),u=l?`word/${l}`:void 0;An(e,s),u&&Mn(e,u);let d=t===`header`?e.xmlParts.headers:e.xmlParts.footers;u&&d.delete(u),t===`header`?e.headers.delete(s):e.footers.delete(s)}function En(e,t){let n=0,r=RegExp(`${t}(\\d+)\\.xml$`,`i`);for(let t of e.keys()){let e=t.match(r);if(e){let t=parseInt(e[1],10);t>n&&(n=t)}}return n+1}function Dn(e){let t=e.xmlParts.documentRels;if(!t)return`rId1`;let n=0,r=t.find(e=>f(e)===`Relationships`);if(r){let e=g(r,`Relationship`);for(let t of e){let e=_(t,`Id`);if(e){let t=parseInt(e.replace(/^rId/,``),10);!isNaN(t)&&t>n&&(n=t)}}}return`rId${n+1}`}function On(e,t,n,r){e.xmlParts.documentRels||(e.xmlParts.documentRels=[C(`Relationships`,[],{xmlns:`http://schemas.openxmlformats.org/package/2006/relationships`})]);let i=C(`Relationship`,[],{Id:t,Type:r===`header`?`http://schemas.openxmlformats.org/officeDocument/2006/relationships/header`:`http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer`,Target:n}),a=e.xmlParts.documentRels.find(e=>f(e)===`Relationships`);a&&m(a).push(i)}function kn(e,t){let n=e.xmlParts.documentRels;if(!n)return;let r=n.find(e=>f(e)===`Relationships`);if(!r)return;let i=g(r,`Relationship`);for(let e of i)if(_(e,`Id`)===t)return _(e,`Target`)}function An(e,t){let n=e.xmlParts.documentRels;if(!n)return;let r=n.find(e=>f(e)===`Relationships`);if(!r)return;let i=m(r),a=i.findIndex(e=>f(e)===`Relationship`&&_(e,`Id`)===t);a!==-1&&i.splice(a,1)}function jn(e,t,n){if(!e.xmlParts.contentTypes)return;let r=n===`header`?`application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml`:`application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml`,i=C(`Override`,[],{PartName:`/${t}`,ContentType:r}),a=e.xmlParts.contentTypes.find(e=>f(e)===`Types`);a&&m(a).push(i)}function Mn(e,t){if(!e.xmlParts.contentTypes)return;let n=e.xmlParts.contentTypes.find(e=>f(e)===`Types`);if(!n)return;let r=m(n),i=r.findIndex(e=>{if(f(e)!==`Override`)return!1;let n=_(e,`PartName`);return n===`/${t}`||n===t});i!==-1&&r.splice(i,1)}function Nn(e){let t=e.xmlParts.document.find(e=>f(e)===`w:document`);if(!t)return;let n=h(t,`w:body`);if(!n)return;let r=m(n);for(let e=r.length-1;e>=0;e--)if(f(r[e])===`w:sectPr`)return r[e]}function Pn(e,t,n,r){let i=Nn(e);if(!i){i=C(`w:sectPr`,[]);let t=e.xmlParts.document.find(e=>f(e)===`w:document`);if(!t)throw Error(`Cannot find w:document`);let n=h(t,`w:body`);if(!n)throw Error(`Cannot find w:body`);m(n).push(i)}let a=C(t===`header`?`w:headerReference`:`w:footerReference`,[],{"w:type":yn[n],"r:id":r});if(m(i).unshift(a),n===`first`){let e=m(i);e.some(e=>f(e)===`w:titlePg`)||e.push(C(`w:titlePg`,[]))}n===`even`&&Fn(e)}function Fn(e){e.xmlParts.settings||(e.xmlParts.settings=[C(`w:settings`,[C(`w:evenAndOddHeaders`,[])],{"xmlns:w":`http://schemas.openxmlformats.org/wordprocessingml/2006/main`})]);let t=e.xmlParts.settings.find(e=>f(e)===`w:settings`);if(!t)return;let n=m(t);n.some(e=>f(e)===`w:evenAndOddHeaders`)||n.push(C(`w:evenAndOddHeaders`,[])),In(e),Ln(e)}function In(e){e.xmlParts.documentRels||(e.xmlParts.documentRels=[C(`Relationships`,[],{xmlns:`http://schemas.openxmlformats.org/package/2006/relationships`})]);let t=e.xmlParts.documentRels.find(e=>f(e)===`Relationships`);if(!t)return;let n=`http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings`,r=m(t);r.some(e=>f(e)===`Relationship`?_(e,`Type`)===n&&_(e,`Target`)===`settings.xml`:!1)||r.push(C(`Relationship`,[],{Id:Dn(e),Type:n,Target:`settings.xml`}))}function Ln(e){e.xmlParts.contentTypes||(e.xmlParts.contentTypes=[C(`Types`,[],{xmlns:`http://schemas.openxmlformats.org/package/2006/content-types`})]);let t=e.xmlParts.contentTypes.find(e=>f(e)===`Types`);if(!t)return;let n=`/word/settings.xml`,r=m(t);r.some(e=>f(e)===`Override`?_(e,`PartName`)===n:!1)||r.push(C(`Override`,[],{PartName:n,ContentType:`application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml`}))}function Rn(e){let t=e.xmlParts.document.find(e=>f(e)===`w:document`);if(!t)throw Error(`Cannot find w:document in FXP tree`);let n=h(t,`w:body`);if(!n)throw Error(`Cannot find w:body in FXP tree`);return m(n)}function zn(e,t,n){if(n){let r=e.indexOf(n);if(r!==-1){e.splice(r+1,0,...t);return}}let r=e.findIndex(e=>f(e)===`w:sectPr`);r===-1?e.push(...t):e.splice(r,0,...t)}function Bn(e){let t=-1;for(let n of e.body)if(n.kind===`paragraph`){let e=parseInt(n.id.replace(`p`,``),10);!isNaN(e)&&e>t&&(t=e)}return t+1}function Vn(e,t){let n=Rn(e);if(n.includes(t))return n;for(let e of n)if(f(e)===`w:tbl`){let n=Hn(e,t);if(n)return n}for(let n of[...e.headers.values(),...e.footers.values()]){let e=n.type===`header`?`w:hdr`:`w:ftr`,r=n.xmlRef.find(t=>f(t)===e);if(r){let e=m(r);if(e.includes(t))return e;for(let n of e)if(f(n)===`w:tbl`){let e=Hn(n,t);if(e)return e}}}}function Hn(e,t){for(let n of m(e).filter(e=>f(e)===`w:tr`))for(let e of m(n).filter(e=>f(e)===`w:tc`)){let n=m(e);if(n.includes(t))return n;for(let e of n)if(f(e)===`w:tbl`){let n=Hn(e,t);if(n)return n}}}function Un(e,t){let n=e.body.findIndex(e=>e.id===t);if(n!==-1)return{array:e.body,index:n};for(let n of[...e.headers.values(),...e.footers.values()]){let e=n.paragraphs.findIndex(e=>e.id===t);if(e!==-1)return{array:n.paragraphs,index:e}}let r=[];for(let t of e.body)t.kind===`table`&&r.push(t);for(let t of[...e.headers.values(),...e.footers.values()])r.push(...t.tables);for(let e of r){let n=Wn(e,t);if(n)return n}}function Wn(e,t){for(let n of e.rows)for(let e of n.cells){let n=e.paragraphs.findIndex(e=>e.id===t);if(n!==-1)return{array:e.paragraphs,index:n};for(let n of e.nestedTables){let e=Wn(n,t);if(e)return e}}}function Gn(e,t){let n=e.match(/^(.*p)(\d+)$/);if(!n)return`p${Date.now()}`;let r=n[1],i=-1;for(let e of t)if(e.id.startsWith(r)){let t=e.id.slice(r.length),n=parseInt(t,10);!isNaN(n)&&n>i&&(i=n)}return`${r}${i+1}`}function J(e,t){let n=T(t.xmlRef,t.id);t.text=n.text,t.runs=n.runs,t.style=n.style,t.listInfo=n.listInfo,t.revisions=n.revisions,t.paraId=n.paraId,t.hasPageBreak=n.hasPageBreak,t.pageBreakOffset=n.pageBreakOffset,e.xmlParts.numbering&&A(t,e.xmlParts.numbering)}function Kn(e,t,n,r,i=!0){let a=C(`w:pPr`,[C(`w:numPr`,[C(`w:ilvl`,[],{"w:val":String(n)}),C(`w:numId`,[],{"w:val":t})])]),o=q(e,void 0,i),s;return s=r?[a,V(r,o)]:[a,...o],{"w:p":s}}function qn(e,t,n,r=!0){let i=t.numId??Ne(e,t.listType),a=t.level??0,o=[];for(let e of t.items){let t=typeof e==`string`?e:e.text,s=typeof e==`string`?a:e.level??a;o.push(Kn(t,i,s,n,r))}if(t.container){let n=t.container.type===`header`?e.headers:e.footers;for(let r of n.values())if(r.variant===t.container.variant){let n=r.type===`header`?`w:hdr`:`w:ftr`,i=r.xmlRef.find(e=>f(e)===n);if(i){let n=m(i);D(e,o,i),n.push(...o);let a=t.container.type===`header`?`hdr`:`ftr`,s=r.paragraphs.length;for(let n of o){let i=T(n,`${a}.${t.container.variant}.p${s++}`);e.xmlParts.numbering&&A(i,e.xmlParts.numbering),r.paragraphs.push(i)}}return}return}let s=Rn(e),c,l;if(t.afterId){let n=ht(t.afterId,e);n&&(c=n.xmlRef,l=n.id)}D(e,o,O(e,`body`)),zn(s,o,c);let u=Bn(e),d=l===void 0?-1:e.body.findIndex(e=>e.id===l);for(let t=0;t<o.length;t++){let n=`p${u++}`,r=T(o[t],n);e.xmlParts.numbering&&A(r,e.xmlParts.numbering),d===-1?e.body.push(r):e.body.splice(d+1+t,0,r)}}function Jn(e,t,n,r=!0){let i=j(t.target,e),a=i.listInfo.numId,o=t.level??i.listInfo.level,s=Kn(t.text,a,o,n,r);D(e,[s],O(e,i.id));let c=Vn(e,i.xmlRef);if(!c)throw Error(`Cannot find parent container for target paragraph`);let l=c.indexOf(i.xmlRef);if(l===-1)throw Error(`Target xmlRef not found in parent`);t.position===`after`?c.splice(l+1,0,s):c.splice(l,0,s);let u=Un(e,i.id),d=u?u.array:e.body,f=T(s,Gn(i.id,d));e.xmlParts.numbering&&A(f,e.xmlParts.numbering),u?t.position===`after`?u.array.splice(u.index+1,0,f):u.array.splice(u.index,0,f):e.body.push(f)}function Yn(e,t,n){let r=j(t.target,e);if(n){let t=m(r.xmlRef),i=h(r.xmlRef,`w:pPr`);if(i){let e=H(n,i);m(i).push(e)}for(let e=t.length-1;e>=0;e--){let r=t[e],i=f(r);if(i===`w:r`){let i=x(r);At(i),t[e]=Et(n,[i])}else i===`w:ins`&&t.splice(e,1)}J(e,r);return}let i=Vn(e,r.xmlRef);if(i){let e=i.indexOf(r.xmlRef);e!==-1&&i.splice(e,1)}let a=Un(e,r.id);a&&a.array.splice(a.index,1)}function Xn(e,t,n){let r=Ne(e,t.listType),i=t.level??0;for(let a of t.targets){let t=j(a,e),o=t.xmlRef,s=h(o,`w:pPr`),c=m(o);if(n&&s){let e=H(n,s);m(s).push(e)}s||(s=C(`w:pPr`,[]),c.unshift(s));let l=m(s),u=l.findIndex(e=>f(e)===`w:numPr`);u!==-1&&l.splice(u,1);let d=C(`w:numPr`,[C(`w:ilvl`,[],{"w:val":String(i)}),C(`w:numId`,[],{"w:val":r})]);l.unshift(d),J(e,t)}}function Zn(e,t,n){for(let r of t.targets){let t=j(r,e),i=h(t.xmlRef,`w:pPr`);if(!i)continue;if(n){let e=H(n,i);m(i).push(e)}let a=m(i),o=a.findIndex(e=>f(e)===`w:numPr`);o!==-1&&a.splice(o,1),J(e,t)}}function Qn(e,t,n){let r=j(t.target,e),i=h(r.xmlRef,`w:pPr`);if(!i)throw Error(`No w:pPr found on target paragraph`);if(n){let e=H(n,i);m(i).push(e)}let a=h(i,`w:numPr`);if(!a)throw Error(`No w:numPr found on target paragraph`);if(t.level!==void 0){let e=h(a,`w:ilvl`);e?v(e,`w:val`,String(t.level)):m(a).unshift(C(`w:ilvl`,[],{"w:val":String(t.level)}))}if(t.listType!==void 0){let n=Ne(e,t.listType),r=h(a,`w:numId`);r?v(r,`w:val`,n):m(a).push(C(`w:numId`,[],{"w:val":n}))}J(e,r)}const $n=new Set([`w:sectPr`,`w:pPrChange`,`w:rPr`]),er=new Set([`w:ins`,`w:del`,`w:moveFrom`,`w:moveTo`,`w:rPrChange`]);function tr(e){let t=h(e,`w:pPr`);if(!t)return;let n=[],r;for(let e of m(t)){let t=f(e);if(t){if(t===`w:rPr`){let t=m(e).filter(e=>{let t=f(e);return t!==void 0&&!er.has(t)}).map(x);t.length>0&&(r=C(`w:rPr`,t));continue}$n.has(t)||n.push(x(e))}}if(r&&n.push(r),n.length!==0)return C(`w:pPr`,n)}function nr(e){let t=e.runs[e.runs.length-1];if(!t)return;let n=h(t.xmlRef,`w:rPr`);if(!n)return;let r=m(n).filter(e=>f(e)!==`w:rPrChange`).map(x);if(r.length!==0)return C(`w:rPr`,r)}function rr(e,t,n){let r=h(e,`w:pPr`);r||(r=C(`w:pPr`,[]),m(e).unshift(r));let i=m(r),a=h(r,`w:rPr`);if(!a){a=C(`w:rPr`,[]);let e=i.findIndex(e=>{let t=f(e);return t===`w:sectPr`||t===`w:pPrChange`});e===-1?i.push(a):i.splice(e,0,a)}let o=C(`w:${t}`,[],{"w:id":z(n),"w:author":n.author,"w:date":n.date});m(a).unshift(o)}function ir(e,t){let n=h(e,`w:pPr`);if(!n)return;let r=h(n,`w:rPr`);if(r)return h(r,`w:${t}`)}function ar(e,t,n,r){let i=t.inheritStyle??!0,a=i?tr(e.xmlRef):void 0,o=i?nr(e):void 0,s=t.text.split(`
15
+ `),c=[];for(let e of s){let t=[];a&&t.push(x(a));let i=q(e,o,r);n?i.length>0&&t.push(V(n,i)):t.push(...i);let s={"w:p":t};n&&rr(s,`ins`,n),c.push(s)}return c}function or(e,t,n,r,i=!0){let a=Vn(e,t.xmlRef);if(!a)throw Error(`Cannot find parent container for paragraph "${t.id}"`);let o=a.indexOf(t.xmlRef);if(o===-1)throw Error(`Paragraph "${t.id}" not found in its parent`);let s=ar(t,n,r,i);D(e,s,O(e,t.id));let c=n.position===`after`?o+1:o;a.splice(c,0,...s);let l=Un(e,t.id);if(!l)throw Error(`Paragraph "${t.id}" not found in normalized document`);let u=n.position===`after`?l.index+1:l.index,d=[];for(let n=0;n<s.length;n++){let r=Gn(t.id,l.array),i=T(s[n],r);e.xmlParts.numbering&&A(i,e.xmlParts.numbering),l.array.splice(u+n,0,i),d.push(i)}return d}const sr=new Set([`w:hyperlink`,`w:smartTag`,`w:customXml`,`w:dir`,`w:bdo`,`w:moveTo`,`w:sdtContent`,`w:fldSimple`]);function cr(e,t){let n=0;for(;n<e.length;){let r=e[n],i=f(r);if(i===`w:r`){let r=n;for(;r<e.length&&f(e[r])===`w:r`;)r++;let i=e.slice(n,r);for(let e of i)At(e);e.splice(n,r-n,Et(t,i)),n++;continue}if(i===`w:ins`){if(_(r,`w:author`)===t.author){e.splice(n,1);continue}cr(m(r),t),n++;continue}if(i===`w:sdt`){let e=h(r,`w:sdtContent`);e&&cr(m(e),t),n++;continue}i&&sr.has(i)&&cr(m(r),t),n++}}function lr(e,t){let n=ir(e,`ins`);if(!n||_(n,`w:author`)!==t)return!1;let r=e=>{for(let n of e){let e=f(n);if(e===`w:r`)return!1;if(e===`w:ins`){if(_(n,`w:author`)!==t)return!1;continue}if(e===`w:del`||e===`w:moveFrom`)return!1;if(e===`w:sdt`){let e=h(n,`w:sdtContent`);if(e&&!r(m(e)))return!1;continue}if(e&&sr.has(e)&&!r(m(n)))return!1}return!0};return r(m(e))}function ur(e,t){let n=Vn(e,t.xmlRef);if(!n)throw Error(`Cannot find parent container for paragraph "${t.id}"`);let r=Un(e,t.id);if(!r)throw Error(`Paragraph "${t.id}" not found in normalized document`);let i=r.array===e.body;if(n.filter(e=>{let t=f(e);return t===`w:p`||i&&t===`w:tbl`}).length===1){let n=m(t.xmlRef),r=h(t.xmlRef,`w:pPr`);n.length=0,r&&n.push(r),J(e,t);return}let a=n.indexOf(t.xmlRef);a!==-1&&n.splice(a,1),r.array.splice(r.index,1)}function dr(e,t,n,r){if(!r){ur(e,t);return}if(lr(t.xmlRef,r.author)){ur(e,t);return}cr(m(t.xmlRef),r),ir(t.xmlRef,`del`)||rr(t.xmlRef,`del`,r),J(e,t)}function fr(){return[{"?xml":[{"#text":``}],":@":{"@_version":`1.0`,"@_encoding":`UTF-8`,"@_standalone":`yes`}},C(`Relationships`,[],{xmlns:`http://schemas.openxmlformats.org/package/2006/relationships`})]}function pr(){return[C(`Types`,[C(`Default`,[],{Extension:`rels`,ContentType:`application/vnd.openxmlformats-package.relationships+xml`}),C(`Default`,[],{Extension:`xml`,ContentType:`application/xml`})],{xmlns:`http://schemas.openxmlformats.org/package/2006/content-types`})]}function mr(e){return e.find(e=>f(e)===`Relationships`)}function hr(e){return e.find(e=>f(e)===`Types`)}function gr(e){let t=0,n=mr(e);if(n)for(let e of g(n,`Relationship`)){let n=_(e,`Id`);if(n){let e=parseInt(n.replace(/^rId/,``),10);!isNaN(e)&&e>t&&(t=e)}}return`rId${t+1}`}function _r(e,t,n){let r=mr(e);if(!r)throw Error(`Relationships root not found`);for(let e of g(r,`Relationship`))if(_(e,`Type`)===t&&_(e,`Target`)===n)return _(e,`Id`)??``;let i=gr(e);return m(r).push(C(`Relationship`,[],{Id:i,Type:t,Target:n})),i}function vr(e,t){if(!e)return[];let n=mr(e);if(!n)return[];let r=m(n),i=[];for(let e=r.length-1;e>=0;e--){let n=r[e];f(n)===`Relationship`&&_(n,`Type`)===t&&(i.push(_(n,`Target`)??``),r.splice(e,1))}return i}function yr(e,t,n){let r=hr(e);if(!r)return;let i=t.startsWith(`/`)?t:`/${t}`;g(r,`Override`).some(e=>_(e,`PartName`)===i)||m(r).push(C(`Override`,[],{PartName:i,ContentType:n}))}function br(e,t){if(!e)return;let n=hr(e);if(!n)return;let r=m(n),i=t.startsWith(`/`)?t:`/${t}`,a=r.findIndex(e=>f(e)===`Override`&&_(e,`PartName`)===i);a!==-1&&r.splice(a,1)}function xr(e){e.xmlParts.comments||(e.xmlParts.comments=[{"?xml":[{"#text":``}],":@":{"@_version":`1.0`,"@_encoding":`UTF-8`,"@_standalone":`yes`}},C(`w:comments`,[],{"xmlns:w":`http://schemas.openxmlformats.org/wordprocessingml/2006/main`,"xmlns:r":`http://schemas.openxmlformats.org/officeDocument/2006/relationships`})],e.xmlParts.documentRels||(e.xmlParts.documentRels=fr()),_r(e.xmlParts.documentRels,`http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments`,`comments.xml`),e.xmlParts.contentTypes||(e.xmlParts.contentTypes=pr()),yr(e.xmlParts.contentTypes,`/word/comments.xml`,`application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml`));let t=e.xmlParts.comments.find(e=>f(e)===`w:comments`);if(!t)throw Error(`word/comments.xml has no w:comments root`);return t}function Sr(e){let t=-1;for(let n of g(e,`w:comment`)){let e=parseInt(_(n,`w:id`)??``,10);!isNaN(e)&&e>t&&(t=e)}return String(t+1)}function Cr(e){return e.split(/\s+/).filter(e=>e.length>0).map(e=>e[0].toUpperCase()).join(``).slice(0,3)||`A`}function wr(e,t,n,r,i,a,o){let s=a.styles.some(e=>e.id===`CommentText`),c=a.styles.some(e=>e.id===`CommentReference`);return C(`w:comment`,i.split(`
16
+ `).map((e,t)=>{let n=[];if(s&&n.push(C(`w:pPr`,[C(`w:pStyle`,[],{"w:val":`CommentText`})])),t===0){let e=[];c&&e.push(C(`w:rPr`,[C(`w:rStyle`,[],{"w:val":`CommentReference`})])),e.push(C(`w:annotationRef`,[])),n.push({"w:r":e})}return n.push(...q(e,void 0,o)),{"w:p":n}}),{"w:id":e,"w:author":t,"w:date":r,"w:initials":n})}function Tr(e,t){let n=[];return t.styles.some(e=>e.id===`CommentReference`)&&n.push(C(`w:rPr`,[C(`w:rStyle`,[],{"w:val":`CommentReference`})])),n.push(C(`w:commentReference`,[],{"w:id":e})),{"w:r":n}}function Er(e,t,n,r){let i=n.author&&n.author.length>0?n.author:r.defaultAuthor,a=n.initials??Cr(i),o;if(n.find!==void 0&&n.find.length>0){let e=n.occurrence??1,i=U(n.find,r.markers??!0),a=N(t.text,i,e);if(a===-1)throw Error(`Text "${i}" not found in paragraph "${t.id}" (occurrence ${e})`);let s=P(t.runs,a,a+i.length);if(s.length===0)throw Error(`No runs found for "${i}" in paragraph "${t.id}"`);if(o=R(t.xmlRef,s),o.length===0)throw Error(`Failed to isolate matched runs in paragraph "${t.id}"`)}let s=xr(e),c=Sr(s),l=C(`w:commentRangeStart`,[],{"w:id":c}),u=C(`w:commentRangeEnd`,[],{"w:id":c}),d=Tr(c,e);if(o){let e=o[0],n=o[o.length-1],r=I(t.xmlRef,e);if(!r)throw Error(`Matched run not found in paragraph`);r.splice(r.indexOf(e),0,l);let i=I(t.xmlRef,n);if(!i)throw Error(`Matched run not found in paragraph`);i.splice(i.indexOf(n)+1,0,u,d)}else{let e=m(t.xmlRef),n=h(t.xmlRef,`w:pPr`),r=n?e.indexOf(n)+1:0;e.splice(r,0,l),e.push(u,d)}m(s).push(wr(c,i,a,r.date,n.text,e,r.markers??!0))}function Dr(e,t,n){return n?.atomic?kr(e,t,n):Or(e,t,n)}function Or(e,t,n){let r=[],i=n?.trackRevisions?Ct(e,n):void 0;for(let a of t){let t=Mr(e,a,i,n);r.push(t)}return r}function kr(e,t,n){let r=Ar(e),i=Or(r,t,{...n,atomic:!1});return i.every(e=>e.success)&&(e.body=r.body,e.headers=r.headers,e.footers=r.footers,e.xmlParts=r.xmlParts),i}function Ar(e){let t={};for(let[n,r]of Object.entries(e.xmlParts))if(r!==void 0)if(r instanceof Map){let e=new Map;for(let[t,n]of r)e.set(t,JSON.parse(JSON.stringify(n)));t[n]=e}else t[n]=JSON.parse(JSON.stringify(r));let n={body:[],headers:new Map,footers:new Map,xmlParts:t,styles:e.styles,loadOptions:e.loadOptions};return k(n),n}function jr(e,t){return e.markers??t?.inlineMarkers??!0}function Mr(e,t,n,r){let i=ct(t,e);if(!i.valid)return{success:!1,patch:t,error:i.error};let a=jr(t,r);try{switch(t.type){case`replace_text`:{let r=j(t.target,e);Lt(r,t,n,a),J(e,r);break}case`insert_text`:{let r=j(t.target,e);Ht(r,t,n,a),J(e,r);break}case`delete_text`:{let r=j(t.target,e);qt(r,t,n,a),J(e,r);break}case`insert_paragraph`:or(e,j(t.target,e),t,n,a);break;case`delete_paragraph`:dr(e,j(t.target,e),t,n);break;case`add_comment`:{let n=j(t.target,e);Er(e,n,t,{defaultAuthor:r?.revisionAuthor??`docxtor`,date:r?.revisionDate??new Date().toISOString(),markers:a}),J(e,n);break}case`replace_cell_text`:{let r=j(t.target,e);Qt(r,t,n,a),J(e,r);break}case`add_table_row`:{let r=M(t.target,e);D(e,[Xt(r,t,n)],O(e,r.id),!0),Nr(e,r);break}case`delete_table_row`:{let r=M(t.target,e);Zt(r,t,n),Nr(e,r);break}case`apply_formatting`:{let r=j(t.target,e);tn(r,t,n),J(e,r);break}case`apply_style`:{let r=j(t.target,e);ln(r,t,e,n),J(e,r);break}case`append_text`:Ir(e,t,n,a);break;case`add_table`:Lr(e,t,n);break;case`add_header`:bn(e,t,n,a);break;case`add_footer`:xn(e,t,n,a);break;case`remove_header`:Sn(e,t);break;case`remove_footer`:Cn(e,t);break;case`add_list`:qn(e,t,n,a);break;case`add_list_item`:Jn(e,t,n,a);break;case`remove_list_item`:Yn(e,t,n);break;case`convert_to_list`:Xn(e,t,n);break;case`remove_list`:Zn(e,t,n);break;case`set_list_properties`:Qn(e,t,n);break}return{success:!0,patch:t}}catch(e){return{success:!1,patch:t,error:e instanceof Error?e.message:String(e)}}}function Nr(e,t){let n=E(t.xmlRef,t.id);if(t.rows=n.rows,t.style=n.style,e.xmlParts.numbering)for(let n of t.rows)for(let t of n.cells)for(let n of t.paragraphs)A(n,e.xmlParts.numbering)}function Pr(e){let t=-1,n=-1;for(let r of e.body)if(r.kind===`paragraph`){let e=parseInt(r.id.replace(`p`,``),10);!isNaN(e)&&e>t&&(t=e)}else if(r.kind===`table`){let e=parseInt(r.id.replace(`tbl`,``),10);!isNaN(e)&&e>n&&(n=e)}return{nextPIdx:t+1,nextTblIdx:n+1}}function Fr(e,t){let n=g(e,`w:tr`);for(let e of n){let n=m(e),r=n.find(e=>`w:trPr`in e);r||(r=C(`w:trPr`,[]),n.unshift(r)),m(r).push(Ot(t))}}function Ir(e,t,n,r){if(t.container){zr(e,t,n,r);return}let i=Rn(e),a=et(t.text),o=[],s,c;for(let t of a)if(t.kind===`text`){t.lines.some(e=>e.trim().length>0)&&(s=void 0,c=void 0);for(let e of t.lines){let t=q(e,void 0,r);if(n){let e=V(n,t);o.push({"w:p":[e]})}else o.push({"w:p":t})}}else if(t.kind===`table`){let e=$t(t.headers,t.rows);n&&Fr(e,n),o.push(e)}else{let i;i=s&&c===t.listType?s:Ne(e,t.listType),s=i,c=t.listType;for(let e of t.items){let t=C(`w:pPr`,[C(`w:numPr`,[C(`w:ilvl`,[],{"w:val":String(e.level)}),C(`w:numId`,[],{"w:val":i})])]),a=q(e.text,void 0,r);if(n){let e=V(n,a);o.push({"w:p":[t,e]})}else o.push({"w:p":[t,...a]})}}D(e,o,O(e,`body`)),zn(i,o);let{nextPIdx:l,nextTblIdx:u}=Pr(e);for(let t of o)if(`w:tbl`in t){let n=E(t,`tbl${u++}`);e.body.push(n)}else{let n=T(t,`p${l++}`);e.xmlParts.numbering&&A(n,e.xmlParts.numbering),e.body.push(n)}}function Lr(e,t,n){if(t.container){if(t.afterId)throw Error(`"afterId" is not supported when "container" is set`);Br(e,t,n);return}let r=Rn(e),i=$t(t.headers,t.rows);n&&Fr(i,n),D(e,[i],O(e,`body`));let a,o;if(t.afterId){let n=ht(t.afterId,e);n&&(a=n.xmlRef,o=n.id)}zn(r,[i],a);let{nextTblIdx:s}=Pr(e),c=E(i,`tbl${s}`);if(o!==void 0){let t=e.body.findIndex(e=>e.id===o);if(t!==-1){e.body.splice(t+1,0,c);return}}e.body.push(c)}function Rr(e,t,n){let r=t===`header`?e.headers:e.footers;for(let e of r.values())if(e.variant===n){let n=t===`header`?`w:hdr`:`w:ftr`,r=e.xmlRef.find(e=>f(e)===n);if(r)return{rootNode:r,hf:e}}}function zr(e,t,n,r){let i=t.container,a=Rr(e,i.type,i.variant);if(!a)throw Error(`${i.type} with variant "${i.variant}" not found`);let{rootNode:o,hf:s}=a,c=m(o),l=i.type===`header`?`hdr`:`ftr`,u=et(t.text),d=s.paragraphs.length,f,p;for(let t of u)if(t.kind===`text`){t.lines.some(e=>e.trim().length>0)&&(f=void 0,p=void 0);for(let a of t.lines){let t,u=q(a,void 0,r);t=n?{"w:p":[V(n,u)]}:{"w:p":u},D(e,[t],o),c.push(t);let f=T(t,`${l}.${i.variant}.p${d++}`);s.paragraphs.push(f)}}else if(t.kind===`table`){let r=$t(t.headers,t.rows);n&&Fr(r,n),D(e,[r],o),c.push(r);let a=s.tables.length,u=E(r,`${l}.${i.variant}.tbl${a}`);s.tables.push(u)}else{let a;a=f&&p===t.listType?f:Ne(e,t.listType),f=a,p=t.listType;for(let u of t.items){let t=C(`w:pPr`,[C(`w:numPr`,[C(`w:ilvl`,[],{"w:val":String(u.level)}),C(`w:numId`,[],{"w:val":a})])]),f=q(u.text,void 0,r),p;p=n?{"w:p":[t,V(n,f)]}:{"w:p":[t,...f]},D(e,[p],o),c.push(p);let m=T(p,`${l}.${i.variant}.p${d++}`);e.xmlParts.numbering&&A(m,e.xmlParts.numbering),s.paragraphs.push(m)}}}function Br(e,t,n){let r=t.container,i=Rr(e,r.type,r.variant);if(!i)throw Error(`${r.type} with variant "${r.variant}" not found`);let{rootNode:a,hf:o}=i,s=m(a),c=$t(t.headers,t.rows);n&&Fr(c,n),D(e,[c],a),s.push(c);let l=r.type===`header`?`hdr`:`ftr`,u=o.tables.length,d=E(c,`${l}.${r.variant}.tbl${u}`);o.tables.push(d)}function Vr(e,t={}){let n=t.runFilter,r=(t.includeFormatting??!1)||n!==void 0,i=t.includeHeadersFooters??!0,a=t.includeTables??!0,o=t.includeRevisions??!1,s=t.includePageBreaks??!1,c=[];for(let t of e.body)t.kind===`paragraph`?c.push(Ur(t,r,o,s,n)):t.kind===`table`&&a?c.push(Wr(t,r,o,n)):t.kind===`page_break`&&s&&c.push({id:t.id,type:`page_break`});let l={body:c};if(e.styles.length>0&&(l.styles=e.styles),i){if(e.headers.size>0){l.headers=[];for(let t of e.headers.values()){let e={type:`header`,variant:t.variant,paragraphs:t.paragraphs.map(e=>Ur(e,r,o,!1,n))};a&&t.tables.length>0&&(e.tables=t.tables.map(e=>Wr(e,r,o,n))),l.headers.push(e)}}if(e.footers.size>0){l.footers=[];for(let t of e.footers.values()){let e={type:`footer`,variant:t.variant,paragraphs:t.paragraphs.map(e=>Ur(e,r,o,!1,n))};a&&t.tables.length>0&&(e.tables=t.tables.map(e=>Wr(e,r,o,n))),l.footers.push(e)}}}return l}function Hr(e,t){let n=(e,t)=>e!==void 0&&t!==void 0&&e.toUpperCase()===t.toUpperCase();return!(t.bold!==void 0&&(e.bold??!1)!==t.bold||t.italic!==void 0&&(e.italic??!1)!==t.italic||t.strikethrough!==void 0&&(e.strikethrough??!1)!==t.strikethrough||t.underline!==void 0&&!n(e.underline,t.underline)||t.color!==void 0&&!n(e.color,t.color)||t.highlight!==void 0&&!n(e.highlight,t.highlight))}function Ur(e,t,n=!1,r=!1,i){let a={id:e.id,text:e.text};if(e.paraId&&(a.paraId=e.paraId),e.style&&(a.style=e.style),e.listInfo&&(a.listLevel=e.listInfo.level,e.listInfo.listType&&(a.listType=e.listInfo.listType),a.numId=e.listInfo.numId),t&&e.runs.length>0){let t=[];for(let n of e.runs){if(i){if(!Hr(n.formatting,i))continue}else if(!(n.formatting.bold||n.formatting.italic||n.formatting.underline||n.formatting.strikethrough||n.formatting.fontSize||n.formatting.fontFamily||n.formatting.color||n.formatting.highlight))continue;let e={text:n.text,startOffset:n.startOffset,endOffset:n.endOffset};n.formatting.bold&&(e.bold=!0),n.formatting.italic&&(e.italic=!0),n.formatting.underline&&(e.underline=n.formatting.underline),n.formatting.strikethrough&&(e.strikethrough=!0),n.formatting.fontSize&&(e.fontSize=n.formatting.fontSize),n.formatting.fontFamily&&(e.fontFamily=n.formatting.fontFamily),n.formatting.color&&(e.color=n.formatting.color),n.formatting.themeColor&&(e.themeColor=n.formatting.themeColor),n.formatting.highlight&&(e.highlight=n.formatting.highlight),t.push(e)}t.length>0&&(a.formatting=t)}return n&&e.revisions&&e.revisions.length>0&&(a.revisions=e.revisions),r&&e.hasPageBreak&&(a.hasPageBreak=!0,e.pageBreakOffset!==void 0&&(a.pageBreakOffset=e.pageBreakOffset)),a}function Wr(e,t=!1,n=!1,r){let i={id:e.id,style:e.style,rows:e.rows.map(i=>i.cells.map(a=>{let o={id:a.paragraphs[0]?.id??`${e.id}.r${i.index}.c${a.colIndex}.p0`,text:a.paragraphs.map(e=>e.text).join(`
17
+ `)};return a.paragraphs[0]?.paraId&&(o.paraId=a.paragraphs[0].paraId),(a.paragraphs.length>1||r!==void 0)&&(o.paragraphs=a.paragraphs.map(e=>Ur(e,t,n,!1,r))),o}))},a=[];for(let i of e.rows)for(let e of i.cells)for(let i of e.nestedTables)a.push(Wr(i,t,n,r));return a.length>0&&(i.nestedTables=a),i}function Gr(e,t){switch(t){case`decimal`:return String(e);case`lowerLetter`:{let t=``,n=e;for(;n>0;)n--,t=String.fromCharCode(97+n%26)+t,n=Math.floor(n/26);return t}case`upperLetter`:{let t=``,n=e;for(;n>0;)n--,t=String.fromCharCode(65+n%26)+t,n=Math.floor(n/26);return t}case`lowerRoman`:return Kr(e).toLowerCase();case`upperRoman`:return Kr(e);default:return String(e)}}function Kr(e){let t=[1e3,900,500,400,100,90,50,40,10,9,5,4,1],n=[`M`,`CM`,`D`,`CD`,`C`,`XC`,`L`,`XL`,`X`,`IX`,`V`,`IV`,`I`],r=``,i=e;for(let e=0;e<t.length;e++)for(;i>=t[e];)r+=n[e],i-=t[e];return r}const qr=[`decimal`,`lowerLetter`,`lowerRoman`];function Jr(e,t,n){if(!e.listInfo)return``;let{numId:r,level:i,listType:a}=e.listInfo,o=` `.repeat(i);for(let e of t.keys()){let[n,a]=e.split(`:`);n===r&&Number(a)>i&&t.delete(e)}let s=`${r}:${i}`,c=(t.get(s)??0)+1;if(t.set(s,c),a===`bullet`||!a)return`${o}- `;if(a===`multilevel`&&n){let e=Fe(n,r,i);if(e){let n=e.pattern,i=n.match(/%(\d+)/g);if(i)for(let a of i){let i=parseInt(a.slice(1),10)-1,o=t.get(`${r}:${i}`)??1,s=e.ancestorFormats.get(i)??`decimal`;n=n.replaceAll(a,Gr(o,s))}return`${o}${n} `}}let l=qr[i%qr.length]??`decimal`;if(n){let e=Fe(n,r,i);e&&(l=e.format)}return`${o}${Gr(c,l)}. `}function Yr(e,t={}){let n=t.includeHeadersFooters??!0,r=t.includeTables??!0,i=t.includeRevisions??!1,a=t.includePageBreaks??!1,o=t.includeFormatting??!1,s=[],c=new Map,l=e.xmlParts.numbering;for(let t of e.body)if(t.kind===`paragraph`){let e=Jr(t,c,l);s.push(Xr(t,i,a,o,e))}else t.kind===`table`&&r?(s.push(``),s.push(...ti(t,i,o)),s.push(``)):t.kind===`page_break`&&a&&(s.push(``),s.push(`[${t.id}] --- PAGE BREAK ---`),s.push(``));if(n){for(let t of e.headers.values()){s.push(``),s.push(`--- Header (${t.variant}) ---`);let e=new Map;for(let n of t.paragraphs){let t=Jr(n,e,l);s.push(Xr(n,i,!1,o,t))}if(r)for(let e of t.tables)s.push(``),s.push(...ti(e,i))}for(let t of e.footers.values()){s.push(``),s.push(`--- Footer (${t.variant}) ---`);let e=new Map;for(let n of t.paragraphs){let t=Jr(n,e,l);s.push(Xr(n,i,!1,o,t))}if(r)for(let e of t.tables)s.push(``),s.push(...ti(e,i))}}if(e.styles.length>0){s.push(``),s.push(`--- Available Styles ---`);for(let t of e.styles)s.push(`[${t.type}] ${t.id} — ${t.name}`)}return s.join(`
18
+ `).trim()}function Xr(e,t=!1,n=!1,r=!1,i=``){let a=e.style?` (${e.style})`:``,o=r?Qr(e.text,e.runs):e.text,s=t&&e.revisions&&e.revisions.length>0;if(s)if(r){let t=e.revisions.map(t=>({...t,startOffset:Y(t.startOffset,e.runs),endOffset:Y(t.endOffset,e.runs)}));o=ei(o,t)}else o=ei(e.text,e.revisions);if(n&&e.hasPageBreak){let t=e.pageBreakOffset??0;if(r&&(t=Y(t,e.runs)),s)if(r){let n=e.revisions.map(t=>({...t,startOffset:Y(t.startOffset,e.runs),endOffset:Y(t.endOffset,e.runs)}));t=$r(t,n)}else t=$r(t,e.revisions);let n=o.slice(0,t),i=o.slice(t);o=n.length===0?`[PAGE BREAK] ${i}`:i.length===0?`${n} [PAGE BREAK]`:`${n} [PAGE BREAK] ${i}`}let c=e.listInfo?` {list:${e.listInfo.numId} L${e.listInfo.level}}`:``;return`[${Zr(e)}]${a}${c} ${i}${o}`}function Zr(e){return e.paraId?`${e.id} ¶${e.paraId}`:e.id}function Qr(e,t){if(t.length===0)return e;let n=[],r=0;for(let i of t){i.startOffset>r&&n.push(e.slice(r,i.startOffset));let t=e.slice(i.startOffset,i.endOffset),{bold:a,italic:o,strikethrough:s,underline:c}=i.formatting;if(!a&&!o&&!s&&!c)n.push(t);else{let e=t;c&&(e=`<u>${e}</u>`),o&&(e=`_${e}_`),a&&(e=`**${e}**`),s&&(e=`~~${e}~~`),n.push(e)}r=i.endOffset}return r<e.length&&n.push(e.slice(r)),n.join(``)}function Y(e,t){let n=0;for(let r of t){let{bold:t,italic:i,strikethrough:a,underline:o}=r.formatting;if(!t&&!i&&!a&&!o)continue;let s=0,c=0;a&&(s+=2,c+=2),t&&(s+=2,c+=2),i&&(s+=1,c+=1),o&&(s+=3,c+=4),e>=r.startOffset&&(n+=s),e>=r.endOffset&&(n+=c)}return e+n}function $r(e,t){let n=[...t].sort((e,t)=>e.startOffset-t.startOffset),r=0;for(let t of n)t.type===`insert`?(e>=t.startOffset&&(r+=2),e>=t.endOffset&&(r+=2)):t.type===`delete`&&e>=t.startOffset&&(r+=t.text.length+4);return e+r}function ei(e,t){let n=[...t].sort((e,t)=>t.startOffset-e.startOffset),r=e;for(let e of n)if(e.type===`insert`)r=`${r.slice(0,e.startOffset)}{+${r.slice(e.startOffset,e.endOffset)}+}${r.slice(e.endOffset)}`;else if(e.type===`delete`){let t=r.slice(0,e.startOffset),n=r.slice(e.startOffset);r=`${t}{-${e.text}-}${n}`}return r}function ti(e,t=!1,n=!1){let r=[];for(let i of e.rows){let a=i.cells.map(r=>{let a;a=n?r.paragraphs.map(e=>{let n=Qr(e.text,e.runs);if(t&&e.revisions&&e.revisions.length>0){let t=e.revisions.map(t=>({...t,startOffset:Y(t.startOffset,e.runs),endOffset:Y(t.endOffset,e.runs)}));n=ei(n,t)}return n}).join(` `):t?r.paragraphs.map(e=>e.revisions&&e.revisions.length>0?ei(e.text,e.revisions):e.text).join(` `):r.paragraphs.map(e=>e.text).join(` `);let o=r.paragraphs[0];return`[${o?Zr(o):`${e.id}.r${i.index}.c${r.colIndex}.p0`}] ${a}`});r.push(`| ${a.join(` | `)} |`)}return r}function ni(e,t=`json`,n={}){switch(t){case`json`:return Vr(e,n);case`markdown`:return Yr(e,n);default:throw Error(`Unknown projection format: ${t}`)}}const ri={"w:rPrChange":new Set([`w:ins`,`w:del`,`w:moveFrom`,`w:moveTo`]),"w:pPrChange":new Set([`w:rPr`,`w:sectPr`]),"w:trPrChange":new Set([`w:ins`,`w:del`]),"w:tcPrChange":new Set([`w:cellIns`,`w:cellDel`,`w:cellMerge`]),"w:tblPrChange":new Set,"w:tblPrExChange":new Set,"w:tblGridChange":new Set,"w:sectPrChange":new Set},ii=new Set([`w:rPr`,`w:trPr`,`w:tcPr`]),ai=new Set([`w:moveFromRangeStart`,`w:moveToRangeStart`]),oi=new Set([`w:moveFromRangeEnd`,`w:moveToRangeEnd`]),si=new Set([`w:bookmarkStart`,`w:bookmarkEnd`,`w:proofErr`,`w:permStart`,`w:permEnd`,`w:commentRangeStart`,`w:commentRangeEnd`,`w:customXmlInsRangeStart`,`w:customXmlInsRangeEnd`,`w:customXmlDelRangeStart`,`w:customXmlDelRangeEnd`]);function ci(e,t){let n=ui(`accept`,t);return di(e,n),{accepted:n.count}}function li(e,t){let n=ui(`reject`,t);return di(e,n),{rejected:n.count}}function ui(e,t){return{mode:e,authors:t?.authors?new Set(t.authors):void 0,ids:t?.ids?new Set(t.ids.map(String)):void 0,count:0,moveRangeIds:new Set}}function di(e,t){let n=[e.xmlParts.document,...e.xmlParts.headers.values(),...e.xmlParts.footers.values()];for(let t of[`footnotes`,`endnotes`,`comments`]){let r=e.xmlParts[t];r&&n.push(r)}for(let e of n)t.ids&&fi(e,t.ids),t.moveRangeIds=pi(e,t),Z(e,void 0,t)}function X(e,t){if(t.ids){let n=_(e,`w:id`);if(n===void 0||!t.ids.has(n))return!1}if(!t.authors)return!0;let n=_(e,`w:author`);return n!==void 0&&t.authors.has(n)}function fi(e,t){let n={from:[],to:[]},r=[],i=new Map,a=new Map,o=e=>{for(let t of e){let e=f(t);if(!e)continue;let s=_(t,`w:id`);if(s!==void 0){if(e===`w:moveFromRangeStart`||e===`w:moveToRangeStart`){let o=_(t,`w:name`),c=o!==void 0&&a.get(o)||[];c.length||r.push(c),o!==void 0&&a.set(o,c),c.push(s),i.set(s,c),(e===`w:moveFromRangeStart`?n.from:n.to).push(s)}else if(e===`w:moveFromRangeEnd`||e===`w:moveToRangeEnd`){let t=e===`w:moveFromRangeEnd`?n.from:n.to,r=t.lastIndexOf(s);r!==-1&&t.splice(r,1)}else if(e===`w:moveFrom`||e===`w:moveTo`)for(let t of e===`w:moveFrom`?n.from:n.to)i.get(t).push(s)}let c=m(t);c.length>0&&o(c)}};o(e);for(let e of r)if(e.some(e=>t.has(e)))for(let n of e)t.add(n)}function pi(e,t){let n=new Set,r=e=>{for(let i of e){let e=f(i);if(!e)continue;if(ai.has(e)&&X(i,t)){let e=_(i,`w:id`);e!==void 0&&n.add(e)}let a=m(i);a.length>0&&r(a)}};return r(e),n}function Z(e,t,n){let r=0;for(;r<e.length;){let i=e[r],a=f(i);if(!a){r++;continue}let o=t!==void 0&&ii.has(t);if((a===`w:ins`||a===`w:moveTo`)&&!o){if(X(i,n)){n.count++,n.mode===`accept`?e.splice(r,1,...m(i)):e.splice(r,1);continue}Z(m(i),a,n),r++;continue}if((a===`w:del`||a===`w:moveFrom`)&&!o){if(X(i,n)){if(n.count++,n.mode===`accept`)e.splice(r,1);else{let t=m(i);for(let e of t)mi(e);e.splice(r,1,...t)}continue}Z(m(i),a,n),r++;continue}if(ai.has(a)||oi.has(a)){let t=_(i,`w:id`);if(ai.has(a)?X(i,n):t!==void 0&&n.moveRangeIds.has(t)){e.splice(r,1);continue}r++;continue}if(a in ri){if(X(i,n)){n.count++,n.mode===`accept`?e.splice(r,1):gi(e,i,a),r=0;continue}r++;continue}if(a===`w:p`){if(Z(m(i),a,n),_i(e,r,i,n))continue;r++;continue}if(a===`w:tr`){if(yi(e,r,i,n))continue;Z(m(i),a,n),r++;continue}if(a===`w:tc`){if(bi(e,r,i,n))continue;Z(m(i),a,n),r++;continue}let s=m(i);s.length>0&&Z(s,a,n),r++}}function mi(e){let t=f(e);if(t){if(t===`w:r`){let t=m(e);for(let e=0;e<t.length;e++){let n=t[e];if(`w:delText`in n){let r={"w:t":n[`w:delText`]};n[`:@`]&&(r[`:@`]=n[`:@`]),w(hi(r[`w:t`]))&&v(r,`xml:space`,`preserve`),t[e]=r}else if(`w:delInstrText`in n){let r={"w:instrText":n[`w:delInstrText`]};n[`:@`]&&(r[`:@`]=n[`:@`]),t[e]=r}}return}for(let t of m(e))mi(t)}}function hi(e){let t=``;for(let n of e)`#text`in n&&(t+=b(String(n[`#text`])));return t}function gi(e,t,n){let r=ri[n]??new Set,i=m(t).find(e=>f(e)!==void 0),a=i?m(i).map(x):[],o=e.filter(e=>{let n=f(e);return n!==void 0&&r.has(n)&&e!==t});e.length=0,n===`w:rPrChange`?e.push(...o,...a):e.push(...a,...o)}function _i(e,t,n,r){let i=h(n,`w:pPr`);if(!i)return!1;let a=h(i,`w:rPr`);if(!a)return!1;let o=m(a),s=!1,c=o.findIndex(e=>f(e)===`w:ins`);c!==-1&&X(o[c],r)&&(r.count++,o.splice(c,1),r.mode===`reject`&&(s=!0));let l=o.findIndex(e=>f(e)===`w:del`);if(l!==-1&&X(o[l],r)&&(r.count++,o.splice(l,1),r.mode===`accept`&&(s=!0)),o.length===0){let e=m(i);if(e.splice(e.indexOf(a),1),e.length===0){let e=m(n);e.splice(e.indexOf(i),1)}}return s?vi(e,t,n):!1}function vi(e,t,n){let r=h(n,`w:pPr`);if(r&&h(r,`w:sectPr`))return!1;let i=t+1;for(;i<e.length;){let t=f(e[i]);if(t===`w:p`)break;if(t===void 0||si.has(t)){i++;continue}return!1}if(i>=e.length)return!1;let a=e[i],o=m(a),s=h(a,`w:pPr`),c=s?o.indexOf(s)+1:0,l=m(n).filter(e=>f(e)!==`w:pPr`);return o.splice(c,0,...l),e.splice(t,1),!0}function yi(e,t,n,r){let i=h(n,`w:trPr`);if(!i)return!1;let a=m(i),o=a.findIndex(e=>f(e)===`w:ins`);if(o!==-1&&X(a[o],r)){if(r.count++,r.mode===`reject`)return e.splice(t,1),!0;a.splice(o,1)}let s=a.findIndex(e=>f(e)===`w:del`);if(s!==-1&&X(a[s],r)){if(r.count++,r.mode===`accept`)return e.splice(t,1),!0;a.splice(s,1)}return!1}function bi(e,t,n,r){let i=h(n,`w:tcPr`);if(!i)return!1;let a=m(i),o=a.findIndex(e=>f(e)===`w:cellIns`);if(o!==-1&&X(a[o],r)){if(r.count++,r.mode===`reject`)return e.splice(t,1),!0;a.splice(o,1)}let s=a.findIndex(e=>f(e)===`w:cellDel`);if(s!==-1&&X(a[s],r)){if(r.count++,r.mode===`accept`)return e.splice(t,1),!0;a.splice(s,1)}let c=a.find(e=>f(e)===`w:cellMerge`);return c&&X(c,r)&&(r.count++,r.mode===`reject`&&xi(a,c),a.splice(a.indexOf(c),1)),!1}function xi(e,t){let n=_(t,`w:vMergeOrig`),r=e.findIndex(e=>f(e)===`w:vMerge`);if(n===void 0){r!==-1&&e.splice(r,1);return}let i={"w:vMerge":[]};if(n===`rest`&&v(i,`w:val`,`restart`),r!==-1)e[r]=i;else{let t=e.findIndex(e=>{let t=f(e);return t===`w:tcBorders`||t===`w:shd`||t===`w:noWrap`||t===`w:tcMar`||t===`w:textDirection`||t===`w:tcFitText`||t===`w:vAlign`||t===`w:hideMark`||t===`w:cellIns`||t===`w:cellDel`||t===`w:cellMerge`||t===`w:tcPrChange`});t===-1?e.push(i):e.splice(t,0,i)}}const Si=`http://schemas.openxmlformats.org/package/2006/metadata/core-properties`,Ci=`http://purl.org/dc/elements/1.1/`,wi=`http://purl.org/dc/terms/`,Ti=`http://purl.org/dc/dcmitype/`,Ei=`http://www.w3.org/2001/XMLSchema-instance`,Q={creator:`dc:creator`,lastModifiedBy:`cp:lastModifiedBy`,title:`dc:title`,subject:`dc:subject`,description:`dc:description`,keywords:`cp:keywords`,category:`cp:category`,contentStatus:`cp:contentStatus`,revision:`cp:revision`,created:`dcterms:created`,modified:`dcterms:modified`},Di=[`creator`,`lastModifiedBy`,`description`,`keywords`,`category`,`contentStatus`],Oi=new Set([`Company`,`Manager`,`HyperlinkBase`]);function $(e){let t=e.indexOf(`:`);return t===-1?e:e.slice(t+1)}function ki(e,t){if(e)return e.find(e=>{let n=f(e);return n!==void 0&&$(n)===t})}function Ai(e,t){return m(e).find(e=>{let n=f(e);return n!==void 0&&$(n)===t})}function ji(e){let t=``;for(let n of m(e))`#text`in n&&(t+=b(String(n[`#text`])));return t}function Mi(e,t){let n=f(e);e[n]=t.length>0?[{"#text":y(t)}]:[]}function Ni(e,t){let n=m(e),r=n.findIndex(e=>{let n=f(e);return n!==void 0&&$(n)===t});return r===-1?!1:(n.splice(r,1),!0)}function Pi(){return[{"?xml":[{"#text":``}],":@":{"@_version":`1.0`,"@_encoding":`UTF-8`,"@_standalone":`yes`}},C(`cp:coreProperties`,[],{"xmlns:cp":Si,"xmlns:dc":Ci,"xmlns:dcterms":wi,"xmlns:dcmitype":Ti,"xmlns:xsi":Ei})]}const Fi={"xmlns:cp":Si,"xmlns:dc":Ci,"xmlns:dcterms":wi,"xmlns:dcmitype":Ti,"xmlns:xsi":Ei};function Ii(e){e.xmlParts.core||(e.xmlParts.core=Pi(),e.xmlParts.rootRels||(e.xmlParts.rootRels=fr()),_r(e.xmlParts.rootRels,`http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties`,`docProps/core.xml`),e.xmlParts.contentTypes||(e.xmlParts.contentTypes=pr()),yr(e.xmlParts.contentTypes,`/docProps/core.xml`,`application/vnd.openxmlformats-package.core-properties+xml`));let t=ki(e.xmlParts.core,`coreProperties`);if(!t)throw Error(`docProps/core.xml has no coreProperties root`);for(let[e,n]of Object.entries(Fi))_(t,e)===void 0&&v(t,e,n);return t}function Li(e){let t={},n=ki(e.xmlParts.core,`coreProperties`);if(!n)return t;for(let e of Object.keys(Q)){let r=Ai(n,$(Q[e]));r&&(t[e]=ji(r))}return t}function Ri(e,t){let n=Ii(e);for(let e of Object.keys(t)){let r=t[e];if(r===void 0||!(e in Q))continue;let i=Q[e],a=Ai(n,$(i));a||(a=C(i,[]),m(n).push(a)),Mi(a,r),(e===`created`||e===`modified`)&&v(a,`xsi:type`,`dcterms:W3CDTF`)}}function zi(e){let t=[],n=ki(e.xmlParts.core,`coreProperties`);if(n){for(let e of Di)Ni(n,$(Q[e]));Ri(e,{revision:`1`})}let r=ki(e.xmlParts.app,`Properties`);if(r){let e=m(r);for(let t=e.length-1;t>=0;t--){let n=f(e[t]);n!==void 0&&Oi.has($(n))&&e.splice(t,1)}}let i=vr(e.xmlParts.rootRels,`http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties`);br(e.xmlParts.contentTypes,`/docProps/custom.xml`),t.push(`docProps/custom.xml`);for(let e of i){let n=e.replace(/^\//,``);n&&!t.includes(n)&&t.push(n)}return{removedParts:t}}var Bi=class n{zipEntries;doc;removedParts=new Set;constructor(e,t){this.zipEntries=e,this.doc=t}static async load(t,r){let a;a=typeof t==`string`?await e(t):t;let o=await i(a);return new n(o,Je(o,r))}static async create(e,r){if(e!==void 0)return n.load(e,r);let i=new t;i.file(`[Content_Types].xml`,`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
19
19
  <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
20
20
  <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
21
21
  <Default Extension="xml" ContentType="application/xml"/>
@@ -33,5 +33,5 @@ import{readFile as e}from"node:fs/promises";import t from"jszip";import{XMLBuild
33
33
  </w:r>
34
34
  </w:p>
35
35
  </w:body>
36
- </w:document>`);let a=await i.generateAsync({type:`nodebuffer`,compression:`DEFLATE`});return n.load(a,r)}project(e=`json`,t={}){return ti(this.doc,e,t)}applyPatches(e,t){return Er(this.doc,e,t)}acceptAllRevisions(e){let t=si(this.doc,e);return A(this.doc),t}rejectAllRevisions(e){let t=ci(this.doc,e);return A(this.doc),t}getCoreProperties(){return Ii(this.doc)}setCoreProperties(e){Li(this.doc,e)}scrubMetadata(){let{removedParts:e}=Ri(this.doc);for(let t of e)this.removedParts.add(t)}toString(){return l(this.doc.xmlParts.document)}async toBuffer(){let e=l(this.doc.xmlParts.document),t=new Map;for(let[e,n]of this.doc.xmlParts.headers)t.set(e,l(n));let n=new Map;for(let[e,t]of this.doc.xmlParts.footers)n.set(e,l(t));let r=[];for(let e of Object.keys(this.zipEntries.zip.files))/^word\/header\d+\.xml$/i.test(e)?this.doc.xmlParts.headers.has(e)||r.push(e):/^word\/footer\d+\.xml$/i.test(e)&&(this.doc.xmlParts.footers.has(e)||r.push(e));for(let e of this.removedParts)r.includes(e)||r.push(e);let i=e=>e?l(e):void 0,a=this.doc.xmlParts;return s({zip:this.zipEntries.zip,documentXml:e,headers:t.size>0?t:void 0,footers:n.size>0?n:void 0,documentRelsXml:i(a.documentRels),contentTypesXml:i(a.contentTypes),settingsXml:i(a.settings),numberingXml:i(a.numbering),footnotesXml:i(a.footnotes),endnotesXml:i(a.endnotes),commentsXml:i(a.comments),coreXml:i(a.core),appXml:i(a.app),rootRelsXml:i(a.rootRels),removedParts:r.length>0?r:void 0})}async toFile(e){let{writeFile:t}=await import(`node:fs/promises`);await t(e,await this.toBuffer())}getAST(){return this.doc}getBody(){return this.doc.body}getParagraphs(){return this.doc.body.filter(e=>e.kind===`paragraph`)}getTables(){return this.doc.body.filter(e=>e.kind===`table`)}getHeaders(){return this.doc.headers}getFooters(){return this.doc.footers}findParagraphById(e){return M(e,this.doc)}findParagraphByParaId(e){return lt(e,this.doc)}findTableById(e){return N(e,this.doc)}getStyles(){return this.doc.styles}validatePatches(e){return e.map(e=>st(e,this.doc))}};export{zi as Docxtor,si as acceptAllRevisions,Er as applyPatches,kr as cloneDocument,M as findParagraphById,lt as findParagraphByParaId,N as findTableById,Ii as getCoreProperties,qe as normalizeDocument,d as parseXml,ti as project,Br as projectToJson,Jr as projectToMarkdown,A as reNormalizeDocument,i as readDocx,ci as rejectAllRevisions,Ri as scrubMetadata,l as serializeXml,Li as setCoreProperties,st as validatePatch,s as writeDocx};
36
+ </w:document>`);let a=await i.generateAsync({type:`nodebuffer`,compression:`DEFLATE`});return n.load(a,r)}project(e=`json`,t={}){return ni(this.doc,e,t)}applyPatches(e,t){return Dr(this.doc,e,t)}acceptAllRevisions(e){let t=ci(this.doc,e);return k(this.doc),t}rejectAllRevisions(e){let t=li(this.doc,e);return k(this.doc),t}getCoreProperties(){return Li(this.doc)}setCoreProperties(e){Ri(this.doc,e)}scrubMetadata(){let{removedParts:e}=zi(this.doc);for(let t of e)this.removedParts.add(t)}toString(){return l(this.doc.xmlParts.document)}async toBuffer(){let e=l(this.doc.xmlParts.document),t=new Map;for(let[e,n]of this.doc.xmlParts.headers)t.set(e,l(n));let n=new Map;for(let[e,t]of this.doc.xmlParts.footers)n.set(e,l(t));let r=[];for(let e of Object.keys(this.zipEntries.zip.files))/^word\/header\d+\.xml$/i.test(e)?this.doc.xmlParts.headers.has(e)||r.push(e):/^word\/footer\d+\.xml$/i.test(e)&&(this.doc.xmlParts.footers.has(e)||r.push(e));for(let e of this.removedParts)r.includes(e)||r.push(e);let i=e=>e?l(e):void 0,a=this.doc.xmlParts;return s({zip:this.zipEntries.zip,documentXml:e,headers:t.size>0?t:void 0,footers:n.size>0?n:void 0,documentRelsXml:i(a.documentRels),contentTypesXml:i(a.contentTypes),settingsXml:i(a.settings),numberingXml:i(a.numbering),footnotesXml:i(a.footnotes),endnotesXml:i(a.endnotes),commentsXml:i(a.comments),coreXml:i(a.core),appXml:i(a.app),rootRelsXml:i(a.rootRels),removedParts:r.length>0?r:void 0})}async toFile(e){let{writeFile:t}=await import(`node:fs/promises`);await t(e,await this.toBuffer())}getAST(){return this.doc}getBody(){return this.doc.body}getParagraphs(){return this.doc.body.filter(e=>e.kind===`paragraph`)}getTables(){return this.doc.body.filter(e=>e.kind===`table`)}getHeaders(){return this.doc.headers}getFooters(){return this.doc.footers}findParagraphById(e){return j(e,this.doc)}findParagraphByParaId(e){return ut(e,this.doc)}findTableById(e){return M(e,this.doc)}getStyles(){return this.doc.styles}validatePatches(e){return e.map(e=>ct(e,this.doc))}};export{Bi as Docxtor,ci as acceptAllRevisions,Dr as applyPatches,Ar as cloneDocument,j as findParagraphById,ut as findParagraphByParaId,M as findTableById,Li as getCoreProperties,Je as normalizeDocument,d as parseXml,ni as project,Vr as projectToJson,Yr as projectToMarkdown,k as reNormalizeDocument,i as readDocx,li as rejectAllRevisions,zi as scrubMetadata,l as serializeXml,Ri as setCoreProperties,ct as validatePatch,s as writeDocx};
37
37
  //# sourceMappingURL=index.mjs.map