@lucio-ai/docxtor 0.4.0 → 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.cjs CHANGED
@@ -1,19 +1,21 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`node:fs/promises`),l=require(`jszip`);l=s(l);let u=require(`fast-xml-parser`);async function d(e){let t=await l.default.loadAsync(e),n=await f(t,`word/document.xml`);if(n===void 0)throw Error(`Invalid DOCX: missing word/document.xml`);let r=await f(t,`word/_rels/document.xml.rels`),i=await f(t,`[Content_Types].xml`),a=await f(t,`word/styles.xml`),o=await f(t,`word/settings.xml`),s=await f(t,`word/numbering.xml`),c=await f(t,`word/footnotes.xml`),u=await f(t,`word/endnotes.xml`),d=await f(t,`word/comments.xml`),p=await f(t,`docProps/core.xml`),m=await f(t,`docProps/app.xml`),h=await f(t,`_rels/.rels`),g=new Map,ee=new Map;for(let e of Object.keys(t.files))if(/^word\/header\d+\.xml$/i.test(e)){let n=await f(t,e);n!==void 0&&g.set(e,n)}else if(/^word\/footer\d+\.xml$/i.test(e)){let n=await f(t,e);n!==void 0&&ee.set(e,n)}return{documentXml:n,headers:g,footers:ee,documentRels:r,contentTypes:i,stylesXml:a,settingsXml:o,numberingXml:s,footnotesXml:c,endnotesXml:u,commentsXml:d,coreXml:p,appXml:m,rootRels:h,zip:t}}async function f(e,t){let n=e.file(t);if(n)return n.async(`string`)}const p=[[`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 m(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 p){let i=e[n];typeof i==`string`&&t.file(r,i)}return await t.generateAsync({type:`nodebuffer`,compression:`DEFLATE`,compressionOptions:{level:6}})}const h=new u.XMLBuilder({preserveOrder:!0,ignoreAttributes:!1,attributeNamePrefix:`@_`,commentPropName:`#comment`,cdataPropName:`#cdata`,processEntities:!1,format:!1,suppressEmptyNode:!1,suppressBooleanAttributes:!1});function g(e){return h.build(e)}const ee=new u.XMLParser({preserveOrder:!0,ignoreAttributes:!1,attributeNamePrefix:`@_`,commentPropName:`#comment`,cdataPropName:`#cdata`,processEntities:!1,trimValues:!1,parseTagValue:!1,parseAttributeValue:!1,ignoreDeclaration:!1,ignorePiTags:!1});function _(e){return ee.parse(e)}function v(e){for(let t of Object.keys(e))if(t!==`:@`&&t!==`#text`)return t}function y(e,t){return t in e}function b(e){let t=v(e);if(!t)return[];let n=e[t];return Array.isArray(n)?n:[]}function x(e,t){return b(e).find(e=>t in e)}function S(e,t){return b(e).filter(e=>t in e)}function C(e,t){let n=e[`:@`];if(n)return n[`@_${t}`]}function w(e,t,n){e[`:@`]||={},e[`:@`][`@_${t}`]=n}function te(e,t){let n=e[`:@`];n&&delete n[`@_${t}`]}function ne(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 T(e){return e.replace(/&/g,`&amp;`).replace(/</g,`&lt;`).replace(/>/g,`&gt;`)}function E(e){return e.replace(/&amp;/g,`&`).replace(/&lt;/g,`<`).replace(/&gt;/g,`>`).replace(/&quot;/g,`"`).replace(/&apos;/g,`'`)}function re(e,t){e[`#text`]=T(t)}function ie(e){let t=b(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+=E(String(e[`#text`])))}else if(`w:br`in e){let t=C(e,`w:type`);(!t||t===`textWrapping`)&&(n+=`
2
- `)}return n}function ae(e){return b(e).find(e=>`w:t`in e)}function oe(e){let t=b(e);for(let e of t)if(`w:br`in e&&C(e,`w:type`)===`page`||`w:lastRenderedPageBreak`in e)return!0;return!1}function se(e){let t=b(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+=E(String(e[`#text`])).length)}else if(`w:br`in e){let t=C(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 ce(e){let t=x(e,`w:pPr`);if(t){let e=x(t,`w:pageBreakBefore`);if(e){let t=C(e,`w:val`);if(t!==`0`&&t!==`false`)return!0}}return le(b(e))}function le(e){for(let t of e){let e=v(t);if(e===`w:r`){if(oe(t))return!0}else if((e===`w:hyperlink`||e===`w:ins`)&&le(b(t)))return!0}return!1}function D(e){return JSON.parse(JSON.stringify(e))}function ue(e,t,n){let r=b(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 O(e,t){let n=b(e),r=n.indexOf(t);return r===-1?!1:(n.splice(r,1),!0)}function k(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 de(e){return{"#text":T(e)}}function fe(e){let t={"w:t":[de(e)]};return A(e)&&w(t,`xml:space`,`preserve`),t}function A(e){return e.length>0&&(e!==e.trim()||e.includes(` `))}function pe(e,t){let n=[];if(t&&n.push(D(t)),e.includes(`
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`node:fs/promises`),l=require(`jszip`);l=s(l,1);let u=require(`fast-xml-parser`);async function d(e){let t=await l.default.loadAsync(e),n=await f(t,`word/document.xml`);if(n===void 0)throw Error(`Invalid DOCX: missing word/document.xml`);let r=await f(t,`word/_rels/document.xml.rels`),i=await f(t,`[Content_Types].xml`),a=await f(t,`word/styles.xml`),o=await f(t,`word/settings.xml`),s=await f(t,`word/numbering.xml`),c=await f(t,`word/footnotes.xml`),u=await f(t,`word/endnotes.xml`),d=await f(t,`word/comments.xml`),p=await f(t,`docProps/core.xml`),m=await f(t,`docProps/app.xml`),ee=await f(t,`_rels/.rels`),h=new Map,te=new Map;for(let e of Object.keys(t.files))if(/^word\/header\d+\.xml$/i.test(e)){let n=await f(t,e);n!==void 0&&h.set(e,n)}else if(/^word\/footer\d+\.xml$/i.test(e)){let n=await f(t,e);n!==void 0&&te.set(e,n)}return{documentXml:n,headers:h,footers:te,documentRels:r,contentTypes:i,stylesXml:a,settingsXml:o,numberingXml:s,footnotesXml:c,endnotesXml:u,commentsXml:d,coreXml:p,appXml:m,rootRels:ee,zip:t}}async function f(e,t){let n=e.file(t);if(n)return n.async(`string`)}const p=[[`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 m(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 p){let i=e[n];typeof i==`string`&&t.file(r,i)}return await t.generateAsync({type:`nodebuffer`,compression:`DEFLATE`,compressionOptions:{level:6}})}const ee=new u.XMLBuilder({preserveOrder:!0,ignoreAttributes:!1,attributeNamePrefix:`@_`,commentPropName:`#comment`,cdataPropName:`#cdata`,processEntities:!1,format:!1,suppressEmptyNode:!1,suppressBooleanAttributes:!1});function h(e){return ee.build(e)}const te=new u.XMLParser({preserveOrder:!0,ignoreAttributes:!1,attributeNamePrefix:`@_`,commentPropName:`#comment`,cdataPropName:`#cdata`,processEntities:!1,trimValues:!1,parseTagValue:!1,parseAttributeValue:!1,ignoreDeclaration:!1,ignorePiTags:!1});function g(e){return te.parse(e)}function _(e){for(let t of Object.keys(e))if(t!==`:@`&&t!==`#text`)return t}function v(e,t){return t in e}function y(e){let t=_(e);if(!t)return[];let n=e[t];return Array.isArray(n)?n:[]}function b(e,t){return y(e).find(e=>t in e)}function x(e,t){return y(e).filter(e=>t in e)}function S(e,t){let n=e[`:@`];if(n)return n[`@_${t}`]}function C(e,t,n){e[`:@`]||={},e[`:@`][`@_${t}`]=n}function ne(e,t){let n=e[`:@`];n&&delete n[`@_${t}`]}function re(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 w(e){return e.replace(/&/g,`&amp;`).replace(/</g,`&lt;`).replace(/>/g,`&gt;`)}function T(e){return e.replace(/&amp;/g,`&`).replace(/&lt;/g,`<`).replace(/&gt;/g,`>`).replace(/&quot;/g,`"`).replace(/&apos;/g,`'`)}function ie(e,t){e[`#text`]=w(t)}function ae(e){let t=y(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+=T(String(e[`#text`])))}else if(`w:br`in e){let t=S(e,`w:type`);(!t||t===`textWrapping`)&&(n+=`
2
+ `)}return n}function oe(e){return y(e).find(e=>`w:t`in e)}function se(e){let t=y(e);for(let e of t)if(`w:br`in e&&S(e,`w:type`)===`page`||`w:lastRenderedPageBreak`in e)return!0;return!1}function ce(e){let t=y(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+=T(String(e[`#text`])).length)}else if(`w:br`in e){let t=S(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 le(e){let t=b(e,`w:pPr`);if(t){let e=b(t,`w:pageBreakBefore`);if(e){let t=S(e,`w:val`);if(t!==`0`&&t!==`false`)return!0}}return ue(y(e))}function ue(e){for(let t of e){let e=_(t);if(e===`w:r`){if(se(t))return!0}else if((e===`w:hyperlink`||e===`w:ins`)&&ue(y(t)))return!0}return!1}function E(e){return JSON.parse(JSON.stringify(e))}function de(e,t,n){let r=y(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 D(e,t){let n=y(e),r=n.indexOf(t);return r===-1?!1:(n.splice(r,1),!0)}function O(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 fe(e){return{"#text":w(e)}}function pe(e){let t={"w:t":[fe(e)]};return k(e)&&C(t,`xml:space`,`preserve`),t}function k(e){return e.length>0&&(e!==e.trim()||e.includes(` `))}function me(e,t){let n=[];if(t&&n.push(E(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(fe(r)),e<t.length-1&&n.push({"w:br":[]})}}else n.push(fe(e));return{"w:r":n}}function me(e){let t=b(e),n=[];for(let e of t){let t=v(e);t===`w:r`?n.push(e):(t===`w:hyperlink`||t===`w:ins`)&&he(e,n)}return n}function he(e,t){let n=b(e);for(let e of n){let n=v(e);n===`w:r`?t.push(e):(n===`w:hyperlink`||n===`w:ins`)&&he(e,t)}}function j(e,t){let n=me(e),r=[],i=0,a;for(let e of n){let t=ie(e);if(a===void 0&&oe(e)){let t=se(e);t>=0&&(a=i+t)}if(t.length===0)continue;let n=ae(e),o=ge(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=ye(e,r),c={kind:`paragraph`,id:t,text:o,runs:r,xmlRef:e,style:_e(e),listInfo:ve(e)},l=C(e,`w14:paraId`);if(l&&(c.paraId=l.toUpperCase()),s.length>0&&(c.revisions=s),ce(e)){c.hasPageBreak=!0;let t=x(e,`w:pPr`);if(t){let e=x(t,`w:pageBreakBefore`);if(e){let t=C(e,`w:val`);if(t!==`0`&&t!==`false`)return c.pageBreakOffset=0,c}}a!==void 0&&(c.pageBreakOffset=a)}return c}function ge(e){let t=x(e,`w:rPr`);if(!t)return{};let n={},r=b(t);for(let e of r)if(y(e,`w:b`)){let t=C(e,`w:val`);n.bold=t!==`0`&&t!==`false`}else if(y(e,`w:i`)){let t=C(e,`w:val`);n.italic=t!==`0`&&t!==`false`}else if(y(e,`w:u`))n.underline=C(e,`w:val`)??`single`;else if(y(e,`w:strike`)){let t=C(e,`w:val`);n.strikethrough=t!==`0`&&t!==`false`}else if(y(e,`w:sz`)){let t=C(e,`w:val`);t&&(n.fontSize=parseInt(t,10))}else if(y(e,`w:rFonts`))n.fontFamily=C(e,`w:ascii`)??C(e,`w:hAnsi`);else if(y(e,`w:color`)){n.color=C(e,`w:val`);let t=C(e,`w:themeColor`);t&&(n.themeColor=t)}else y(e,`w:highlight`)&&(n.highlight=C(e,`w:val`));return n}function _e(e){let t=x(e,`w:pPr`);if(!t)return;let n=x(t,`w:pStyle`);if(n)return C(n,`w:val`)}function ve(e){let t=x(e,`w:pPr`);if(!t)return;let n=x(t,`w:numPr`);if(!n)return;let r=x(n,`w:numId`),i=x(n,`w:ilvl`),a=r?C(r,`w:val`):void 0,o=i?C(i,`w:val`):void 0;if(a&&a!==`0`)return{numId:a,level:o?parseInt(o,10):0}}function ye(e,t){let n=[],r=new Set;be(e,r);for(let i of t)if(r.has(i.xmlRef)){let t=Se(e,i.xmlRef);n.push({type:`insert`,text:i.text,startOffset:i.startOffset,endOffset:i.endOffset,author:t?.author??``,date:t?.date??``})}return Te(e,t,n),n}function be(e,t){let n=b(e);for(let e of n){let n=v(e);n===`w:ins`?xe(e,t):n===`w:hyperlink`&&be(e,t)}}function xe(e,t){let n=b(e);for(let e of n){let n=v(e);n===`w:r`?t.add(e):(n===`w:hyperlink`||n===`w:ins`)&&xe(e,t)}}function Se(e,t){return Ce(b(e),t)}function Ce(e,t){for(let n of e){let e=v(n);if(e===`w:ins`){if(we(n,t))return{author:C(n,`w:author`)??``,date:C(n,`w:date`)??``}}else if(e===`w:hyperlink`){let e=Ce(b(n),t);if(e)return e}}}function we(e,t){let n=b(e);for(let e of n){if(e===t)return!0;let n=v(e);if(n&&n!==`w:del`&&we(e,t))return!0}return!1}function Te(e,t,n){let r=new Map;for(let e of t)r.set(e.xmlRef,e);Ee(b(e),r,n,0)}function Ee(e,t,n,r){for(let i of e){let e=v(i);if(e===`w:r`){let e=t.get(i);e&&(r=e.endOffset)}else if(e===`w:del`){let e=De(i);e.length>0&&n.push({type:`delete`,text:e,startOffset:r,endOffset:r,author:C(i,`w:author`)??``,date:C(i,`w:date`)??``})}else (e===`w:hyperlink`||e===`w:ins`)&&(r=Ee(b(i),t,n,r))}return r}function De(e){let t=b(e),n=``;for(let e of t)if(v(e)===`w:r`){let t=b(e);for(let e of t)if(`w:br`in e){let t=C(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+=E(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+=E(String(e[`#text`])))}}return n}function M(e,t){let n=[],r=S(e,`w:tr`);for(let e=0;e<r.length;e++){let i=r[e],a=[],o=S(i,`w:tc`);for(let n=0;n<o.length;n++){let r=o[n],i=S(r,`w:p`).map((r,i)=>j(r,`${t}.r${e}.c${n}.p${i}`)),s=S(r,`w:tbl`).map((r,i)=>M(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=x(e,`w:tblPr`),a=i?x(i,`w:tblStyle`):void 0;return{kind:`table`,id:t,rows:n,xmlRef:e,style:a?C(a,`w:val`):void 0}}function Oe(e,t,n,r){let i=t===`header`?`hdr`:`ftr`,a=t===`header`?`w:hdr`:`w:ftr`,o=e.find(e=>v(e)===a),s=[],c=[];if(o){let e=b(o),t=0,r=0;for(let a of e){let e=v(a);e===`w:p`?(s.push(j(a,`${i}.${n}.p${t}`)),t++):e===`w:tbl`&&(c.push(M(a,`${i}.${n}.tbl${r}`)),r++)}}return{type:t,variant:n,rId:r,paragraphs:s,tables:c,xmlRef:e}}function ke(e){if(e.xmlParts.numbering)return e.xmlParts.numbering;let t=k(`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=>v(e)===`Types`);if(t){let e=b(t);e.some(e=>v(e)===`Override`&&C(e,`PartName`)===`/word/numbering.xml`)||e.push(k(`Override`,[],{PartName:`/word/numbering.xml`,ContentType:`application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml`}))}}if(e.xmlParts.documentRels){let t=e.xmlParts.documentRels.find(e=>v(e)===`Relationships`);if(t){let e=b(t);if(!e.some(e=>C(e,`Type`)===`http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering`)){let t=0;for(let n of e){let e=C(n,`Id`);if(e){let n=parseInt(e.replace(`rId`,``),10);!isNaN(n)&&n>t&&(t=n)}}e.push(k(`Relationship`,[],{Id:`rId${t+1}`,Type:`http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering`,Target:`numbering.xml`}))}}}return e.xmlParts.numbering}function Ae(e){let t=e.find(e=>v(e)===`w:numbering`);if(!t)return 1;let n=0;for(let e of S(t,`w:num`)){let t=C(e,`w:numId`);if(t){let e=parseInt(t,10);!isNaN(e)&&e>n&&(n=e)}}return n+1}function je(e){let t=e.find(e=>v(e)===`w:numbering`);if(!t)return 0;let n=-1;for(let e of S(t,`w:abstractNum`)){let t=C(e,`w:abstractNumId`);if(t){let e=parseInt(t,10);!isNaN(e)&&e>n&&(n=e)}}return n+1}const Me=[{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`}],Ne=[{fmt:`decimal`,textFn:e=>`%${e+1}.`},{fmt:`lowerLetter`,textFn:e=>`%${e+1}.`},{fmt:`lowerRoman`,textFn:e=>`%${e+1}.`}];function N(e,t,n=9){let r=ke(e),i=b(r.find(e=>v(e)===`w:numbering`)),a=je(r),o=Ae(r),s=[];for(let e=0;e<n;e++){let n,r;if(t===`bullet`){n=`bullet`;let t=Me[e%Me.length];r=t.char;let i=k(`w:rPr`,[k(`w:rFonts`,[],{"w:ascii":t.font,"w:hAnsi":t.font,"w:hint":`default`})]);s.push(k(`w:lvl`,[k(`w:start`,[],{"w:val":`1`}),k(`w:numFmt`,[],{"w:val":n}),k(`w:lvlText`,[],{"w:val":r}),k(`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=Ne[e%Ne.length];n=t.fmt,r=t.textFn(e)}s.push(k(`w:lvl`,[k(`w:start`,[],{"w:val":`1`}),k(`w:numFmt`,[],{"w:val":n}),k(`w:lvlText`,[],{"w:val":r}),k(`w:lvlJc`,[],{"w:val":`left`})],{"w:ilvl":String(e)}))}let c=k(`w:abstractNum`,s,{"w:abstractNumId":String(a)}),l=k(`w:num`,[k(`w:abstractNumId`,[],{"w:val":String(a)})],{"w:numId":String(o)}),u=i.findIndex(e=>v(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=>v(e)===`w:numbering`);if(!n)return;let r=S(n,`w:num`).find(e=>C(e,`w:numId`)===t);if(!r)return;let i=x(r,`w:abstractNumId`);if(!i)return;let a=C(i,`w:val`);if(!a)return;let o=S(n,`w:abstractNum`).find(e=>C(e,`w:abstractNumId`)===a);if(!o)return;let s=S(o,`w:lvl`),c=!1,l=!1;for(let e of s){let t=x(e,`w:numFmt`);t&&C(t,`w:val`)===`bullet`&&(c=!0);let n=x(e,`w:lvlText`);if(n){let e=(C(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=>v(e)===`w:numbering`);if(!r)return null;let i=S(r,`w:num`).find(e=>C(e,`w:numId`)===t);if(!i)return null;let a=x(i,`w:abstractNumId`);if(!a)return null;let o=C(a,`w:val`);if(!o)return null;let s=S(r,`w:abstractNum`).find(e=>C(e,`w:abstractNumId`)===o);if(!s)return null;let c=S(s,`w:lvl`),l=c.find(e=>C(e,`w:ilvl`)===String(n));if(!l)return null;let u=x(l,`w:numFmt`),d=x(l,`w:lvlText`),f=u?C(u,`w:val`)??`decimal`:`decimal`,p=d?C(d,`w:val`)??``:``,m=new Map,h=p.match(/%(\d+)/g);if(h)for(let e of h){let t=parseInt(e.slice(1),10)-1,n=c.find(e=>C(e,`w:ilvl`)===String(t));if(n){let e=x(n,`w:numFmt`);m.set(t,e?C(e,`w:val`)??`decimal`:`decimal`)}}return{pattern:p,format:f,ancestorFormats:m}}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=v(n);if(!e)continue;if(e===`w:p`){let e=C(n,`w14:paraId`);e&&t.add(e.toUpperCase())}let r=b(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?C(e,`xmlns:w14`)!==void 0:!1}function We(e){C(e,`xmlns:w14`)===void 0&&w(e,`xmlns:w14`,`http://schemas.microsoft.com/office/word/2010/wordml`),C(e,`xmlns:mc`)===void 0&&w(e,`xmlns:mc`,`http://schemas.openxmlformats.org/markup-compatibility/2006`);let t=C(e,`mc:Ignorable`);if(t===void 0)w(e,`mc:Ignorable`,`w14`);else{let n=t.split(/\s+/).filter(e=>e.length>0);n.includes(`w14`)||(n.push(`w14`),w(e,`mc:Ignorable`,n.join(` `)))}}function Ge(e){return e.find(e=>{let t=v(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=v(n);if(!e)continue;if(e===`w:p`){let e=C(n,`w14:paraId`);if(!e||!He(e)){let e=Ve(t);w(n,`w14:paraId`,e),r++,C(n,`w14:textId`)===void 0&&w(n,`w14:textId`,e)}else C(n,`w14:textId`)===void 0&&w(n,`w14:textId`,e.toUpperCase())}let a=b(n);a.length>0&&i(a)}};return i(b(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 P(e,t,n,r=!1){if(!Ue(n))return;let i=Re(e),a=e=>{for(let t of e){let e=v(t);if(!e)continue;if(e===`w:p`&&(r||C(t,`w14:paraId`)===void 0)){let e=Ve(i);w(t,`w14:paraId`,e),w(t,`w14:textId`,e)}let n=b(t);n.length>0&&a(n)}};a(t)}function F(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=>v(e)===r);return}return e.xmlParts.document.find(e=>v(e)===`w:document`)}function Je(e,t){let n=_(e.documentXml),r=new Map;for(let[t,n]of e.headers)r.set(t,_(n));let i=new Map;for(let[t,n]of e.footers)i.set(t,_(n));let a=e.stylesXml?$e(e.stylesXml):[],o=e=>e?_(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 Ye(c),c}function Ye(e){let t=e.loadOptions?.includeHeadersFooters??!0;if(e.body=Xe(e.xmlParts.document),e.headers=new Map,e.footers=new Map,t){let t=Ze(e.xmlParts.documentRels),n=Qe(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,Oe(t,`header`,r.variant,r.rId))}else{let t=e.xmlParts.footers.get(i);t&&e.footers.set(r.rId,Oe(t,`footer`,r.variant,r.rId))}}}et(e)}function Xe(e){let t=[],n=e.find(e=>v(e)===`w:document`);if(!n)return t;let r=x(n,`w:body`);if(!r)return t;let i=b(r),a=0,o=0,s=0;for(let e of i){let n=v(e);if(n===`w:p`){let n=j(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(M(e,`tbl${o}`)),o++)}return t}function Ze(e){let t=new Map;if(!e)return t;let n=e.find(e=>v(e)===`Relationships`);if(!n)return t;let r=S(n,`Relationship`);for(let e of r){let n=C(e,`Id`),r=C(e,`Target`);n&&r&&t.set(n,r)}return t}function Qe(e){let t=[],n=e.find(e=>v(e)===`w:document`);if(!n)return t;let r=x(n,`w:body`);if(!r)return t;let i=S(r,`w:sectPr`),a=b(r);for(let e of a)if(v(e)===`w:p`){let t=x(e,`w:pPr`);if(t){let e=x(t,`w:sectPr`);e&&i.push(e)}}for(let e of i){let n=b(e);for(let e of n){let n=v(e);if(n===`w:headerReference`){let n=C(e,`r:id`),r=C(e,`w:type`)??`default`;n&&t.push({type:`header`,variant:r,rId:n})}else if(n===`w:footerReference`){let n=C(e,`r:id`),r=C(e,`w:type`)??`default`;n&&t.push({type:`footer`,variant:r,rId:n})}}}return t}function $e(e){let t=_(e),n=[],r=t.find(e=>v(e)===`w:styles`);if(!r)return n;let i=S(r,`w:style`);for(let e of i){let t=C(e,`w:styleId`),r=C(e,`w:type`);if(!t||!r||![`paragraph`,`character`,`table`,`numbering`].includes(r))continue;let i=x(e,`w:name`),a=i?C(i,`w:val`)??t:t;n.push({id:t,name:a,type:r})}return n}function et(e){let t=e.xmlParts.numbering;if(t){for(let n of e.body)if(n.kind===`paragraph`)I(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)I(e,t);for(let n of e.headers.values())for(let e of n.paragraphs)I(e,t);for(let n of e.footers.values())for(let e of n.paragraphs)I(e,t)}}function I(e,t){if(!e.listInfo)return;let n=Pe(t,e.listInfo.numId);n&&(e.listInfo.listType=n)}function tt(e){let t=e.split(`
6
- `),n=[],r=0;for(;r<t.length;){if(r+1<t.length&&ot(t[r])&&st(t[r+1])){let e=ct(t[r]);r+=2;let i=[];for(;r<t.length&&ot(t[r]);){let n=ct(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=at(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=at(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 nt(e){return tt(e).some(e=>e.kind!==`text`)}const rt=/^(\s*)[*+-]\s(.*)$/,it=/^(\s*)\d+\.\s(.*)$/;function at(e){let t=e.replace(/\t/g,` `),n=rt.exec(t);if(n){let e=n[1].length;return{listType:`bullet`,text:n[2],level:Math.floor(e/2)}}if(n=it.exec(t),n){let e=n[1].length;return{listType:`ordered`,text:n[2],level:Math.floor(e/2)}}}function ot(e){let t=e.trim();return t.startsWith(`|`)&&t.endsWith(`|`)&&t.length>1}function st(e){let t=e.trim();return!t.startsWith(`|`)||!t.endsWith(`|`)?!1:t.slice(1,-1).split(`|`).every(e=>/^\s*:?-+:?\s*$/.test(e))}function ct(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 lt(e,t){switch(e.type){case`insert_paragraph`:return L(e.target,t)?!e.text||e.text.length===0?{valid:!1,error:`Empty paragraph text`}:[`before`,`after`].includes(e.position)?nt(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 L(e.target,t)?{valid:!0}:{valid:!1,error:`Paragraph "${e.target}" not found`};case`add_comment`:return L(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 L(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=R(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=R(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 L(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(!L(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=ut(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(!gt(e.afterId,t))return{valid:!1,error:`Element "${e.afterId}" not found`}}if(e.container){let n=ut(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&&!gt(e.afterId,t))return{valid:!1,error:`Element "${e.afterId}" not found`};if(e.container){let n=ut(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=>v(e)===`w:numbering`);if(!(n&&b(n).some(t=>v(t)===`w:num`&&C(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=L(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=L(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(!L(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=L(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=L(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 ut(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 L(e,t){return e.startsWith(`para:`)?dt(e.slice(5),t):ft(t,t=>t.id===e)}function dt(e,t){let n=e.toUpperCase();return ft(t,e=>e.paraId===n)}function ft(e,t){for(let n of e.body){let e=mt(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=ht(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=ht(t,e);if(n)return n}}}function R(e,t){for(let n of t.body)if(n.kind===`table`){let t=pt(e,n);if(t)return t}for(let n of t.headers.values())for(let t of n.tables){let n=pt(e,t);if(n)return n}for(let n of t.footers.values())for(let t of n.tables){let n=pt(e,t);if(n)return n}}function pt(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=pt(e,n);if(t)return t}}function mt(e,t){if(t.kind===`paragraph`)return e(t)?t:void 0;if(t.kind===`table`)return ht(e,t)}function ht(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=ht(e,n);if(t)return t}}}function gt(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 z(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 _t(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 B(e,t){let n=b(e);return n.includes(t)?n:vt(n,t)}function vt(e,t){for(let n of e){let e=v(n);if(e===`w:hyperlink`||e===`w:ins`||e===`w:del`){let e=b(n);if(e.includes(t))return e;let r=vt(e,t);if(r)return r}}}function V(e,t){if(t.isFullRun)return{matched:t.run.xmlRef};let n=t.run.xmlRef,r=t.run.text,i=B(e,n);if(!i)throw Error(`Run not found in paragraph children or hyperlink children`);let a=i.indexOf(n),o=x(n,`w:rPr`),s=r.slice(0,t.localStart),c=r.slice(t.localStart,t.localEnd),l=r.slice(t.localEnd),u={matched:void 0},d=[];if(s.length>0){let e=yt(s,o);u.before=e,d.push(e)}let f=yt(c,o);if(u.matched=f,d.push(f),l.length>0){let e=yt(l,o);u.after=e,d.push(e)}return i.splice(a,1,...d),u}function H(e,t){if(t.length===0)return[];if(t.length===1)return[V(e,t[0]).matched];let n=[],r=[],i=t[t.length-1];if(!i.isEndAligned){let n=V(e,i);r.push({index:t.length-1,split:n})}let a=t[0];if(!a.isStartAligned){let t=V(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=B(e,a.run.xmlRef);t&&t.includes(a.run.xmlRef)&&n.push(a.run.xmlRef)}}return n}function yt(e,t){return pe(e,t)}function bt(e,t){return{nextId:St(e)+1,author:t.revisionAuthor??`docxtor`,date:t.revisionDate??new Date().toISOString()}}function U(e){let t=String(e.nextId);return e.nextId++,t}const xt=new Set([`w:ins`,`w:del`,`w:rPrChange`,`w:pPrChange`,`w:trPrChange`,`w:tblPrChange`,`w:sectPrChange`,`w:cellIns`,`w:cellDel`,`w:cellMerge`]);function St(e){let t=0;t=Math.max(t,Ct(e.xmlParts.document));for(let n of e.xmlParts.headers.values())t=Math.max(t,Ct(n));for(let n of e.xmlParts.footers.values())t=Math.max(t,Ct(n));for(let n of[`footnotes`,`endnotes`,`comments`]){let r=e.xmlParts[n];r&&(t=Math.max(t,Ct(r)))}return t}function Ct(e){let t=0;for(let n of e){let e=v(n);if(e&&xt.has(e)){let e=C(n,`w:id`);if(e){let n=parseInt(e,10);!isNaN(n)&&n>t&&(t=n)}}if(e){let e=b(n);e.length>0&&(t=Math.max(t,Ct(e)))}}return t}function W(e,t){let n=U(e);return k(`w:ins`,t,{"w:id":n,"w:author":e.author,"w:date":e.date})}function wt(e,t){let n=U(e);return k(`w:del`,t,{"w:id":n,"w:author":e.author,"w:date":e.date})}function Tt(e,t){let n=U(e);return k(`w:rPrChange`,[D(t)],{"w:id":n,"w:author":e.author,"w:date":e.date})}function G(e,t){let n=U(e);return k(`w:pPrChange`,[D(t)],{"w:id":n,"w:author":e.author,"w:date":e.date})}function Et(e){let t=U(e);return k(`w:ins`,[],{"w:id":t,"w:author":e.author,"w:date":e.date})}function Dt(e){let t=U(e);return k(`w:del`,[],{"w:id":t,"w:author":e.author,"w:date":e.date})}function Ot(e){let t=b(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 kt(e,t){let n=[];if(t&&n.push(D(t)),e.includes(`
4
+ `);for(let e=0;e<t.length;e++){let r=t[e];(r.length>0||e===0)&&n.push(pe(r)),e<t.length-1&&n.push({"w:br":[]})}}else n.push(pe(e));return{"w:r":n}}function he(e){let t=y(e),n=[];for(let e of t){let t=_(e);t===`w:r`?n.push(e):(t===`w:hyperlink`||t===`w:ins`)&&ge(e,n)}return n}function ge(e,t){let n=y(e);for(let e of n){let n=_(e);n===`w:r`?t.push(e):(n===`w:hyperlink`||n===`w:ins`)&&ge(e,t)}}function A(e,t){let n=he(e),r=[],i=0,a;for(let e of n){let t=ae(e);if(a===void 0&&se(e)){let t=ce(e);t>=0&&(a=i+t)}if(t.length===0)continue;let n=oe(e),o=_e(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=be(e,r),c={kind:`paragraph`,id:t,text:o,runs:r,xmlRef:e,style:ve(e),listInfo:ye(e)},l=S(e,`w14:paraId`);if(l&&(c.paraId=l.toUpperCase()),s.length>0&&(c.revisions=s),le(e)){c.hasPageBreak=!0;let t=b(e,`w:pPr`);if(t){let e=b(t,`w:pageBreakBefore`);if(e){let t=S(e,`w:val`);if(t!==`0`&&t!==`false`)return c.pageBreakOffset=0,c}}a!==void 0&&(c.pageBreakOffset=a)}return c}function _e(e){let t=b(e,`w:rPr`);if(!t)return{};let n={},r=y(t);for(let e of r)if(v(e,`w:b`)){let t=S(e,`w:val`);n.bold=t!==`0`&&t!==`false`}else if(v(e,`w:i`)){let t=S(e,`w:val`);n.italic=t!==`0`&&t!==`false`}else if(v(e,`w:u`))n.underline=S(e,`w:val`)??`single`;else if(v(e,`w:strike`)){let t=S(e,`w:val`);n.strikethrough=t!==`0`&&t!==`false`}else if(v(e,`w:sz`)){let t=S(e,`w:val`);t&&(n.fontSize=parseInt(t,10))}else if(v(e,`w:rFonts`))n.fontFamily=S(e,`w:ascii`)??S(e,`w:hAnsi`);else if(v(e,`w:color`)){n.color=S(e,`w:val`);let t=S(e,`w:themeColor`);t&&(n.themeColor=t)}else v(e,`w:highlight`)&&(n.highlight=S(e,`w:val`));return n}function ve(e){let t=b(e,`w:pPr`);if(!t)return;let n=b(t,`w:pStyle`);if(n)return S(n,`w:val`)}function ye(e){let t=b(e,`w:pPr`);if(!t)return;let n=b(t,`w:numPr`);if(!n)return;let r=b(n,`w:numId`),i=b(n,`w:ilvl`),a=r?S(r,`w:val`):void 0,o=i?S(i,`w:val`):void 0;if(a&&a!==`0`)return{numId:a,level:o?parseInt(o,10):0}}function be(e,t){let n=[],r=new Set;xe(e,r);for(let i of t)if(r.has(i.xmlRef)){let t=Ce(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 Ee(e,t,n),n}function xe(e,t){let n=y(e);for(let e of n){let n=_(e);n===`w:ins`?Se(e,t):n===`w:hyperlink`&&xe(e,t)}}function Se(e,t){let n=y(e);for(let e of n){let n=_(e);n===`w:r`?t.add(e):(n===`w:hyperlink`||n===`w:ins`)&&Se(e,t)}}function Ce(e,t){return we(y(e),t)}function we(e,t){for(let n of e){let e=_(n);if(e===`w:ins`){if(Te(n,t))return{author:S(n,`w:author`)??``,date:S(n,`w:date`)??``,id:S(n,`w:id`)}}else if(e===`w:hyperlink`){let e=we(y(n),t);if(e)return e}}}function Te(e,t){let n=y(e);for(let e of n){if(e===t)return!0;let n=_(e);if(n&&n!==`w:del`&&Te(e,t))return!0}return!1}function Ee(e,t,n){let r=new Map;for(let e of t)r.set(e.xmlRef,e);De(y(e),r,n,0)}function De(e,t,n,r){for(let i of e){let e=_(i);if(e===`w:r`){let e=t.get(i);e&&(r=e.endOffset)}else if(e===`w:del`){let e=Oe(i);e.length>0&&n.push({type:`delete`,text:e,startOffset:r,endOffset:r,author:S(i,`w:author`)??``,date:S(i,`w:date`)??``,...S(i,`w:id`)===void 0?{}:{id:S(i,`w:id`)}})}else (e===`w:hyperlink`||e===`w:ins`)&&(r=De(y(i),t,n,r))}return r}function Oe(e){let t=y(e),n=``;for(let e of t)if(_(e)===`w:r`){let t=y(e);for(let e of t)if(`w:br`in e){let t=S(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+=T(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+=T(String(e[`#text`])))}}return n}function j(e,t){let n=[],r=x(e,`w:tr`);for(let e=0;e<r.length;e++){let i=r[e],a=[],o=x(i,`w:tc`);for(let n=0;n<o.length;n++){let r=o[n],i=x(r,`w:p`).map((r,i)=>A(r,`${t}.r${e}.c${n}.p${i}`)),s=x(r,`w:tbl`).map((r,i)=>j(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=b(e,`w:tblPr`),a=i?b(i,`w:tblStyle`):void 0;return{kind:`table`,id:t,rows:n,xmlRef:e,style:a?S(a,`w:val`):void 0}}function ke(e,t,n,r){let i=t===`header`?`hdr`:`ftr`,a=t===`header`?`w:hdr`:`w:ftr`,o=e.find(e=>_(e)===a),s=[],c=[];if(o){let e=y(o),t=0,r=0;for(let a of e){let e=_(a);e===`w:p`?(s.push(A(a,`${i}.${n}.p${t}`)),t++):e===`w:tbl`&&(c.push(j(a,`${i}.${n}.tbl${r}`)),r++)}}return{type:t,variant:n,rId:r,paragraphs:s,tables:c,xmlRef:e}}function Ae(e){if(e.xmlParts.numbering)return e.xmlParts.numbering;let t=O(`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=>_(e)===`Types`);if(t){let e=y(t);e.some(e=>_(e)===`Override`&&S(e,`PartName`)===`/word/numbering.xml`)||e.push(O(`Override`,[],{PartName:`/word/numbering.xml`,ContentType:`application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml`}))}}if(e.xmlParts.documentRels){let t=e.xmlParts.documentRels.find(e=>_(e)===`Relationships`);if(t){let e=y(t);if(!e.some(e=>S(e,`Type`)===`http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering`)){let t=0;for(let n of e){let e=S(n,`Id`);if(e){let n=parseInt(e.replace(`rId`,``),10);!isNaN(n)&&n>t&&(t=n)}}e.push(O(`Relationship`,[],{Id:`rId${t+1}`,Type:`http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering`,Target:`numbering.xml`}))}}}return e.xmlParts.numbering}function je(e){let t=e.find(e=>_(e)===`w:numbering`);if(!t)return 1;let n=0;for(let e of x(t,`w:num`)){let t=S(e,`w:numId`);if(t){let e=parseInt(t,10);!isNaN(e)&&e>n&&(n=e)}}return n+1}function Me(e){let t=e.find(e=>_(e)===`w:numbering`);if(!t)return 0;let n=-1;for(let e of x(t,`w:abstractNum`)){let t=S(e,`w:abstractNumId`);if(t){let e=parseInt(t,10);!isNaN(e)&&e>n&&(n=e)}}return n+1}const Ne=[{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`}],Pe=[{fmt:`decimal`,textFn:e=>`%${e+1}.`},{fmt:`lowerLetter`,textFn:e=>`%${e+1}.`},{fmt:`lowerRoman`,textFn:e=>`%${e+1}.`}];function Fe(e,t,n=9){let r=Ae(e),i=y(r.find(e=>_(e)===`w:numbering`)),a=Me(r),o=je(r),s=[];for(let e=0;e<n;e++){let n,r;if(t===`bullet`){n=`bullet`;let t=Ne[e%Ne.length];r=t.char;let i=O(`w:rPr`,[O(`w:rFonts`,[],{"w:ascii":t.font,"w:hAnsi":t.font,"w:hint":`default`})]);s.push(O(`w:lvl`,[O(`w:start`,[],{"w:val":`1`}),O(`w:numFmt`,[],{"w:val":n}),O(`w:lvlText`,[],{"w:val":r}),O(`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=Pe[e%Pe.length];n=t.fmt,r=t.textFn(e)}s.push(O(`w:lvl`,[O(`w:start`,[],{"w:val":`1`}),O(`w:numFmt`,[],{"w:val":n}),O(`w:lvlText`,[],{"w:val":r}),O(`w:lvlJc`,[],{"w:val":`left`})],{"w:ilvl":String(e)}))}let c=O(`w:abstractNum`,s,{"w:abstractNumId":String(a)}),l=O(`w:num`,[O(`w:abstractNumId`,[],{"w:val":String(a)})],{"w:numId":String(o)}),u=i.findIndex(e=>_(e)===`w:num`);return u>=0?i.splice(u,0,c):i.push(c),i.push(l),String(o)}function Ie(e,t){let n=e.find(e=>_(e)===`w:numbering`);if(!n)return;let r=x(n,`w:num`).find(e=>S(e,`w:numId`)===t);if(!r)return;let i=b(r,`w:abstractNumId`);if(!i)return;let a=S(i,`w:val`);if(!a)return;let o=x(n,`w:abstractNum`).find(e=>S(e,`w:abstractNumId`)===a);if(!o)return;let s=x(o,`w:lvl`),c=!1,l=!1;for(let e of s){let t=b(e,`w:numFmt`);t&&S(t,`w:val`)===`bullet`&&(c=!0);let n=b(e,`w:lvlText`);if(n){let e=(S(n,`w:val`)??``).match(/%\d+/g);e&&e.length>1&&(l=!0)}}return c?`bullet`:l?`multilevel`:`ordered`}function Le(e,t,n){let r=e.find(e=>_(e)===`w:numbering`);if(!r)return null;let i=x(r,`w:num`).find(e=>S(e,`w:numId`)===t);if(!i)return null;let a=b(i,`w:abstractNumId`);if(!a)return null;let o=S(a,`w:val`);if(!o)return null;let s=x(r,`w:abstractNum`).find(e=>S(e,`w:abstractNumId`)===o);if(!s)return null;let c=x(s,`w:lvl`),l=c.find(e=>S(e,`w:ilvl`)===String(n));if(!l)return null;let u=b(l,`w:numFmt`),d=b(l,`w:lvlText`),f=u?S(u,`w:val`)??`decimal`:`decimal`,p=d?S(d,`w:val`)??``:``,m=new Map,ee=p.match(/%(\d+)/g);if(ee)for(let e of ee){let t=parseInt(e.slice(1),10)-1,n=c.find(e=>S(e,`w:ilvl`)===String(t));if(n){let e=b(n,`w:numFmt`);m.set(t,e?S(e,`w:val`)??`decimal`:`decimal`)}}return{pattern:p,format:f,ancestorFormats:m}}const Re=new Set([`w:document`,`w:hdr`,`w:ftr`,`w:footnotes`,`w:endnotes`,`w:comments`]),ze=new WeakMap;function Be(e){let t=ze.get(e.xmlParts);if(!t){t=new Set;for(let n of Ve(e.xmlParts))He(n,t);ze.set(e.xmlParts,t)}return t}function*Ve(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 He(e,t){for(let n of e){let e=_(n);if(!e)continue;if(e===`w:p`){let e=S(n,`w14:paraId`);e&&t.add(e.toUpperCase())}let r=y(n);r.length>0&&He(r,t)}}function Ue(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 We(e){if(!/^[0-9A-Fa-f]{8}$/.test(e))return!1;let t=parseInt(e,16);return t>0&&t<2147483648}function Ge(e){return e?S(e,`xmlns:w14`)!==void 0:!1}function Ke(e){S(e,`xmlns:w14`)===void 0&&C(e,`xmlns:w14`,`http://schemas.microsoft.com/office/word/2010/wordml`),S(e,`xmlns:mc`)===void 0&&C(e,`xmlns:mc`,`http://schemas.openxmlformats.org/markup-compatibility/2006`);let t=S(e,`mc:Ignorable`);if(t===void 0)C(e,`mc:Ignorable`,`w14`);else{let n=t.split(/\s+/).filter(e=>e.length>0);n.includes(`w14`)||(n.push(`w14`),C(e,`mc:Ignorable`,n.join(` `)))}}function qe(e){return e.find(e=>{let t=_(e);return t!==void 0&&Re.has(t)})}function Je(e,t){let n=qe(e);if(!n)return 0;let r=0,i=e=>{for(let n of e){let e=_(n);if(!e)continue;if(e===`w:p`){let e=S(n,`w14:paraId`);if(!e||!We(e)){let e=Ue(t);C(n,`w14:paraId`,e),r++,S(n,`w14:textId`)===void 0&&C(n,`w14:textId`,e)}else S(n,`w14:textId`)===void 0&&C(n,`w14:textId`,e.toUpperCase())}let a=y(n);a.length>0&&i(a)}};return i(y(n)),r>0&&Ke(n),r}function Ye(e){let t=new Set;for(let n of Ve(e))He(n,t);Je(e.document,t);for(let n of e.headers.values())Je(n,t);for(let n of e.footers.values())Je(n,t);ze.set(e,t)}function M(e,t,n,r=!1){if(!Ge(n))return;let i=Be(e),a=e=>{for(let t of e){let e=_(t);if(!e)continue;if(e===`w:p`&&(r||S(t,`w14:paraId`)===void 0)){let e=Ue(i);C(t,`w14:paraId`,e),C(t,`w14:textId`,e)}let n=y(t);n.length>0&&a(n)}};a(t)}function N(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=>_(e)===r);return}return e.xmlParts.document.find(e=>_(e)===`w:document`)}function Xe(e,t){let n=g(e.documentXml),r=new Map;for(let[t,n]of e.headers)r.set(t,g(n));let i=new Map;for(let[t,n]of e.footers)i.set(t,g(n));let a=e.stylesXml?et(e.stylesXml):[],o=e=>e?g(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&&Ye(s);let c={body:[],headers:new Map,footers:new Map,xmlParts:s,styles:a,loadOptions:t};return P(c),c}function P(e){let t=e.loadOptions?.includeHeadersFooters??!0;if(e.body=Ze(e.xmlParts.document),e.headers=new Map,e.footers=new Map,t){let t=Qe(e.xmlParts.documentRels),n=$e(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,ke(t,`header`,r.variant,r.rId))}else{let t=e.xmlParts.footers.get(i);t&&e.footers.set(r.rId,ke(t,`footer`,r.variant,r.rId))}}}tt(e)}function Ze(e){let t=[],n=e.find(e=>_(e)===`w:document`);if(!n)return t;let r=b(n,`w:body`);if(!r)return t;let i=y(r),a=0,o=0,s=0;for(let e of i){let n=_(e);if(n===`w:p`){let n=A(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(j(e,`tbl${o}`)),o++)}return t}function Qe(e){let t=new Map;if(!e)return t;let n=e.find(e=>_(e)===`Relationships`);if(!n)return t;let r=x(n,`Relationship`);for(let e of r){let n=S(e,`Id`),r=S(e,`Target`);n&&r&&t.set(n,r)}return t}function $e(e){let t=[],n=e.find(e=>_(e)===`w:document`);if(!n)return t;let r=b(n,`w:body`);if(!r)return t;let i=x(r,`w:sectPr`),a=y(r);for(let e of a)if(_(e)===`w:p`){let t=b(e,`w:pPr`);if(t){let e=b(t,`w:sectPr`);e&&i.push(e)}}for(let e of i){let n=y(e);for(let e of n){let n=_(e);if(n===`w:headerReference`){let n=S(e,`r:id`),r=S(e,`w:type`)??`default`;n&&t.push({type:`header`,variant:r,rId:n})}else if(n===`w:footerReference`){let n=S(e,`r:id`),r=S(e,`w:type`)??`default`;n&&t.push({type:`footer`,variant:r,rId:n})}}}return t}function et(e){let t=g(e),n=[],r=t.find(e=>_(e)===`w:styles`);if(!r)return n;let i=x(r,`w:style`);for(let e of i){let t=S(e,`w:styleId`),r=S(e,`w:type`);if(!t||!r||![`paragraph`,`character`,`table`,`numbering`].includes(r))continue;let i=b(e,`w:name`),a=i?S(i,`w:val`)??t:t;n.push({id:t,name:a,type:r})}return n}function tt(e){let t=e.xmlParts.numbering;if(t){for(let n of e.body)if(n.kind===`paragraph`)F(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)F(e,t);for(let n of e.headers.values())for(let e of n.paragraphs)F(e,t);for(let n of e.footers.values())for(let e of n.paragraphs)F(e,t)}}function F(e,t){if(!e.listInfo)return;let n=Ie(t,e.listInfo.numId);n&&(e.listInfo.listType=n)}function nt(e){let t=e.split(`
6
+ `),n=[],r=0;for(;r<t.length;){if(r+1<t.length&&st(t[r])&&ct(t[r+1])){let e=lt(t[r]);r+=2;let i=[];for(;r<t.length&&st(t[r]);){let n=lt(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=ot(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=ot(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 rt(e){return nt(e).some(e=>e.kind!==`text`)}const it=/^(\s*)[*+-]\s(.*)$/,at=/^(\s*)\d+\.\s(.*)$/;function ot(e){let t=e.replace(/\t/g,` `),n=it.exec(t);if(n){let e=n[1].length;return{listType:`bullet`,text:n[2],level:Math.floor(e/2)}}if(n=at.exec(t),n){let e=n[1].length;return{listType:`ordered`,text:n[2],level:Math.floor(e/2)}}}function st(e){let t=e.trim();return t.startsWith(`|`)&&t.endsWith(`|`)&&t.length>1}function ct(e){let t=e.trim();return!t.startsWith(`|`)||!t.endsWith(`|`)?!1:t.slice(1,-1).split(`|`).every(e=>/^\s*:?-+:?\s*$/.test(e))}function lt(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 ut(e,t){switch(e.type){case`insert_paragraph`:return I(e.target,t)?!e.text||e.text.length===0?{valid:!1,error:`Empty paragraph text`}:[`before`,`after`].includes(e.position)?rt(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 I(e.target,t)?{valid:!0}:{valid:!1,error:`Paragraph "${e.target}" not found`};case`add_comment`:return I(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 I(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=L(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=L(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 I(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(!I(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=dt(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(!_t(e.afterId,t))return{valid:!1,error:`Element "${e.afterId}" not found`}}if(e.container){let n=dt(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&&!_t(e.afterId,t))return{valid:!1,error:`Element "${e.afterId}" not found`};if(e.container){let n=dt(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=>_(e)===`w:numbering`);if(!(n&&y(n).some(t=>_(t)===`w:num`&&S(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=I(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=I(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(!I(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=I(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=I(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 dt(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 I(e,t){return e.startsWith(`para:`)?ft(e.slice(5),t):pt(t,t=>t.id===e)}function ft(e,t){let n=e.toUpperCase();return pt(t,e=>e.paraId===n)}function pt(e,t){for(let n of e.body){let e=ht(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=gt(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=gt(t,e);if(n)return n}}}function L(e,t){for(let n of t.body)if(n.kind===`table`){let t=mt(e,n);if(t)return t}for(let n of t.headers.values())for(let t of n.tables){let n=mt(e,t);if(n)return n}for(let n of t.footers.values())for(let t of n.tables){let n=mt(e,t);if(n)return n}}function mt(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=mt(e,n);if(t)return t}}function ht(e,t){if(t.kind===`paragraph`)return e(t)?t:void 0;if(t.kind===`table`)return gt(e,t)}function gt(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=gt(e,n);if(t)return t}}}function _t(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 R(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 vt(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 yt(e){if(`w:t`in e)return!0;if(`w:br`in e){let t=S(e,`w:type`);return!t||t===`textWrapping`}return!1}function bt(e){let t={text:``,anchored:[]};for(let n of y(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+=T(String(n[`#text`])))}else yt(n)?t.text+=`
7
+ `:t.anchored.push({node:n,offset:t.text.length});return t}function xt(e,t){let n=y(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(pe(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(pe(``))}function St(e){let t={"w:r":[]};return xt(t,{rPr:e.rPr?E(e.rPr):void 0,text:e.text,anchored:e.anchored}),t}function Ct(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 wt(e){return e.text.length>0||e.anchored.length>0}function z(e,t){let n=y(e);return n.includes(t)?n:Tt(n,t)}function Tt(e,t){for(let n of e){let e=_(n);if(e===`w:hyperlink`||e===`w:ins`||e===`w:del`){let e=y(n);if(e.includes(t))return e;let r=Tt(e,t);if(r)return r}}}function B(e,t){if(t.isFullRun)return{matched:t.run.xmlRef};let n=t.run.xmlRef,r=z(e,n);if(!r)throw Error(`Run not found in paragraph children or hyperlink children`);let i=r.indexOf(n),a=Ct(bt(n),t.localStart,t.localEnd),o={matched:void 0},s=[];if(wt(a.before)){let e=St(a.before);o.before=e,s.push(e)}let c=St(a.matched);if(o.matched=c,s.push(c),wt(a.after)){let e=St(a.after);o.after=e,s.push(e)}return r.splice(i,1,...s),o}function Et(e,t,n,r=`before`){let i=z(e,t);if(!i)throw Error(`Run not found in paragraph children or hyperlink children`);let a=i.indexOf(t),o=Ct(bt(t),n,n,r),s=[],c={parentChildren:i,index:a};return wt(o.before)&&(c.before=St(o.before),s.push(c.before),c.index=a+1),wt(o.after)&&(c.after=St(o.after),s.push(c.after)),i.splice(a,1,...s),c}function V(e,t){if(t.length===0)return[];if(t.length===1)return[B(e,t[0]).matched];let n=[],r=[],i=t[t.length-1];if(!i.isEndAligned){let n=B(e,i);r.push({index:t.length-1,split:n})}let a=t[0];if(!a.isStartAligned){let t=B(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=z(e,a.run.xmlRef);t&&t.includes(a.run.xmlRef)&&n.push(a.run.xmlRef)}}return n}function Dt(e,t){return{nextId:kt(e)+1,author:t.revisionAuthor??`docxtor`,date:t.revisionDate??new Date().toISOString()}}function H(e){let t=String(e.nextId);return e.nextId++,t}const Ot=new Set([`w:ins`,`w:del`,`w:rPrChange`,`w:pPrChange`,`w:trPrChange`,`w:tblPrChange`,`w:sectPrChange`,`w:cellIns`,`w:cellDel`,`w:cellMerge`]);function kt(e){let t=0;t=Math.max(t,U(e.xmlParts.document));for(let n of e.xmlParts.headers.values())t=Math.max(t,U(n));for(let n of e.xmlParts.footers.values())t=Math.max(t,U(n));for(let n of[`footnotes`,`endnotes`,`comments`]){let r=e.xmlParts[n];r&&(t=Math.max(t,U(r)))}return t}function U(e){let t=0;for(let n of e){let e=_(n);if(e&&Ot.has(e)){let e=S(n,`w:id`);if(e){let n=parseInt(e,10);!isNaN(n)&&n>t&&(t=n)}}if(e){let e=y(n);e.length>0&&(t=Math.max(t,U(e)))}}return t}function W(e,t){return O(`w:ins`,t,{"w:id":H(e),"w:author":e.author,"w:date":e.date})}function At(e,t){return O(`w:del`,t,{"w:id":H(e),"w:author":e.author,"w:date":e.date})}function jt(e,t){let n=H(e);return O(`w:rPrChange`,[E(t)],{"w:id":n,"w:author":e.author,"w:date":e.date})}function G(e,t){let n=H(e);return O(`w:pPrChange`,[E(t)],{"w:id":n,"w:author":e.author,"w:date":e.date})}function Mt(e){return O(`w:ins`,[],{"w:id":H(e),"w:author":e.author,"w:date":e.date})}function Nt(e){return O(`w:del`,[],{"w:id":H(e),"w:author":e.author,"w:date":e.date})}function Pt(e){let t=y(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 Ft(e,t){let n=[];if(t&&n.push(E(t)),e.includes(`
7
9
  `)){let t=e.split(`
8
- `);for(let e=0;e<t.length;e++){let r=t[e];if(r.length>0||e===0){let e={"w:t":[{"#text":T(r)}]};A(r)&&w(e,`xml:space`,`preserve`),n.push(e)}e<t.length-1&&n.push({"w:br":[]})}}else{let t={"w:t":[{"#text":T(e)}]};A(e)&&w(t,`xml:space`,`preserve`),n.push(t)}return{"w:r":n}}const At=new Set([`*`,`_`,`~`,`<`,`\\`]);function K(e,t=!0){return t?q(e).map(e=>e.text).join(``):e}function q(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&&At.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]===`_`&&jt(e,r,o)){l(),o=!o,r+=1;continue}i+=e[r],r++}return l(),Mt(n)}function jt(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 Mt(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 Nt(e,t){if(O(e,t))return;let n=b(e);for(let e of n){let n=v(e);if(n===`w:hyperlink`||n===`w:ins`||n===`w:del`){if(O(e,t))return;let n=b(e);for(let e of n){let n=v(e);if((n===`w:hyperlink`||n===`w:ins`||n===`w:del`)&&O(e,t))return}}}}function Pt(e,t){let n=b(e);if(!n.includes(t))for(let e of n){let n=v(e);if(n===`w:hyperlink`||n===`w:ins`||n===`w:del`){let n=b(e);if(n.includes(t))return e;for(let e of n){let n=v(e);if((n===`w:hyperlink`||n===`w:ins`||n===`w:del`)&&b(e).includes(t))return e}}}}function Ft(e,t,n,r=!0){let i=t.occurrence??1,a=K(t.find,r),o=z(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=_t(e.runs,o,s);if(c.length===0)throw Error(`No runs found for text range [${o}, ${s}] in paragraph "${e.id}"`);n?Lt(e,t,c,n,r):It(e,t,c,r)}function It(e,t,n,r){let i=q(t.replace,r),a=i.some(e=>e.bold||e.italic||e.strikethrough||e.underline),o=K(t.replace,r);if(n.length===1){let t=n[0],r=V(e.xmlRef,t);a?Kt(e.xmlRef,r.matched,i):qt(r.matched,o)}else{let t=H(e.xmlRef,n);if(t.length===0)throw Error(`Failed to isolate matched runs in paragraph "${e.id}"`);a?Kt(e.xmlRef,t[0],i):qt(t[0],o);for(let n=1;n<t.length;n++)Nt(e.xmlRef,t[n])}}function Lt(e,t,n,r,i){let a;if(n.length===1){let t=n[0];a=[V(e.xmlRef,t).matched]}else a=H(e.xmlRef,n);if(a.length===0)throw Error(`Failed to isolate matched runs in paragraph "${e.id}"`);let o=x(a[0],`w:rPr`),s=B(e.xmlRef,a[0]);if(!s)throw Error(`Matched run not found in paragraph`);let c=Pt(e.xmlRef,a[0]),l=c!=null&&v(c)===`w:ins`,u=q(t.replace,i),d=u.some(e=>e.bold||e.italic||e.strikethrough||e.underline),f=K(t.replace,i),p=[];u.length>0&&f.length>0&&(p=d?Gt(u,o):[kt(f,o)]);let m=p.length>0?W(r,p):void 0,h=s.indexOf(a[0]);for(let e of a){let t=s.indexOf(e);t!==-1&&s.splice(t,1)}if(l)Rt(e.xmlRef,c,h,p,r);else{let e=[];for(let t of a){let n=D(t);Ot(n),e.push(n)}let t=wt(r,e);m?s.splice(h,0,t,m):s.splice(h,0,t)}}function Rt(e,t,n,r,i){let a=b(t),o=B(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(C(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(W(i,r)),c.length>0){let e=k(`w:ins`,c,{"w:id":U(i),"w:author":C(t,`w:author`)??``,"w:date":C(t,`w:date`)??``});l.push(e)}o.splice(s,1,...l)}function zt(e,t,n,r=!0){let i=t.occurrence??1,a=K(t.anchor,r),o=z(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?Vt(e,t,s,n,r):Bt(e,t,s,r)}function Bt(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=q(t.text,r);if(a.some(e=>e.bold||e.italic||e.strikethrough||e.underline)){let t=n-i.startOffset,r=x(i.xmlRef,`w:rPr`),o=B(e.xmlRef,i.xmlRef);if(!o)throw Error(`Target run not found in paragraph`);let s=o.indexOf(i.xmlRef),c=i.text,l=c.slice(0,t),u=c.slice(t),d=[];l.length>0&&d.push(Ht(l,r));let f=Gt(a,r);d.push(...f),u.length>0&&d.push(Ht(u,r)),o.splice(s,1,...d)}else{let e=K(t.text,r),a=n-i.startOffset,o=ie(i.xmlRef),s=o.slice(0,a)+e+o.slice(a);qt(i.xmlRef,s)}}function Vt(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=x(a.xmlRef,`w:rPr`),c=q(t.text,i),l=c.some(e=>e.bold||e.italic||e.strikethrough||e.underline),u;u=l?Gt(c,s):[kt(K(t.text,i),s)];let d=W(r,u),f=B(e.xmlRef,a.xmlRef);if(!f)throw Error(`Target run not found in paragraph`);let p=f.indexOf(a.xmlRef),m=o===0,h=o===a.text.length;if(m)f.splice(p,0,d);else if(h)f.splice(p+1,0,d);else{let e=a.text,t=e.slice(0,o),n=e.slice(o),r=Ht(t,s),i=Ht(n,s);f.splice(p,1,r,d,i)}}function Ht(e,t){let n=[];if(t&&n.push(D(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":w(r)}]};k(r)&&C(e,`xml:space`,`preserve`),n.push(e)}e<t.length-1&&n.push({"w:br":[]})}}else{let t={"w:t":[{"#text":w(e)}]};k(e)&&C(t,`xml:space`,`preserve`),n.push(t)}return{"w:r":n}}const It=new Set([`*`,`_`,`~`,`<`,`\\`]);function K(e,t=!0){return t?q(e).map(e=>e.text).join(``):e}function q(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&&It.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]===`_`&&Lt(e,r,o)){l(),o=!o,r+=1;continue}i+=e[r],r++}return l(),Rt(n)}function Lt(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 Rt(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 zt(e,t){if(D(e,t))return;let n=y(e);for(let e of n){let n=_(e);if(n===`w:hyperlink`||n===`w:ins`||n===`w:del`){if(D(e,t))return;let n=y(e);for(let e of n){let n=_(e);if((n===`w:hyperlink`||n===`w:ins`||n===`w:del`)&&D(e,t))return}}}}function Bt(e,t){let n=y(e);if(!n.includes(t))for(let e of n){let n=_(e);if(n===`w:hyperlink`||n===`w:ins`||n===`w:del`){let n=y(e);if(n.includes(t))return e;for(let e of n){let n=_(e);if((n===`w:hyperlink`||n===`w:ins`||n===`w:del`)&&y(e).includes(t))return e}}}}function Vt(e,t,n,r=!0){let i=t.occurrence??1,a=K(t.find,r),o=R(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=vt(e.runs,o,s);if(c.length===0)throw Error(`No runs found for text range [${o}, ${s}] in paragraph "${e.id}"`);n?Ut(e,t,c,n,r):Ht(e,t,c,r)}function Ht(e,t,n,r){let i=q(t.replace,r),a=i.some(e=>e.bold||e.italic||e.strikethrough||e.underline),o=K(t.replace,r);if(n.length===1){let t=n[0],r=B(e.xmlRef,t);a?en(e.xmlRef,r.matched,i):tn(r.matched,o)}else{let t=V(e.xmlRef,n);if(t.length===0)throw Error(`Failed to isolate matched runs in paragraph "${e.id}"`);a?en(e.xmlRef,t[0],i):tn(t[0],o);for(let n=1;n<t.length;n++){let r=bt(t[n]);r.anchored.length>0?xt(t[n],{...r,text:``}):zt(e.xmlRef,t[n])}}}function Ut(e,t,n,r,i){let a;if(n.length===1){let t=n[0];a=[B(e.xmlRef,t).matched]}else a=V(e.xmlRef,n);if(a.length===0)throw Error(`Failed to isolate matched runs in paragraph "${e.id}"`);let o=b(a[0],`w:rPr`),s=q(t.replace,i),c=s.some(e=>e.bold||e.italic||e.strikethrough||e.underline),l=K(t.replace,i),u=[];s.length>0&&l.length>0&&(u=c?$t(s,o):[Ft(l,o)]);let d=Wt(e.xmlRef,a,r);u.length!==0&&(d.container?Gt(e.xmlRef,d.container,d.index,u,r):d.parentChildren.splice(d.index,0,W(r,u)))}function Wt(e,t,n){let r,i=[];for(let n of t){let t=z(e,n);if(!t)throw Error(`Matched run not found in paragraph`);let a=Bt(e,n),o=a!=null&&_(a)===`w:ins`,s=t.indexOf(n);if(t.splice(s,1),o){let n=!0,i,o=-1;t.length===0&&(i=z(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=E(e);return Pt(t),t});t.parent.splice(t.index,0,At(n,r))}if(!r){let e=i[0];r={parentChildren:e.parent,index:e.index+1}}return r}function Gt(e,t,n,r,i){let a=y(t),o=z(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(S(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(W(i,r)),c.length>0){let e=O(`w:ins`,c,{"w:id":H(i),"w:author":S(t,`w:author`)??``,"w:date":S(t,`w:date`)??``});l.push(e)}o.splice(s,1,...l)}function Kt(e,t,n,r=!0){let i=t.occurrence??1,a=K(t.anchor,r),o=R(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?Jt(e,t,s,n,r):qt(e,t,s,r)}function qt(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=q(t.text,r);if(a.some(e=>e.bold||e.italic||e.strikethrough||e.underline)){let r=n-i.startOffset,o=$t(a,b(i.xmlRef,`w:rPr`)),s=Et(e.xmlRef,i.xmlRef,r,Yt(t));s.parentChildren.splice(s.index,0,...o)}else{let e=K(t.text,r),a=n-i.startOffset,o=ae(i.xmlRef),s=o.slice(0,a)+e+o.slice(a),c=Yt(t);tn(i.xmlRef,s,t=>t<a||t===a&&c===`before`?t:t+e.length)}}function Jt(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=b(a.xmlRef,`w:rPr`),c=q(t.text,i),l=c.some(e=>e.bold||e.italic||e.strikethrough||e.underline),u;u=l?$t(c,s):[Ft(K(t.text,i),s)];let d=W(r,u),f=z(e.xmlRef,a.xmlRef);if(!f)throw Error(`Target run not found in paragraph`);let p=f.indexOf(a.xmlRef),m=o===0,ee=o===a.text.length;if(m)f.splice(p,0,d);else if(ee)f.splice(p+1,0,d);else{let n=Et(e.xmlRef,a.xmlRef,o,Yt(t));n.parentChildren.splice(n.index,0,d)}}function Yt(e){return e.position===`after`?`after`:`before`}function Xt(e,t){let n=[];if(t&&n.push(E(t)),e.includes(`
9
11
  `)){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={"#text":T(r)},t={"w:t":[e]};A(r)&&w(t,`xml:space`,`preserve`),n.push(t)}e<t.length-1&&n.push({"w:br":[]})}}else{let t={"#text":T(e)},r={"w:t":[t]};A(e)&&w(r,`xml:space`,`preserve`),n.push(r)}return{"w:r":n}}function Ut(e,t,n,r=!0){n?Wt(e,t,n,r):Ft(e,{type:`replace_text`,target:t.target,find:t.find,replace:``,occurrence:t.occurrence},void 0,r)}function Wt(e,t,n,r){let i=t.occurrence??1,a=K(t.find,r),o=z(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=_t(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=[V(e.xmlRef,t).matched]}else l=H(e.xmlRef,c);if(l.length===0)throw Error(`Failed to isolate matched runs in paragraph "${e.id}"`);let u=B(e.xmlRef,l[0]);if(!u)throw Error(`Matched run not found in paragraph`);let d=Pt(e.xmlRef,l[0]),f=d!=null&&v(d)===`w:ins`,p=u.indexOf(l[0]);for(let e of l){let t=u.indexOf(e);t!==-1&&u.splice(t,1)}if(f){if(u.length===0&&d){let t=b(e.xmlRef),n=t.indexOf(d);n!==-1&&t.splice(n,1)}}else{let e=[];for(let t of l){let n=D(t);Ot(n),e.push(n)}let t=wt(n,e);u.splice(p,0,t)}}function Gt(e,t){let n=[];for(let r of e){if(r.text.length===0)continue;let e=Ht(r.text,t);if(r.bold||r.italic||r.strikethrough||r.underline){let t=x(e,`w:rPr`);t||(t=k(`w:rPr`,[]),b(e).unshift(t));let n=b(t);r.bold&&!x(t,`w:b`)&&n.push(k(`w:b`,[])),r.italic&&!x(t,`w:i`)&&n.push(k(`w:i`,[])),r.strikethrough&&!x(t,`w:strike`)&&n.push(k(`w:strike`,[])),r.underline&&!x(t,`w:u`)&&n.push(k(`w:u`,[],{"w:val":`single`}))}n.push(e)}return n}function Kt(e,t,n){let r=Gt(n,x(t,`w:rPr`));if(r.length===0){qt(t,``);return}let i=B(e,t);if(i){let e=i.indexOf(t);if(e!==-1){i.splice(e,1,...r);return}}qt(t,n.map(e=>e.text).join(``))}function qt(e,t){if(t.includes(`
11
- `)||Jt(e)){Yt(e,t);return}let n=ae(e);if(n){let e=n[`w:t`];if(Array.isArray(e)){let n=e.find(e=>`#text`in e);n?re(n,t):e.push({"#text":T(t)})}A(t)&&w(n,`xml:space`,`preserve`)}else{let n=b(e),r={"w:t":[{"#text":T(t)}]};A(t)&&w(r,`xml:space`,`preserve`),n.push(r)}}function Jt(e){let t=0;for(let n of b(e))if(`w:br`in n){let e=C(n,`w:type`);if(!e||e===`textWrapping`)return!0}else `w:t`in n&&t++;return t>1}function Yt(e,t){let n=b(e),r=e=>{if(`w:t`in e)return!0;if(`w:br`in e){let t=C(e,`w:type`);return!t||t===`textWrapping`}return!1},i=[],a=[],o=[],s=!1;for(let e of n)`w:rPr`in e?i.push(e):r(e)?s=!0:(s?o:a).push(e);n.length=0,n.push(...i,...a);let c=t.split(`
12
- `);for(let e=0;e<c.length;e++){let t=c[e];if(t.length>0||e===0){let e={"w:t":[{"#text":T(t)}]};A(t)&&w(e,`xml:space`,`preserve`),n.push(e)}e<c.length-1&&n.push({"w:br":[]})}n.push(...o)}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=D(i.xmlRef),o=S(a,`w:tc`);for(let e=0;e<o.length;e++){let n=o[e],r=t.cells[e],i=x(n,`w:p`);if(i){let e=b(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":T(r)}]};A(r)&&(n[`:@`]||={},n[`:@`][`@_xml:space`]=`preserve`);let a={"w:r":[n]};e.push(a)}}let s=b(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 ue(e.xmlRef,i.xmlRef,a);if(n){let e=b(a),t=e.find(e=>`w:trPr`in e);t||(t=k(`w:trPr`,[]),e.unshift(t)),b(t).push(Et(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=b(r.xmlRef),t=e.find(e=>`w:trPr`in e);t||(t=k(`w:trPr`,[]),e.unshift(t)),b(t).push(Dt(n))}else O(e.xmlRef,r.xmlRef)}function Qt(e,t,n,r=!0){Ft(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=k(`w:tblBorders`,[k(`w:top`,[],r),k(`w:left`,[],r),k(`w:bottom`,[],r),k(`w:right`,[],r),k(`w:insideH`,[],r),k(`w:insideV`,[],r)]),a=k(`w:tblPr`,[k(`w:tblStyle`,[],{"w:val":`TableGrid`}),k(`w:tblW`,[],{"w:w":`0`,"w:type":`auto`}),i,k(`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(k(`w:gridCol`,[]));let s=k(`w:tblGrid`,o),c=[];e&&c.push(en(e));for(let e of t)c.push(en(e));return k(`w:tbl`,[a,s,...c])}function en(e){let t=[];for(let n of e){let e={"w:t":[{"#text":T(n)}]};A(n)&&(e[`:@`]||={},e[`:@`][`@_xml:space`]=`preserve`);let r={"w:r":[e]},i={"w:p":[r]},a=k(`w:tc`,[k(`w:tcPr`,[k(`w:tcW`,[],{"w:w":`0`,"w:type":`auto`})]),i]);t.push(a)}return k(`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=z(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=_t(e.runs,a,o);if(s.length===0)throw Error(`No runs found for text range [${a}, ${o}] in paragraph "${e.id}"`);let c=H(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=x(e,`w:rPr`);if(!i&&!r)return;let a=n&&i?D(i):void 0;if(i||(i=k(`w:rPr`),b(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=x(i,`w:rPrChange`);e&&O(i,e);let t=Tt(n,a??k(`w:rPr`));b(i).push(t)}}function on(e,t,n){let r=x(e,t);n===!0?r||b(e).push(k(t)):r&&O(e,r)}function sn(e,t,n,r){let i=x(e,t);if(r!==null)if(i)w(i,n,r);else{let i=k(t,[],{[n]:r});b(e).push(i)}else i&&O(e,i)}function cn(e,t){let n=x(e,`w:rFonts`);if(t!==null)if(n)w(n,`w:ascii`,t),w(n,`w:hAnsi`,t);else{let n=k(`w:rFonts`,[],{"w:ascii":t,"w:hAnsi":t});b(e).push(n)}else n&&(te(n,`w:ascii`),te(n,`w:hAnsi`),Object.keys(ne(n)).length===0&&O(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=x(r,`w:pPr`),a=n&&i?D(i):void 0;i||(i=k(`w:pPr`),b(r).unshift(i));let o=x(i,`w:pStyle`);if(o?w(o,`w:val`,t):(o=k(`w:pStyle`,[],{"w:val":t}),b(i).unshift(o)),n){let e=x(i,`w:pPrChange`);e&&O(i,e);let t=G(n,a??k(`w:pPr`));b(i).push(t)}}function dn(e,t,n,r,i){let a=z(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=_t(e.runs,a,o);if(s.length===0)throw Error(`No runs found for text range [${a}, ${o}] in paragraph "${e.id}"`);let c=H(e.xmlRef,s);for(let e of c)fn(e,r,i)}function fn(e,t,n){let r=x(e,`w:rPr`),i=n&&r?D(r):void 0;r||(r=k(`w:rPr`),b(e).unshift(r));let a=x(r,`w:rStyle`);a&&O(r,a);let o=k(`w:rStyle`,[],{"w:val":t});if(b(r).unshift(o),n){let e=x(r,`w:rPrChange`);e&&O(r,e);let t=Tt(n,i??k(`w:rPr`));b(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 J(e,t,n=!0){let r=mn(e);if(r.length===1&&r[0].kind===`text`)return gn(q(r[0].text,n),t);let i=r.filter(e=>e.kind===`field`).map(e=>e.fieldCode),a=hn(e),o=q(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 Gt(e,t);let n=e.map(e=>e.text).join(``);return n.length>0?[pe(n,t)]:[]}function _n(e,t){let n=()=>t?[JSON.parse(JSON.stringify(t))]:[],r={"w:r":[...n(),k(`w:fldChar`,[],{"w:fldCharType":`begin`})]},i=` ${e} `,a={"w:instrText":[{"#text":i}]};return w(a,`xml:space`,`preserve`),[r,{"w:r":[...n(),a]},{"w:r":[...n(),k(`w:fldChar`,[],{"w:fldCharType":`separate`})]},{"w:r":[...n(),k(`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)):k(`w:rPr`,[]);if(!n)return r;let i=b(r);return t.bold&&!i.some(e=>`w:b`in e)&&i.push(k(`w:b`,[])),t.italic&&!i.some(e=>`w:i`in e)&&i.push(k(`w:i`,[])),t.strikethrough&&!i.some(e=>`w:strike`in e)&&i.push(k(`w:strike`,[])),t.underline&&!i.some(e=>`w:u`in e)&&i.push(k(`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=J(e,void 0,a);if(i){let e=W(i,t.map(e=>e));u.push({"w:p":[e]})}else u.push({"w:p":t})}let d=[k(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=Oe(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=b(i),o=a.find(e=>v(e)===r?(C(e,`w:type`)??`default`)===yn[n]:!1);if(!o)throw Error(`No ${t} with variant "${n}" found in sectPr`);let s=C(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=>v(e)===`Relationships`);if(r){let e=S(r,`Relationship`);for(let t of e){let e=C(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=[k(`Relationships`,[],{xmlns:`http://schemas.openxmlformats.org/package/2006/relationships`})]);let i=k(`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=>v(e)===`Relationships`);a&&b(a).push(i)}function kn(e,t){let n=e.xmlParts.documentRels;if(!n)return;let r=n.find(e=>v(e)===`Relationships`);if(!r)return;let i=S(r,`Relationship`);for(let e of i)if(C(e,`Id`)===t)return C(e,`Target`)}function An(e,t){let n=e.xmlParts.documentRels;if(!n)return;let r=n.find(e=>v(e)===`Relationships`);if(!r)return;let i=b(r),a=i.findIndex(e=>v(e)===`Relationship`&&C(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=k(`Override`,[],{PartName:`/${t}`,ContentType:r}),a=e.xmlParts.contentTypes.find(e=>v(e)===`Types`);a&&b(a).push(i)}function Mn(e,t){if(!e.xmlParts.contentTypes)return;let n=e.xmlParts.contentTypes.find(e=>v(e)===`Types`);if(!n)return;let r=b(n),i=r.findIndex(e=>{if(v(e)!==`Override`)return!1;let n=C(e,`PartName`);return n===`/${t}`||n===t});i!==-1&&r.splice(i,1)}function Nn(e){let t=e.xmlParts.document.find(e=>v(e)===`w:document`);if(!t)return;let n=x(t,`w:body`);if(!n)return;let r=b(n);for(let e=r.length-1;e>=0;e--)if(v(r[e])===`w:sectPr`)return r[e]}function Pn(e,t,n,r){let i=Nn(e);if(!i){i=k(`w:sectPr`,[]);let t=e.xmlParts.document.find(e=>v(e)===`w:document`);if(!t)throw Error(`Cannot find w:document`);let n=x(t,`w:body`);if(!n)throw Error(`Cannot find w:body`);b(n).push(i)}let a=k(t===`header`?`w:headerReference`:`w:footerReference`,[],{"w:type":yn[n],"r:id":r});if(b(i).unshift(a),n===`first`){let e=b(i);e.some(e=>v(e)===`w:titlePg`)||e.push(k(`w:titlePg`,[]))}n===`even`&&Fn(e)}function Fn(e){e.xmlParts.settings||(e.xmlParts.settings=[k(`w:settings`,[k(`w:evenAndOddHeaders`,[])],{"xmlns:w":`http://schemas.openxmlformats.org/wordprocessingml/2006/main`})]);let t=e.xmlParts.settings.find(e=>v(e)===`w:settings`);if(!t)return;let n=b(t);n.some(e=>v(e)===`w:evenAndOddHeaders`)||n.push(k(`w:evenAndOddHeaders`,[])),In(e),Ln(e)}function In(e){e.xmlParts.documentRels||(e.xmlParts.documentRels=[k(`Relationships`,[],{xmlns:`http://schemas.openxmlformats.org/package/2006/relationships`})]);let t=e.xmlParts.documentRels.find(e=>v(e)===`Relationships`);if(!t)return;let n=`http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings`,r=b(t);r.some(e=>v(e)===`Relationship`?C(e,`Type`)===n&&C(e,`Target`)===`settings.xml`:!1)||r.push(k(`Relationship`,[],{Id:Dn(e),Type:n,Target:`settings.xml`}))}function Ln(e){e.xmlParts.contentTypes||(e.xmlParts.contentTypes=[k(`Types`,[],{xmlns:`http://schemas.openxmlformats.org/package/2006/content-types`})]);let t=e.xmlParts.contentTypes.find(e=>v(e)===`Types`);if(!t)return;let n=`/word/settings.xml`,r=b(t);r.some(e=>v(e)===`Override`?C(e,`PartName`)===n:!1)||r.push(k(`Override`,[],{PartName:n,ContentType:`application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml`}))}function Rn(e){let t=e.xmlParts.document.find(e=>v(e)===`w:document`);if(!t)throw Error(`Cannot find w:document in FXP tree`);let n=x(t,`w:body`);if(!n)throw Error(`Cannot find w:body in FXP tree`);return b(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=>v(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(v(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=>v(t)===e);if(r){let e=b(r);if(e.includes(t))return e;for(let n of e)if(v(n)===`w:tbl`){let e=Hn(n,t);if(e)return e}}}}function Hn(e,t){for(let n of b(e).filter(e=>v(e)===`w:tr`))for(let e of b(n).filter(e=>v(e)===`w:tc`)){let n=b(e);if(n.includes(t))return n;for(let e of n)if(v(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 Y(e,t){let n=j(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&&I(t,e.xmlParts.numbering)}function Kn(e,t,n,r,i=!0){let a=k(`w:pPr`,[k(`w:numPr`,[k(`w:ilvl`,[],{"w:val":String(n)}),k(`w:numId`,[],{"w:val":t})])]),o=J(e,void 0,i),s;return s=r?[a,W(r,o)]:[a,...o],{"w:p":s}}function qn(e,t,n,r=!0){let i=t.numId??N(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=>v(e)===n);if(i){let n=b(i);P(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=j(n,`${a}.${t.container.variant}.p${s++}`);e.xmlParts.numbering&&I(i,e.xmlParts.numbering),r.paragraphs.push(i)}}return}return}let s=Rn(e),c,l;if(t.afterId){let n=gt(t.afterId,e);n&&(c=n.xmlRef,l=n.id)}P(e,o,F(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=j(o[t],n);e.xmlParts.numbering&&I(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=L(t.target,e),a=i.listInfo.numId,o=t.level??i.listInfo.level,s=Kn(t.text,a,o,n,r);P(e,[s],F(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=j(s,Gn(i.id,d));e.xmlParts.numbering&&I(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=L(t.target,e);if(n){let t=b(r.xmlRef),i=x(r.xmlRef,`w:pPr`);if(i){let e=G(n,i);b(i).push(e)}for(let e=t.length-1;e>=0;e--){let r=t[e],i=v(r);if(i===`w:r`){let i=D(r);Ot(i),t[e]=wt(n,[i])}else i===`w:ins`&&t.splice(e,1)}Y(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=N(e,t.listType),i=t.level??0;for(let a of t.targets){let t=L(a,e),o=t.xmlRef,s=x(o,`w:pPr`),c=b(o);if(n&&s){let e=G(n,s);b(s).push(e)}s||(s=k(`w:pPr`,[]),c.unshift(s));let l=b(s),u=l.findIndex(e=>v(e)===`w:numPr`);u!==-1&&l.splice(u,1);let d=k(`w:numPr`,[k(`w:ilvl`,[],{"w:val":String(i)}),k(`w:numId`,[],{"w:val":r})]);l.unshift(d),Y(e,t)}}function Zn(e,t,n){for(let r of t.targets){let t=L(r,e),i=x(t.xmlRef,`w:pPr`);if(!i)continue;if(n){let e=G(n,i);b(i).push(e)}let a=b(i),o=a.findIndex(e=>v(e)===`w:numPr`);o!==-1&&a.splice(o,1),Y(e,t)}}function Qn(e,t,n){let r=L(t.target,e),i=x(r.xmlRef,`w:pPr`);if(!i)throw Error(`No w:pPr found on target paragraph`);if(n){let e=G(n,i);b(i).push(e)}let a=x(i,`w:numPr`);if(!a)throw Error(`No w:numPr found on target paragraph`);if(t.level!==void 0){let e=x(a,`w:ilvl`);e?w(e,`w:val`,String(t.level)):b(a).unshift(k(`w:ilvl`,[],{"w:val":String(t.level)}))}if(t.listType!==void 0){let n=N(e,t.listType),r=x(a,`w:numId`);r?w(r,`w:val`,n):b(a).push(k(`w:numId`,[],{"w:val":n}))}Y(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=x(e,`w:pPr`);if(!t)return;let n=[],r;for(let e of b(t)){let t=v(e);if(t){if(t===`w:rPr`){let t=b(e).filter(e=>{let t=v(e);return t!==void 0&&!er.has(t)}).map(D);t.length>0&&(r=k(`w:rPr`,t));continue}$n.has(t)||n.push(D(e))}}if(r&&n.push(r),n.length!==0)return k(`w:pPr`,n)}function nr(e){let t=e.runs[e.runs.length-1];if(!t)return;let n=x(t.xmlRef,`w:rPr`);if(!n)return;let r=b(n).filter(e=>v(e)!==`w:rPrChange`).map(D);if(r.length!==0)return k(`w:rPr`,r)}function rr(e,t,n){let r=x(e,`w:pPr`);r||(r=k(`w:pPr`,[]),b(e).unshift(r));let i=b(r),a=x(r,`w:rPr`);if(!a){a=k(`w:rPr`,[]);let e=i.findIndex(e=>{let t=v(e);return t===`w:sectPr`||t===`w:pPrChange`});e===-1?i.push(a):i.splice(e,0,a)}let o=k(`w:${t}`,[],{"w:id":U(n),"w:author":n.author,"w:date":n.date});b(a).unshift(o)}function ir(e,t){let n=x(e,`w:pPr`);if(!n)return;let r=x(n,`w:rPr`);if(r)return x(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(`
13
- `),c=[];for(let e of s){let t=[];a&&t.push(D(a));let i=J(e,o,r);n?i.length>0&&t.push(W(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);P(e,s,F(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=j(s[n],r);e.xmlParts.numbering&&I(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=v(r);if(i===`w:r`){let r=n;for(;r<e.length&&v(e[r])===`w:r`;)r++;let i=e.slice(n,r);for(let e of i)Ot(e);e.splice(n,r-n,wt(t,i)),n++;continue}if(i===`w:ins`){if(C(r,`w:author`)===t.author){e.splice(n,1);continue}cr(b(r),t),n++;continue}if(i===`w:sdt`){let e=x(r,`w:sdtContent`);e&&cr(b(e),t),n++;continue}i&&sr.has(i)&&cr(b(r),t),n++}}function lr(e,t){let n=ir(e,`ins`);if(!n||C(n,`w:author`)!==t)return!1;let r=e=>{for(let n of e){let e=v(n);if(e===`w:r`)return!1;if(e===`w:ins`){if(C(n,`w:author`)!==t)return!1;continue}if(e===`w:del`||e===`w:moveFrom`)return!1;if(e===`w:sdt`){let e=x(n,`w:sdtContent`);if(e&&!r(b(e)))return!1;continue}if(e&&sr.has(e)&&!r(b(n)))return!1}return!0};return r(b(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=v(e);return t===`w:p`||i&&t===`w:tbl`}).length===1){let n=b(t.xmlRef),r=x(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 dr(e,t,n,r){if(!r){ur(e,t);return}if(lr(t.xmlRef,r.author)){ur(e,t);return}cr(b(t.xmlRef),r),ir(t.xmlRef,`del`)||rr(t.xmlRef,`del`,r),Y(e,t)}function fr(){return[{"?xml":[{"#text":``}],":@":{"@_version":`1.0`,"@_encoding":`UTF-8`,"@_standalone":`yes`}},k(`Relationships`,[],{xmlns:`http://schemas.openxmlformats.org/package/2006/relationships`})]}function pr(){return[k(`Types`,[k(`Default`,[],{Extension:`rels`,ContentType:`application/vnd.openxmlformats-package.relationships+xml`}),k(`Default`,[],{Extension:`xml`,ContentType:`application/xml`})],{xmlns:`http://schemas.openxmlformats.org/package/2006/content-types`})]}function mr(e){return e.find(e=>v(e)===`Relationships`)}function hr(e){return e.find(e=>v(e)===`Types`)}function gr(e){let t=0,n=mr(e);if(n)for(let e of S(n,`Relationship`)){let n=C(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 S(r,`Relationship`))if(C(e,`Type`)===t&&C(e,`Target`)===n)return C(e,`Id`)??``;let i=gr(e);return b(r).push(k(`Relationship`,[],{Id:i,Type:t,Target:n})),i}function vr(e,t){if(!e)return[];let n=mr(e);if(!n)return[];let r=b(n),i=[];for(let e=r.length-1;e>=0;e--){let n=r[e];v(n)===`Relationship`&&C(n,`Type`)===t&&(i.push(C(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}`;S(r,`Override`).some(e=>C(e,`PartName`)===i)||b(r).push(k(`Override`,[],{PartName:i,ContentType:n}))}function br(e,t){if(!e)return;let n=hr(e);if(!n)return;let r=b(n),i=t.startsWith(`/`)?t:`/${t}`,a=r.findIndex(e=>v(e)===`Override`&&C(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`}},k(`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=>v(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 S(e,`w:comment`)){let e=parseInt(C(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 k(`w:comment`,i.split(`
14
- `).map((e,t)=>{let n=[];if(s&&n.push(k(`w:pPr`,[k(`w:pStyle`,[],{"w:val":`CommentText`})])),t===0){let e=[];c&&e.push(k(`w:rPr`,[k(`w:rStyle`,[],{"w:val":`CommentReference`})])),e.push(k(`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 Tr(e,t){let n=[];return t.styles.some(e=>e.id===`CommentReference`)&&n.push(k(`w:rPr`,[k(`w:rStyle`,[],{"w:val":`CommentReference`})])),n.push(k(`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=K(n.find,r.markers??!0),a=z(t.text,i,e);if(a===-1)throw Error(`Text "${i}" not found in paragraph "${t.id}" (occurrence ${e})`);let s=_t(t.runs,a,a+i.length);if(s.length===0)throw Error(`No runs found for "${i}" in paragraph "${t.id}"`);if(o=H(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=k(`w:commentRangeStart`,[],{"w:id":c}),u=k(`w:commentRangeEnd`,[],{"w:id":c}),d=Tr(c,e);if(o){let e=o[0],n=o[o.length-1],r=B(t.xmlRef,e);if(!r)throw Error(`Matched run not found in paragraph`);r.splice(r.indexOf(e),0,l);let i=B(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=b(t.xmlRef),n=x(t.xmlRef,`w:pPr`),r=n?e.indexOf(n)+1:0;e.splice(r,0,l),e.push(u,d)}b(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?bt(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 Ye(n),n}function jr(e,t){return e.markers??t?.inlineMarkers??!0}function Mr(e,t,n,r){let i=lt(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=L(t.target,e);Ft(r,t,n,a),Y(e,r);break}case`insert_text`:{let r=L(t.target,e);zt(r,t,n,a),Y(e,r);break}case`delete_text`:{let r=L(t.target,e);Ut(r,t,n,a),Y(e,r);break}case`insert_paragraph`:or(e,L(t.target,e),t,n,a);break;case`delete_paragraph`:dr(e,L(t.target,e),t,n);break;case`add_comment`:{let n=L(t.target,e);Er(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=L(t.target,e);Qt(r,t,n,a),Y(e,r);break}case`add_table_row`:{let r=R(t.target,e);P(e,[Xt(r,t,n)],F(e,r.id),!0),Nr(e,r);break}case`delete_table_row`:{let r=R(t.target,e);Zt(r,t,n),Nr(e,r);break}case`apply_formatting`:{let r=L(t.target,e);tn(r,t,n),Y(e,r);break}case`apply_style`:{let r=L(t.target,e);ln(r,t,e,n),Y(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=M(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)I(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=S(e,`w:tr`);for(let e of n){let n=b(e),r=n.find(e=>`w:trPr`in e);r||(r=k(`w:trPr`,[]),n.unshift(r)),b(r).push(Et(t))}}function Ir(e,t,n,r){if(t.container){zr(e,t,n,r);return}let i=Rn(e),a=tt(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=W(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:N(e,t.listType),s=i,c=t.listType;for(let e of t.items){let t=k(`w:pPr`,[k(`w:numPr`,[k(`w:ilvl`,[],{"w:val":String(e.level)}),k(`w:numId`,[],{"w:val":i})])]),a=J(e.text,void 0,r);if(n){let e=W(n,a);o.push({"w:p":[t,e]})}else o.push({"w:p":[t,...a]})}}P(e,o,F(e,`body`)),zn(i,o);let{nextPIdx:l,nextTblIdx:u}=Pr(e);for(let t of o)if(`w:tbl`in t){let n=M(t,`tbl${u++}`);e.body.push(n)}else{let n=j(t,`p${l++}`);e.xmlParts.numbering&&I(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),P(e,[i],F(e,`body`));let a,o;if(t.afterId){let n=gt(t.afterId,e);n&&(a=n.xmlRef,o=n.id)}zn(r,[i],a);let{nextTblIdx:s}=Pr(e),c=M(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=>v(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=b(o),l=i.type===`header`?`hdr`:`ftr`,u=tt(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);if(n){let e=W(n,u);t={"w:p":[e]}}else t={"w:p":u};P(e,[t],o),c.push(t);let f=j(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),P(e,[r],o),c.push(r);let a=s.tables.length,u=M(r,`${l}.${i.variant}.tbl${a}`);s.tables.push(u)}else{let a;a=f&&p===t.listType?f:N(e,t.listType),f=a,p=t.listType;for(let u of t.items){let t=k(`w:pPr`,[k(`w:numPr`,[k(`w:ilvl`,[],{"w:val":String(u.level)}),k(`w:numId`,[],{"w:val":a})])]),f=J(u.text,void 0,r),p;if(n){let e=W(n,f);p={"w:p":[t,e]}}else p={"w:p":[t,...f]};P(e,[p],o),c.push(p);let m=j(p,`${l}.${i.variant}.p${d++}`);e.xmlParts.numbering&&I(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=b(a),c=$t(t.headers,t.rows);n&&Fr(c,n),P(e,[c],a),s.push(c);let l=r.type===`header`?`hdr`:`ftr`,u=o.tables.length,d=M(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(`
15
- `)};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(`
16
- `).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:X(t.startOffset,e.runs),endOffset:X(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=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=$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 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 $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:X(t.startOffset,e.runs),endOffset:X(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,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.moveRangeIds=fi(e,t),Q(e,void 0,t)}function Z(e,t){if(!t.authors)return!0;let n=C(e,`w:author`);return n!==void 0&&t.authors.has(n)}function fi(e,t){let n=new Set,r=e=>{for(let i of e){let e=v(i);if(!e)continue;if(ai.has(e)&&Z(i,t)){let e=C(i,`w:id`);e!==void 0&&n.add(e)}let a=b(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=v(i);if(!a){r++;continue}let o=t!==void 0&&ii.has(t);if((a===`w:ins`||a===`w:moveTo`)&&!o){if(Z(i,n)){n.count++,n.mode===`accept`?e.splice(r,1,...b(i)):e.splice(r,1);continue}Q(b(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=b(i);for(let e of t)pi(e);e.splice(r,1,...t)}continue}Q(b(i),a,n),r++;continue}if(ai.has(a)||oi.has(a)){let t=C(i,`w:id`);if(ai.has(a)?Z(i,n):t!==void 0&&n.moveRangeIds.has(t)){e.splice(r,1);continue}r++;continue}if(a in ri){if(Z(i,n)){n.count++,n.mode===`accept`?e.splice(r,1):hi(e,i,a),r=0;continue}r++;continue}if(a===`w:p`){if(Q(b(i),a,n),gi(e,r,i,n))continue;r++;continue}if(a===`w:tr`){if(vi(e,r,i,n))continue;Q(b(i),a,n),r++;continue}if(a===`w:tc`){if(yi(e,r,i,n))continue;Q(b(i),a,n),r++;continue}let s=b(i);s.length>0&&Q(s,a,n),r++}}function pi(e){let t=v(e);if(t){if(t===`w:r`){let t=b(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[`:@`]),A(mi(r[`w:t`]))&&w(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 b(e))pi(t)}}function mi(e){let t=``;for(let n of e)`#text`in n&&(t+=E(String(n[`#text`])));return t}function hi(e,t,n){let r=ri[n]??new Set,i=b(t).find(e=>v(e)!==void 0),a=i?b(i).map(D):[],o=e.filter(e=>{let n=v(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 gi(e,t,n,r){let i=x(n,`w:pPr`);if(!i)return!1;let a=x(i,`w:rPr`);if(!a)return!1;let o=b(a),s=!1,c=o.findIndex(e=>v(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=>v(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=b(i);if(e.splice(e.indexOf(a),1),e.length===0){let e=b(n);e.splice(e.indexOf(i),1)}}return s?_i(e,t,n):!1}function _i(e,t,n){let r=x(n,`w:pPr`);if(r&&x(r,`w:sectPr`))return!1;let i=t+1;for(;i<e.length;){let t=v(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=b(a),s=x(a,`w:pPr`),c=s?o.indexOf(s)+1:0,l=b(n).filter(e=>v(e)!==`w:pPr`);return o.splice(c,0,...l),e.splice(t,1),!0}function vi(e,t,n,r){let i=x(n,`w:trPr`);if(!i)return!1;let a=b(i),o=a.findIndex(e=>v(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=>v(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 yi(e,t,n,r){let i=x(n,`w:tcPr`);if(!i)return!1;let a=b(i),o=a.findIndex(e=>v(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=>v(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=>v(e)===`w:cellMerge`);return c&&Z(c,r)&&(r.count++,r.mode===`reject`&&bi(a,c),a.splice(a.indexOf(c),1)),!1}function bi(e,t){let n=C(t,`w:vMergeOrig`),r=e.findIndex(e=>v(e)===`w:vMerge`);if(n===void 0){r!==-1&&e.splice(r,1);return}let i={"w:vMerge":[]};if(n===`rest`&&w(i,`w:val`,`restart`),r!==-1)e[r]=i;else{let t=e.findIndex(e=>{let t=v(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 xi=`http://schemas.openxmlformats.org/package/2006/metadata/core-properties`,Si=`http://purl.org/dc/elements/1.1/`,Ci=`http://purl.org/dc/terms/`,wi=`http://purl.org/dc/dcmitype/`,Ti=`http://www.w3.org/2001/XMLSchema-instance`,Ei={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=v(e);return n!==void 0&&$(n)===t})}function Ai(e,t){return b(e).find(e=>{let n=v(e);return n!==void 0&&$(n)===t})}function ji(e){let t=``;for(let n of b(e))`#text`in n&&(t+=E(String(n[`#text`])));return t}function Mi(e,t){let n=v(e);e[n]=t.length>0?[{"#text":T(t)}]:[]}function Ni(e,t){let n=b(e),r=n.findIndex(e=>{let n=v(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`}},k(`cp:coreProperties`,[],{"xmlns:cp":xi,"xmlns:dc":Si,"xmlns:dcterms":Ci,"xmlns:dcmitype":wi,"xmlns:xsi":Ti})]}const Fi={"xmlns:cp":xi,"xmlns:dc":Si,"xmlns:dcterms":Ci,"xmlns:dcmitype":wi,"xmlns:xsi":Ti};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))C(t,e)===void 0&&w(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(Ei)){let r=Ai(n,$(Ei[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 Ei))continue;let i=Ei[e],a=Ai(n,$(i));a||(a=k(i,[]),b(n).push(a)),Mi(a,r),(e===`created`||e===`modified`)&&w(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,$(Ei[e]));Ri(e,{revision:`1`})}let r=ki(e.xmlParts.app,`Properties`);if(r){let e=b(r);for(let t=e.length-1;t>=0;t--){let n=v(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 e{zipEntries;doc;removedParts=new Set;constructor(e,t){this.zipEntries=e,this.doc=t}static async load(t,n){let r;r=typeof t==`string`?await(0,c.readFile)(t):t;let i=await d(r);return new e(i,Je(i,n))}static async create(t,n){if(t!==void 0)return e.load(t,n);let r=new l.default;r.file(`[Content_Types].xml`,`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
12
+ `);for(let e=0;e<t.length;e++){let r=t[e];if(r.length>0||e===0){let e={"w:t":[{"#text":w(r)}]};k(r)&&C(e,`xml:space`,`preserve`),n.push(e)}e<t.length-1&&n.push({"w:br":[]})}}else{let t={"w:t":[{"#text":w(e)}]};k(e)&&C(t,`xml:space`,`preserve`),n.push(t)}return{"w:r":n}}function Zt(e,t,n,r=!0){n?Qt(e,t,n,r):Vt(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=K(t.find,r),o=R(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=vt(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=[B(e.xmlRef,t).matched]}else l=V(e.xmlRef,c);if(l.length===0)throw Error(`Failed to isolate matched runs in paragraph "${e.id}"`);Wt(e.xmlRef,l,n)}function $t(e,t){let n=[];for(let r of e){if(r.text.length===0)continue;let e=Xt(r.text,t);if(r.bold||r.italic||r.strikethrough||r.underline){let t=b(e,`w:rPr`);t||(t=O(`w:rPr`,[]),y(e).unshift(t));let n=y(t);r.bold&&!b(t,`w:b`)&&n.push(O(`w:b`,[])),r.italic&&!b(t,`w:i`)&&n.push(O(`w:i`,[])),r.strikethrough&&!b(t,`w:strike`)&&n.push(O(`w:strike`,[])),r.underline&&!b(t,`w:u`)&&n.push(O(`w:u`,[],{"w:val":`single`}))}n.push(e)}return n}function en(e,t,n){let r=bt(t),i=$t(n,r.rPr);if(i.length===0){tn(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=y(i[0]),t=+(e.length>0&&`w:rPr`in e[0]);e.splice(t,0,...a)}o.length>0&&y(i[i.length-1]).push(...o);let s=z(e,t);if(s){let e=s.indexOf(t);if(e!==-1){s.splice(e,1,...i);return}}tn(t,n.map(e=>e.text).join(``))}function tn(e,t,n){let r=bt(e),i=oe(e);if(i!==void 0&&r.anchored.length===0&&!t.includes(`
13
+ `)&&!r.text.includes(`
14
+ `)&&y(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?ie(n,t):e.push({"#text":w(t)})}k(t)&&C(i,`xml:space`,`preserve`);return}let a=n??(e=>e<=0?0:t.length);xt(e,{rPr:r.rPr,text:t,anchored:r.anchored.map(e=>({node:e.node,offset:a(e.offset)}))})}function nn(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=E(i.xmlRef),o=x(a,`w:tc`);for(let e=0;e<o.length;e++){let n=o[e],r=t.cells[e],i=b(n,`w:p`);if(i){let e=y(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":w(r)}]};k(r)&&(n[`:@`]||={},n[`:@`][`@_xml:space`]=`preserve`);let a={"w:r":[n]};e.push(a)}}let s=y(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 de(e.xmlRef,i.xmlRef,a);if(n){let e=y(a),t=e.find(e=>`w:trPr`in e);t||(t=O(`w:trPr`,[]),e.unshift(t)),y(t).push(Mt(n))}return a}function rn(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=y(r.xmlRef),t=e.find(e=>`w:trPr`in e);t||(t=O(`w:trPr`,[]),e.unshift(t)),y(t).push(Nt(n))}else D(e.xmlRef,r.xmlRef)}function an(e,t,n,r=!0){Vt(e,{type:`replace_text`,target:t.target,find:t.find,replace:t.replace,occurrence:t.occurrence},n,r)}function on(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=O(`w:tblBorders`,[O(`w:top`,[],r),O(`w:left`,[],r),O(`w:bottom`,[],r),O(`w:right`,[],r),O(`w:insideH`,[],r),O(`w:insideV`,[],r)]),a=O(`w:tblPr`,[O(`w:tblStyle`,[],{"w:val":`TableGrid`}),O(`w:tblW`,[],{"w:w":`0`,"w:type":`auto`}),i,O(`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(O(`w:gridCol`,[]));let s=O(`w:tblGrid`,o),c=[];e&&c.push(sn(e));for(let e of t)c.push(sn(e));return O(`w:tbl`,[a,s,...c])}function sn(e){let t=[];for(let n of e){let e={"w:t":[{"#text":w(n)}]};k(n)&&(e[`:@`]||={},e[`:@`][`@_xml:space`]=`preserve`);let r={"w:p":[{"w:r":[e]}]},i=O(`w:tc`,[O(`w:tcPr`,[O(`w:tcW`,[],{"w:w":`0`,"w:type":`auto`})]),r]);t.push(i)}return O(`w:tr`,t)}function cn(e,t,n){t.find!==void 0&&t.find.length>0?ln(e,t.find,t.occurrence??1,t.formatting,n):un(e,t.formatting,n)}function ln(e,t,n,r,i){let a=R(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=vt(e.runs,a,o);if(s.length===0)throw Error(`No runs found for text range [${a}, ${o}] in paragraph "${e.id}"`);let c=V(e.xmlRef,s);for(let e of c)dn(e,r,i)}function un(e,t,n){for(let r of e.runs)dn(r.xmlRef,t,n)}function dn(e,t,n){let r=Object.entries(t).some(([,e])=>e!==null&&e!==!1&&e!==void 0),i=b(e,`w:rPr`);if(!i&&!r)return;let a=n&&i?E(i):void 0;if(i||(i=O(`w:rPr`),y(e).unshift(i)),t.bold!==void 0&&fn(i,`w:b`,t.bold),t.italic!==void 0&&fn(i,`w:i`,t.italic),t.strikethrough!==void 0&&fn(i,`w:strike`,t.strikethrough),t.underline!==void 0&&pn(i,`w:u`,`w:val`,t.underline),t.fontSize!==void 0&&pn(i,`w:sz`,`w:val`,t.fontSize===null?null:String(t.fontSize)),t.fontFamily!==void 0&&mn(i,t.fontFamily),t.color!==void 0&&pn(i,`w:color`,`w:val`,t.color),t.highlight!==void 0&&pn(i,`w:highlight`,`w:val`,t.highlight),n){let e=b(i,`w:rPrChange`);e&&D(i,e);let t=jt(n,a??O(`w:rPr`));y(i).push(t)}}function fn(e,t,n){let r=b(e,t);n===!0?r||y(e).push(O(t)):r&&D(e,r)}function pn(e,t,n,r){let i=b(e,t);if(r!==null)if(i)C(i,n,r);else{let i=O(t,[],{[n]:r});y(e).push(i)}else i&&D(e,i)}function mn(e,t){let n=b(e,`w:rFonts`);if(t!==null)if(n)C(n,`w:ascii`,t),C(n,`w:hAnsi`,t);else{let n=O(`w:rFonts`,[],{"w:ascii":t,"w:hAnsi":t});y(e).push(n)}else n&&(ne(n,`w:ascii`),ne(n,`w:hAnsi`),Object.keys(re(n)).length===0&&D(e,n))}function hn(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`);_n(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`);gn(e,t.style,r)}}function gn(e,t,n){let r=e.xmlRef,i=b(r,`w:pPr`),a=n&&i?E(i):void 0;i||(i=O(`w:pPr`),y(r).unshift(i));let o=b(i,`w:pStyle`);if(o?C(o,`w:val`,t):(o=O(`w:pStyle`,[],{"w:val":t}),y(i).unshift(o)),n){let e=b(i,`w:pPrChange`);e&&D(i,e);let t=G(n,a??O(`w:pPr`));y(i).push(t)}}function _n(e,t,n,r,i){let a=R(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=vt(e.runs,a,o);if(s.length===0)throw Error(`No runs found for text range [${a}, ${o}] in paragraph "${e.id}"`);let c=V(e.xmlRef,s);for(let e of c)vn(e,r,i)}function vn(e,t,n){let r=b(e,`w:rPr`),i=n&&r?E(r):void 0;r||(r=O(`w:rPr`),y(e).unshift(r));let a=b(r,`w:rStyle`);a&&D(r,a);let o=O(`w:rStyle`,[],{"w:val":t});if(y(r).unshift(o),n){let e=b(r,`w:rPrChange`);e&&D(r,e);let t=jt(n,i??O(`w:rPr`));y(r).push(t)}}const yn=new Set([`PAGE`,`NUMPAGES`,`DATE`,`TIME`,`FILENAME`]);function bn(e){let t=[],n=/\{\{(\w+)\}\}/g,r=0,i;for(;(i=n.exec(e))!==null;){let n=i[1];yn.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 xn(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=bn(e);if(r.length===1&&r[0].kind===`text`)return Sn(q(r[0].text,n),t);let i=r.filter(e=>e.kind===`field`).map(e=>e.fieldCode),a=xn(e),o=q(r.map(e=>e.kind===`text`?e.text:a).join(``),n),s=[],c=[],l=0,u=()=>{c.length>0&&(s.push(...Sn(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=wn(t,{bold:!!e.bold,italic:!!e.italic,strikethrough:!!e.strikethrough,underline:!!e.underline});s.push(...Cn(i[l++],n))}}}return u(),s}function Sn(e,t){if(e.some(e=>e.bold||e.italic||e.strikethrough||e.underline))return $t(e,t);let n=e.map(e=>e.text).join(``);return n.length>0?[me(n,t)]:[]}function Cn(e,t){let n=()=>t?[JSON.parse(JSON.stringify(t))]:[],r={"w:r":[...n(),O(`w:fldChar`,[],{"w:fldCharType":`begin`})]},i={"w:instrText":[{"#text":` ${e} `}]};return C(i,`xml:space`,`preserve`),[r,{"w:r":[...n(),i]},{"w:r":[...n(),O(`w:fldChar`,[],{"w:fldCharType":`separate`})]},{"w:r":[...n(),O(`w:fldChar`,[],{"w:fldCharType":`end`})]}]}function wn(e,t){let n=t.bold||t.italic||t.strikethrough||t.underline;if(!e&&!n)return;let r=e?JSON.parse(JSON.stringify(e)):O(`w:rPr`,[]);if(!n)return r;let i=y(r);return t.bold&&!i.some(e=>`w:b`in e)&&i.push(O(`w:b`,[])),t.italic&&!i.some(e=>`w:i`in e)&&i.push(O(`w:i`,[])),t.strikethrough&&!i.some(e=>`w:strike`in e)&&i.push(O(`w:strike`,[])),t.underline&&!i.some(e=>`w:u`in e)&&i.push(O(`w:u`,[],{"w:val":`single`})),r}const Tn={default:`default`,first:`first`,even:`even`};function En(e,t,n,r=!0){An(e,`header`,t.variant,t.paragraphs,n,r)}function Dn(e,t,n,r=!0){An(e,`footer`,t.variant,t.paragraphs,n,r)}function On(e,t){jn(e,`header`,t.variant)}function kn(e,t){jn(e,`footer`,t.variant)}function An(e,t,n,r,i,a=!0){let o=t===`header`?e.xmlParts.headers:e.xmlParts.footers,s=`${t}${Mn(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=W(i,t.map(e=>e));u.push({"w:p":[e]})}else u.push({"w:p":t})}let d=[O(l,u,{"xmlns:w":`http://schemas.openxmlformats.org/wordprocessingml/2006/main`,"xmlns:r":`http://schemas.openxmlformats.org/officeDocument/2006/relationships`})],f=Nn(e);Pn(e,f,s,t),Ln(e,c,t),Bn(e,t,n,f),o.set(c,d);let p=ke(d,t,n,f);t===`header`?e.headers.set(f,p):e.footers.set(f,p)}function jn(e,t,n){let r=t===`header`?`w:headerReference`:`w:footerReference`,i=zn(e);if(!i)throw Error(`No sectPr found in document`);let a=y(i),o=a.find(e=>_(e)===r?(S(e,`w:type`)??`default`)===Tn[n]:!1);if(!o)throw Error(`No ${t} with variant "${n}" found in sectPr`);let s=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=Fn(e,s),u=l?`word/${l}`:void 0;In(e,s),u&&Rn(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 Mn(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 Nn(e){let t=e.xmlParts.documentRels;if(!t)return`rId1`;let n=0,r=t.find(e=>_(e)===`Relationships`);if(r){let e=x(r,`Relationship`);for(let t of e){let e=S(t,`Id`);if(e){let t=parseInt(e.replace(/^rId/,``),10);!isNaN(t)&&t>n&&(n=t)}}}return`rId${n+1}`}function Pn(e,t,n,r){e.xmlParts.documentRels||(e.xmlParts.documentRels=[O(`Relationships`,[],{xmlns:`http://schemas.openxmlformats.org/package/2006/relationships`})]);let i=O(`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=>_(e)===`Relationships`);a&&y(a).push(i)}function Fn(e,t){let n=e.xmlParts.documentRels;if(!n)return;let r=n.find(e=>_(e)===`Relationships`);if(!r)return;let i=x(r,`Relationship`);for(let e of i)if(S(e,`Id`)===t)return S(e,`Target`)}function In(e,t){let n=e.xmlParts.documentRels;if(!n)return;let r=n.find(e=>_(e)===`Relationships`);if(!r)return;let i=y(r),a=i.findIndex(e=>_(e)===`Relationship`&&S(e,`Id`)===t);a!==-1&&i.splice(a,1)}function Ln(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=O(`Override`,[],{PartName:`/${t}`,ContentType:r}),a=e.xmlParts.contentTypes.find(e=>_(e)===`Types`);a&&y(a).push(i)}function Rn(e,t){if(!e.xmlParts.contentTypes)return;let n=e.xmlParts.contentTypes.find(e=>_(e)===`Types`);if(!n)return;let r=y(n),i=r.findIndex(e=>{if(_(e)!==`Override`)return!1;let n=S(e,`PartName`);return n===`/${t}`||n===t});i!==-1&&r.splice(i,1)}function zn(e){let t=e.xmlParts.document.find(e=>_(e)===`w:document`);if(!t)return;let n=b(t,`w:body`);if(!n)return;let r=y(n);for(let e=r.length-1;e>=0;e--)if(_(r[e])===`w:sectPr`)return r[e]}function Bn(e,t,n,r){let i=zn(e);if(!i){i=O(`w:sectPr`,[]);let t=e.xmlParts.document.find(e=>_(e)===`w:document`);if(!t)throw Error(`Cannot find w:document`);let n=b(t,`w:body`);if(!n)throw Error(`Cannot find w:body`);y(n).push(i)}let a=O(t===`header`?`w:headerReference`:`w:footerReference`,[],{"w:type":Tn[n],"r:id":r});if(y(i).unshift(a),n===`first`){let e=y(i);e.some(e=>_(e)===`w:titlePg`)||e.push(O(`w:titlePg`,[]))}n===`even`&&Vn(e)}function Vn(e){e.xmlParts.settings||(e.xmlParts.settings=[O(`w:settings`,[O(`w:evenAndOddHeaders`,[])],{"xmlns:w":`http://schemas.openxmlformats.org/wordprocessingml/2006/main`})]);let t=e.xmlParts.settings.find(e=>_(e)===`w:settings`);if(!t)return;let n=y(t);n.some(e=>_(e)===`w:evenAndOddHeaders`)||n.push(O(`w:evenAndOddHeaders`,[])),Hn(e),Un(e)}function Hn(e){e.xmlParts.documentRels||(e.xmlParts.documentRels=[O(`Relationships`,[],{xmlns:`http://schemas.openxmlformats.org/package/2006/relationships`})]);let t=e.xmlParts.documentRels.find(e=>_(e)===`Relationships`);if(!t)return;let n=`http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings`,r=y(t);r.some(e=>_(e)===`Relationship`?S(e,`Type`)===n&&S(e,`Target`)===`settings.xml`:!1)||r.push(O(`Relationship`,[],{Id:Nn(e),Type:n,Target:`settings.xml`}))}function Un(e){e.xmlParts.contentTypes||(e.xmlParts.contentTypes=[O(`Types`,[],{xmlns:`http://schemas.openxmlformats.org/package/2006/content-types`})]);let t=e.xmlParts.contentTypes.find(e=>_(e)===`Types`);if(!t)return;let n=`/word/settings.xml`,r=y(t);r.some(e=>_(e)===`Override`?S(e,`PartName`)===n:!1)||r.push(O(`Override`,[],{PartName:n,ContentType:`application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml`}))}function Wn(e){let t=e.xmlParts.document.find(e=>_(e)===`w:document`);if(!t)throw Error(`Cannot find w:document in FXP tree`);let n=b(t,`w:body`);if(!n)throw Error(`Cannot find w:body in FXP tree`);return y(n)}function Gn(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=>_(e)===`w:sectPr`);r===-1?e.push(...t):e.splice(r,0,...t)}function Kn(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 qn(e,t){let n=Wn(e);if(n.includes(t))return n;for(let e of n)if(_(e)===`w:tbl`){let n=Jn(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=>_(t)===e);if(r){let e=y(r);if(e.includes(t))return e;for(let n of e)if(_(n)===`w:tbl`){let e=Jn(n,t);if(e)return e}}}}function Jn(e,t){for(let n of y(e).filter(e=>_(e)===`w:tr`))for(let e of y(n).filter(e=>_(e)===`w:tc`)){let n=y(e);if(n.includes(t))return n;for(let e of n)if(_(e)===`w:tbl`){let n=Jn(e,t);if(n)return n}}}function Yn(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=Xn(e,t);if(n)return n}}function Xn(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=Xn(n,t);if(e)return e}}}function Zn(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=A(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&&F(t,e.xmlParts.numbering)}function Qn(e,t,n,r,i=!0){let a=O(`w:pPr`,[O(`w:numPr`,[O(`w:ilvl`,[],{"w:val":String(n)}),O(`w:numId`,[],{"w:val":t})])]),o=J(e,void 0,i),s;return s=r?[a,W(r,o)]:[a,...o],{"w:p":s}}function $n(e,t,n,r=!0){let i=t.numId??Fe(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(Qn(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=>_(e)===n);if(i){let n=y(i);M(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=A(n,`${a}.${t.container.variant}.p${s++}`);e.xmlParts.numbering&&F(i,e.xmlParts.numbering),r.paragraphs.push(i)}}return}return}let s=Wn(e),c,l;if(t.afterId){let n=_t(t.afterId,e);n&&(c=n.xmlRef,l=n.id)}M(e,o,N(e,`body`)),Gn(s,o,c);let u=Kn(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=A(o[t],n);e.xmlParts.numbering&&F(r,e.xmlParts.numbering),d===-1?e.body.push(r):e.body.splice(d+1+t,0,r)}}function er(e,t,n,r=!0){let i=I(t.target,e),a=i.listInfo.numId,o=t.level??i.listInfo.level,s=Qn(t.text,a,o,n,r);M(e,[s],N(e,i.id));let c=qn(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=Yn(e,i.id),d=u?u.array:e.body,f=A(s,Zn(i.id,d));e.xmlParts.numbering&&F(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 tr(e,t,n){let r=I(t.target,e);if(n){let t=y(r.xmlRef),i=b(r.xmlRef,`w:pPr`);if(i){let e=G(n,i);y(i).push(e)}for(let e=t.length-1;e>=0;e--){let r=t[e],i=_(r);if(i===`w:r`){let i=E(r);Pt(i),t[e]=At(n,[i])}else i===`w:ins`&&t.splice(e,1)}Y(e,r);return}let i=qn(e,r.xmlRef);if(i){let e=i.indexOf(r.xmlRef);e!==-1&&i.splice(e,1)}let a=Yn(e,r.id);a&&a.array.splice(a.index,1)}function nr(e,t,n){let r=Fe(e,t.listType),i=t.level??0;for(let a of t.targets){let t=I(a,e),o=t.xmlRef,s=b(o,`w:pPr`),c=y(o);if(n&&s){let e=G(n,s);y(s).push(e)}s||(s=O(`w:pPr`,[]),c.unshift(s));let l=y(s),u=l.findIndex(e=>_(e)===`w:numPr`);u!==-1&&l.splice(u,1);let d=O(`w:numPr`,[O(`w:ilvl`,[],{"w:val":String(i)}),O(`w:numId`,[],{"w:val":r})]);l.unshift(d),Y(e,t)}}function rr(e,t,n){for(let r of t.targets){let t=I(r,e),i=b(t.xmlRef,`w:pPr`);if(!i)continue;if(n){let e=G(n,i);y(i).push(e)}let a=y(i),o=a.findIndex(e=>_(e)===`w:numPr`);o!==-1&&a.splice(o,1),Y(e,t)}}function ir(e,t,n){let r=I(t.target,e),i=b(r.xmlRef,`w:pPr`);if(!i)throw Error(`No w:pPr found on target paragraph`);if(n){let e=G(n,i);y(i).push(e)}let a=b(i,`w:numPr`);if(!a)throw Error(`No w:numPr found on target paragraph`);if(t.level!==void 0){let e=b(a,`w:ilvl`);e?C(e,`w:val`,String(t.level)):y(a).unshift(O(`w:ilvl`,[],{"w:val":String(t.level)}))}if(t.listType!==void 0){let n=Fe(e,t.listType),r=b(a,`w:numId`);r?C(r,`w:val`,n):y(a).push(O(`w:numId`,[],{"w:val":n}))}Y(e,r)}const ar=new Set([`w:sectPr`,`w:pPrChange`,`w:rPr`]),or=new Set([`w:ins`,`w:del`,`w:moveFrom`,`w:moveTo`,`w:rPrChange`]);function sr(e){let t=b(e,`w:pPr`);if(!t)return;let n=[],r;for(let e of y(t)){let t=_(e);if(t){if(t===`w:rPr`){let t=y(e).filter(e=>{let t=_(e);return t!==void 0&&!or.has(t)}).map(E);t.length>0&&(r=O(`w:rPr`,t));continue}ar.has(t)||n.push(E(e))}}if(r&&n.push(r),n.length!==0)return O(`w:pPr`,n)}function cr(e){let t=e.runs[e.runs.length-1];if(!t)return;let n=b(t.xmlRef,`w:rPr`);if(!n)return;let r=y(n).filter(e=>_(e)!==`w:rPrChange`).map(E);if(r.length!==0)return O(`w:rPr`,r)}function lr(e,t,n){let r=b(e,`w:pPr`);r||(r=O(`w:pPr`,[]),y(e).unshift(r));let i=y(r),a=b(r,`w:rPr`);if(!a){a=O(`w:rPr`,[]);let e=i.findIndex(e=>{let t=_(e);return t===`w:sectPr`||t===`w:pPrChange`});e===-1?i.push(a):i.splice(e,0,a)}let o=O(`w:${t}`,[],{"w:id":H(n),"w:author":n.author,"w:date":n.date});y(a).unshift(o)}function ur(e,t){let n=b(e,`w:pPr`);if(!n)return;let r=b(n,`w:rPr`);if(r)return b(r,`w:${t}`)}function dr(e,t,n,r){let i=t.inheritStyle??!0,a=i?sr(e.xmlRef):void 0,o=i?cr(e):void 0,s=t.text.split(`
15
+ `),c=[];for(let e of s){let t=[];a&&t.push(E(a));let i=J(e,o,r);n?i.length>0&&t.push(W(n,i)):t.push(...i);let s={"w:p":t};n&&lr(s,`ins`,n),c.push(s)}return c}function fr(e,t,n,r,i=!0){let a=qn(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=dr(t,n,r,i);M(e,s,N(e,t.id));let c=n.position===`after`?o+1:o;a.splice(c,0,...s);let l=Yn(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=Zn(t.id,l.array),i=A(s[n],r);e.xmlParts.numbering&&F(i,e.xmlParts.numbering),l.array.splice(u+n,0,i),d.push(i)}return d}const pr=new Set([`w:hyperlink`,`w:smartTag`,`w:customXml`,`w:dir`,`w:bdo`,`w:moveTo`,`w:sdtContent`,`w:fldSimple`]);function mr(e,t){let n=0;for(;n<e.length;){let r=e[n],i=_(r);if(i===`w:r`){let r=n;for(;r<e.length&&_(e[r])===`w:r`;)r++;let i=e.slice(n,r);for(let e of i)Pt(e);e.splice(n,r-n,At(t,i)),n++;continue}if(i===`w:ins`){if(S(r,`w:author`)===t.author){e.splice(n,1);continue}mr(y(r),t),n++;continue}if(i===`w:sdt`){let e=b(r,`w:sdtContent`);e&&mr(y(e),t),n++;continue}i&&pr.has(i)&&mr(y(r),t),n++}}function hr(e,t){let n=ur(e,`ins`);if(!n||S(n,`w:author`)!==t)return!1;let r=e=>{for(let n of e){let e=_(n);if(e===`w:r`)return!1;if(e===`w:ins`){if(S(n,`w:author`)!==t)return!1;continue}if(e===`w:del`||e===`w:moveFrom`)return!1;if(e===`w:sdt`){let e=b(n,`w:sdtContent`);if(e&&!r(y(e)))return!1;continue}if(e&&pr.has(e)&&!r(y(n)))return!1}return!0};return r(y(e))}function gr(e,t){let n=qn(e,t.xmlRef);if(!n)throw Error(`Cannot find parent container for paragraph "${t.id}"`);let r=Yn(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=_(e);return t===`w:p`||i&&t===`w:tbl`}).length===1){let n=y(t.xmlRef),r=b(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 _r(e,t,n,r){if(!r){gr(e,t);return}if(hr(t.xmlRef,r.author)){gr(e,t);return}mr(y(t.xmlRef),r),ur(t.xmlRef,`del`)||lr(t.xmlRef,`del`,r),Y(e,t)}function vr(){return[{"?xml":[{"#text":``}],":@":{"@_version":`1.0`,"@_encoding":`UTF-8`,"@_standalone":`yes`}},O(`Relationships`,[],{xmlns:`http://schemas.openxmlformats.org/package/2006/relationships`})]}function yr(){return[O(`Types`,[O(`Default`,[],{Extension:`rels`,ContentType:`application/vnd.openxmlformats-package.relationships+xml`}),O(`Default`,[],{Extension:`xml`,ContentType:`application/xml`})],{xmlns:`http://schemas.openxmlformats.org/package/2006/content-types`})]}function br(e){return e.find(e=>_(e)===`Relationships`)}function xr(e){return e.find(e=>_(e)===`Types`)}function Sr(e){let t=0,n=br(e);if(n)for(let e of x(n,`Relationship`)){let n=S(e,`Id`);if(n){let e=parseInt(n.replace(/^rId/,``),10);!isNaN(e)&&e>t&&(t=e)}}return`rId${t+1}`}function Cr(e,t,n){let r=br(e);if(!r)throw Error(`Relationships root not found`);for(let e of x(r,`Relationship`))if(S(e,`Type`)===t&&S(e,`Target`)===n)return S(e,`Id`)??``;let i=Sr(e);return y(r).push(O(`Relationship`,[],{Id:i,Type:t,Target:n})),i}function wr(e,t){if(!e)return[];let n=br(e);if(!n)return[];let r=y(n),i=[];for(let e=r.length-1;e>=0;e--){let n=r[e];_(n)===`Relationship`&&S(n,`Type`)===t&&(i.push(S(n,`Target`)??``),r.splice(e,1))}return i}function Tr(e,t,n){let r=xr(e);if(!r)return;let i=t.startsWith(`/`)?t:`/${t}`;x(r,`Override`).some(e=>S(e,`PartName`)===i)||y(r).push(O(`Override`,[],{PartName:i,ContentType:n}))}function Er(e,t){if(!e)return;let n=xr(e);if(!n)return;let r=y(n),i=t.startsWith(`/`)?t:`/${t}`,a=r.findIndex(e=>_(e)===`Override`&&S(e,`PartName`)===i);a!==-1&&r.splice(a,1)}function Dr(e){e.xmlParts.comments||(e.xmlParts.comments=[{"?xml":[{"#text":``}],":@":{"@_version":`1.0`,"@_encoding":`UTF-8`,"@_standalone":`yes`}},O(`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=vr()),Cr(e.xmlParts.documentRels,`http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments`,`comments.xml`),e.xmlParts.contentTypes||(e.xmlParts.contentTypes=yr()),Tr(e.xmlParts.contentTypes,`/word/comments.xml`,`application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml`));let t=e.xmlParts.comments.find(e=>_(e)===`w:comments`);if(!t)throw Error(`word/comments.xml has no w:comments root`);return t}function Or(e){let t=-1;for(let n of x(e,`w:comment`)){let e=parseInt(S(n,`w:id`)??``,10);!isNaN(e)&&e>t&&(t=e)}return String(t+1)}function kr(e){return e.split(/\s+/).filter(e=>e.length>0).map(e=>e[0].toUpperCase()).join(``).slice(0,3)||`A`}function Ar(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 O(`w:comment`,i.split(`
16
+ `).map((e,t)=>{let n=[];if(s&&n.push(O(`w:pPr`,[O(`w:pStyle`,[],{"w:val":`CommentText`})])),t===0){let e=[];c&&e.push(O(`w:rPr`,[O(`w:rStyle`,[],{"w:val":`CommentReference`})])),e.push(O(`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 jr(e,t){let n=[];return t.styles.some(e=>e.id===`CommentReference`)&&n.push(O(`w:rPr`,[O(`w:rStyle`,[],{"w:val":`CommentReference`})])),n.push(O(`w:commentReference`,[],{"w:id":e})),{"w:r":n}}function Mr(e,t,n,r){let i=n.author&&n.author.length>0?n.author:r.defaultAuthor,a=n.initials??kr(i),o;if(n.find!==void 0&&n.find.length>0){let e=n.occurrence??1,i=K(n.find,r.markers??!0),a=R(t.text,i,e);if(a===-1)throw Error(`Text "${i}" not found in paragraph "${t.id}" (occurrence ${e})`);let s=vt(t.runs,a,a+i.length);if(s.length===0)throw Error(`No runs found for "${i}" in paragraph "${t.id}"`);if(o=V(t.xmlRef,s),o.length===0)throw Error(`Failed to isolate matched runs in paragraph "${t.id}"`)}let s=Dr(e),c=Or(s),l=O(`w:commentRangeStart`,[],{"w:id":c}),u=O(`w:commentRangeEnd`,[],{"w:id":c}),d=jr(c,e);if(o){let e=o[0],n=o[o.length-1],r=z(t.xmlRef,e);if(!r)throw Error(`Matched run not found in paragraph`);r.splice(r.indexOf(e),0,l);let i=z(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=y(t.xmlRef),n=b(t.xmlRef,`w:pPr`),r=n?e.indexOf(n)+1:0;e.splice(r,0,l),e.push(u,d)}y(s).push(Ar(c,i,a,r.date,n.text,e,r.markers??!0))}function Nr(e,t,n){return n?.atomic?Fr(e,t,n):Pr(e,t,n)}function Pr(e,t,n){let r=[],i=n?.trackRevisions?Dt(e,n):void 0;for(let a of t){let t=Rr(e,a,i,n);r.push(t)}return r}function Fr(e,t,n){let r=Ir(e),i=Pr(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 Ir(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 P(n),n}function Lr(e,t){return e.markers??t?.inlineMarkers??!0}function Rr(e,t,n,r){let i=ut(t,e);if(!i.valid)return{success:!1,patch:t,error:i.error};let a=Lr(t,r);try{switch(t.type){case`replace_text`:{let r=I(t.target,e);Vt(r,t,n,a),Y(e,r);break}case`insert_text`:{let r=I(t.target,e);Kt(r,t,n,a),Y(e,r);break}case`delete_text`:{let r=I(t.target,e);Zt(r,t,n,a),Y(e,r);break}case`insert_paragraph`:fr(e,I(t.target,e),t,n,a);break;case`delete_paragraph`:_r(e,I(t.target,e),t,n);break;case`add_comment`:{let n=I(t.target,e);Mr(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=I(t.target,e);an(r,t,n,a),Y(e,r);break}case`add_table_row`:{let r=L(t.target,e);M(e,[nn(r,t,n)],N(e,r.id),!0),zr(e,r);break}case`delete_table_row`:{let r=L(t.target,e);rn(r,t,n),zr(e,r);break}case`apply_formatting`:{let r=I(t.target,e);cn(r,t,n),Y(e,r);break}case`apply_style`:{let r=I(t.target,e);hn(r,t,e,n),Y(e,r);break}case`append_text`:Hr(e,t,n,a);break;case`add_table`:Ur(e,t,n);break;case`add_header`:En(e,t,n,a);break;case`add_footer`:Dn(e,t,n,a);break;case`remove_header`:On(e,t);break;case`remove_footer`:kn(e,t);break;case`add_list`:$n(e,t,n,a);break;case`add_list_item`:er(e,t,n,a);break;case`remove_list_item`:tr(e,t,n);break;case`convert_to_list`:nr(e,t,n);break;case`remove_list`:rr(e,t,n);break;case`set_list_properties`:ir(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 zr(e,t){let n=j(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)F(n,e.xmlParts.numbering)}function Br(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 Vr(e,t){let n=x(e,`w:tr`);for(let e of n){let n=y(e),r=n.find(e=>`w:trPr`in e);r||(r=O(`w:trPr`,[]),n.unshift(r)),y(r).push(Mt(t))}}function Hr(e,t,n,r){if(t.container){Gr(e,t,n,r);return}let i=Wn(e),a=nt(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=W(n,t);o.push({"w:p":[e]})}else o.push({"w:p":t})}}else if(t.kind===`table`){let e=on(t.headers,t.rows);n&&Vr(e,n),o.push(e)}else{let i;i=s&&c===t.listType?s:Fe(e,t.listType),s=i,c=t.listType;for(let e of t.items){let t=O(`w:pPr`,[O(`w:numPr`,[O(`w:ilvl`,[],{"w:val":String(e.level)}),O(`w:numId`,[],{"w:val":i})])]),a=J(e.text,void 0,r);if(n){let e=W(n,a);o.push({"w:p":[t,e]})}else o.push({"w:p":[t,...a]})}}M(e,o,N(e,`body`)),Gn(i,o);let{nextPIdx:l,nextTblIdx:u}=Br(e);for(let t of o)if(`w:tbl`in t){let n=j(t,`tbl${u++}`);e.body.push(n)}else{let n=A(t,`p${l++}`);e.xmlParts.numbering&&F(n,e.xmlParts.numbering),e.body.push(n)}}function Ur(e,t,n){if(t.container){if(t.afterId)throw Error(`"afterId" is not supported when "container" is set`);Kr(e,t,n);return}let r=Wn(e),i=on(t.headers,t.rows);n&&Vr(i,n),M(e,[i],N(e,`body`));let a,o;if(t.afterId){let n=_t(t.afterId,e);n&&(a=n.xmlRef,o=n.id)}Gn(r,[i],a);let{nextTblIdx:s}=Br(e),c=j(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 Wr(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=>_(e)===n);if(r)return{rootNode:r,hf:e}}}function Gr(e,t,n,r){let i=t.container,a=Wr(e,i.type,i.variant);if(!a)throw Error(`${i.type} with variant "${i.variant}" not found`);let{rootNode:o,hf:s}=a,c=y(o),l=i.type===`header`?`hdr`:`ftr`,u=nt(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":[W(n,u)]}:{"w:p":u},M(e,[t],o),c.push(t);let f=A(t,`${l}.${i.variant}.p${d++}`);s.paragraphs.push(f)}}else if(t.kind===`table`){let r=on(t.headers,t.rows);n&&Vr(r,n),M(e,[r],o),c.push(r);let a=s.tables.length,u=j(r,`${l}.${i.variant}.tbl${a}`);s.tables.push(u)}else{let a;a=f&&p===t.listType?f:Fe(e,t.listType),f=a,p=t.listType;for(let u of t.items){let t=O(`w:pPr`,[O(`w:numPr`,[O(`w:ilvl`,[],{"w:val":String(u.level)}),O(`w:numId`,[],{"w:val":a})])]),f=J(u.text,void 0,r),p;p=n?{"w:p":[t,W(n,f)]}:{"w:p":[t,...f]},M(e,[p],o),c.push(p);let m=A(p,`${l}.${i.variant}.p${d++}`);e.xmlParts.numbering&&F(m,e.xmlParts.numbering),s.paragraphs.push(m)}}}function Kr(e,t,n){let r=t.container,i=Wr(e,r.type,r.variant);if(!i)throw Error(`${r.type} with variant "${r.variant}" not found`);let{rootNode:a,hf:o}=i,s=y(a),c=on(t.headers,t.rows);n&&Vr(c,n),M(e,[c],a),s.push(c);let l=r.type===`header`?`hdr`:`ftr`,u=o.tables.length,d=j(c,`${l}.${r.variant}.tbl${u}`);o.tables.push(d)}function qr(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(Yr(t,r,o,s,n)):t.kind===`table`&&a?c.push(Xr(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=>Yr(e,r,o,!1,n))};a&&t.tables.length>0&&(e.tables=t.tables.map(e=>Xr(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=>Yr(e,r,o,!1,n))};a&&t.tables.length>0&&(e.tables=t.tables.map(e=>Xr(e,r,o,n))),l.footers.push(e)}}}return l}function Jr(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 Yr(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(!Jr(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 Xr(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=>Yr(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(Xr(i,t,n,r));return a.length>0&&(i.nestedTables=a),i}function Zr(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 Qr(e).toLowerCase();case`upperRoman`:return Qr(e);default:return String(e)}}function Qr(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 $r=[`decimal`,`lowerLetter`,`lowerRoman`];function ei(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=Le(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,Zr(o,s))}return`${o}${n} `}}let l=$r[i%$r.length]??`decimal`;if(n){let e=Le(n,r,i);e&&(l=e.format)}return`${o}${Zr(c,l)}. `}function ti(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=ei(t,c,l);s.push(ni(t,i,a,o,e))}else t.kind===`table`&&r?(s.push(``),s.push(...si(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=ei(n,e,l);s.push(ni(n,i,!1,o,t))}if(r)for(let e of t.tables)s.push(``),s.push(...si(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=ei(n,e,l);s.push(ni(n,i,!1,o,t))}if(r)for(let e of t.tables)s.push(``),s.push(...si(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 ni(e,t=!1,n=!1,r=!1,i=``){let a=e.style?` (${e.style})`:``,o=r?ii(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=oi(o,t)}else o=oi(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=ai(t,n)}else t=ai(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`[${ri(e)}]${a}${c} ${i}${o}`}function ri(e){return e.paraId?`${e.id} ¶${e.paraId}`:e.id}function ii(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 ai(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 oi(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 si(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=ii(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=oi(n,t)}return n}).join(` `):t?r.paragraphs.map(e=>e.revisions&&e.revisions.length>0?oi(e.text,e.revisions):e.text).join(` `):r.paragraphs.map(e=>e.text).join(` `);let o=r.paragraphs[0];return`[${o?ri(o):`${e.id}.r${i.index}.c${r.colIndex}.p0`}] ${a}`});r.push(`| ${a.join(` | `)} |`)}return r}function ci(e,t=`json`,n={}){switch(t){case`json`:return qr(e,n);case`markdown`:return ti(e,n);default:throw Error(`Unknown projection format: ${t}`)}}const li={"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},ui=new Set([`w:rPr`,`w:trPr`,`w:tcPr`]),di=new Set([`w:moveFromRangeStart`,`w:moveToRangeStart`]),fi=new Set([`w:moveFromRangeEnd`,`w:moveToRangeEnd`]),pi=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 mi(e,t){let n=gi(`accept`,t);return _i(e,n),{accepted:n.count}}function hi(e,t){let n=gi(`reject`,t);return _i(e,n),{rejected:n.count}}function gi(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 _i(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&&vi(e,t.ids),t.moveRangeIds=yi(e,t),Q(e,void 0,t)}function Z(e,t){if(t.ids){let n=S(e,`w:id`);if(n===void 0||!t.ids.has(n))return!1}if(!t.authors)return!0;let n=S(e,`w:author`);return n!==void 0&&t.authors.has(n)}function vi(e,t){let n={from:[],to:[]},r=[],i=new Map,a=new Map,o=e=>{for(let t of e){let e=_(t);if(!e)continue;let s=S(t,`w:id`);if(s!==void 0){if(e===`w:moveFromRangeStart`||e===`w:moveToRangeStart`){let o=S(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=y(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 yi(e,t){let n=new Set,r=e=>{for(let i of e){let e=_(i);if(!e)continue;if(di.has(e)&&Z(i,t)){let e=S(i,`w:id`);e!==void 0&&n.add(e)}let a=y(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=_(i);if(!a){r++;continue}let o=t!==void 0&&ui.has(t);if((a===`w:ins`||a===`w:moveTo`)&&!o){if(Z(i,n)){n.count++,n.mode===`accept`?e.splice(r,1,...y(i)):e.splice(r,1);continue}Q(y(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=y(i);for(let e of t)bi(e);e.splice(r,1,...t)}continue}Q(y(i),a,n),r++;continue}if(di.has(a)||fi.has(a)){let t=S(i,`w:id`);if(di.has(a)?Z(i,n):t!==void 0&&n.moveRangeIds.has(t)){e.splice(r,1);continue}r++;continue}if(a in li){if(Z(i,n)){n.count++,n.mode===`accept`?e.splice(r,1):Si(e,i,a),r=0;continue}r++;continue}if(a===`w:p`){if(Q(y(i),a,n),Ci(e,r,i,n))continue;r++;continue}if(a===`w:tr`){if(Ti(e,r,i,n))continue;Q(y(i),a,n),r++;continue}if(a===`w:tc`){if(Ei(e,r,i,n))continue;Q(y(i),a,n),r++;continue}let s=y(i);s.length>0&&Q(s,a,n),r++}}function bi(e){let t=_(e);if(t){if(t===`w:r`){let t=y(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[`:@`]),k(xi(r[`w:t`]))&&C(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 y(e))bi(t)}}function xi(e){let t=``;for(let n of e)`#text`in n&&(t+=T(String(n[`#text`])));return t}function Si(e,t,n){let r=li[n]??new Set,i=y(t).find(e=>_(e)!==void 0),a=i?y(i).map(E):[],o=e.filter(e=>{let n=_(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 Ci(e,t,n,r){let i=b(n,`w:pPr`);if(!i)return!1;let a=b(i,`w:rPr`);if(!a)return!1;let o=y(a),s=!1,c=o.findIndex(e=>_(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=>_(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=y(i);if(e.splice(e.indexOf(a),1),e.length===0){let e=y(n);e.splice(e.indexOf(i),1)}}return s?wi(e,t,n):!1}function wi(e,t,n){let r=b(n,`w:pPr`);if(r&&b(r,`w:sectPr`))return!1;let i=t+1;for(;i<e.length;){let t=_(e[i]);if(t===`w:p`)break;if(t===void 0||pi.has(t)){i++;continue}return!1}if(i>=e.length)return!1;let a=e[i],o=y(a),s=b(a,`w:pPr`),c=s?o.indexOf(s)+1:0,l=y(n).filter(e=>_(e)!==`w:pPr`);return o.splice(c,0,...l),e.splice(t,1),!0}function Ti(e,t,n,r){let i=b(n,`w:trPr`);if(!i)return!1;let a=y(i),o=a.findIndex(e=>_(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=>_(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 Ei(e,t,n,r){let i=b(n,`w:tcPr`);if(!i)return!1;let a=y(i),o=a.findIndex(e=>_(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=>_(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=>_(e)===`w:cellMerge`);return c&&Z(c,r)&&(r.count++,r.mode===`reject`&&Di(a,c),a.splice(a.indexOf(c),1)),!1}function Di(e,t){let n=S(t,`w:vMergeOrig`),r=e.findIndex(e=>_(e)===`w:vMerge`);if(n===void 0){r!==-1&&e.splice(r,1);return}let i={"w:vMerge":[]};if(n===`rest`&&C(i,`w:val`,`restart`),r!==-1)e[r]=i;else{let t=e.findIndex(e=>{let t=_(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 Oi=`http://schemas.openxmlformats.org/package/2006/metadata/core-properties`,ki=`http://purl.org/dc/elements/1.1/`,Ai=`http://purl.org/dc/terms/`,ji=`http://purl.org/dc/dcmitype/`,Mi=`http://www.w3.org/2001/XMLSchema-instance`,Ni={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`},Pi=[`creator`,`lastModifiedBy`,`description`,`keywords`,`category`,`contentStatus`],Fi=new Set([`Company`,`Manager`,`HyperlinkBase`]);function $(e){let t=e.indexOf(`:`);return t===-1?e:e.slice(t+1)}function Ii(e,t){if(e)return e.find(e=>{let n=_(e);return n!==void 0&&$(n)===t})}function Li(e,t){return y(e).find(e=>{let n=_(e);return n!==void 0&&$(n)===t})}function Ri(e){let t=``;for(let n of y(e))`#text`in n&&(t+=T(String(n[`#text`])));return t}function zi(e,t){let n=_(e);e[n]=t.length>0?[{"#text":w(t)}]:[]}function Bi(e,t){let n=y(e),r=n.findIndex(e=>{let n=_(e);return n!==void 0&&$(n)===t});return r===-1?!1:(n.splice(r,1),!0)}function Vi(){return[{"?xml":[{"#text":``}],":@":{"@_version":`1.0`,"@_encoding":`UTF-8`,"@_standalone":`yes`}},O(`cp:coreProperties`,[],{"xmlns:cp":Oi,"xmlns:dc":ki,"xmlns:dcterms":Ai,"xmlns:dcmitype":ji,"xmlns:xsi":Mi})]}const Hi={"xmlns:cp":Oi,"xmlns:dc":ki,"xmlns:dcterms":Ai,"xmlns:dcmitype":ji,"xmlns:xsi":Mi};function Ui(e){e.xmlParts.core||(e.xmlParts.core=Vi(),e.xmlParts.rootRels||(e.xmlParts.rootRels=vr()),Cr(e.xmlParts.rootRels,`http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties`,`docProps/core.xml`),e.xmlParts.contentTypes||(e.xmlParts.contentTypes=yr()),Tr(e.xmlParts.contentTypes,`/docProps/core.xml`,`application/vnd.openxmlformats-package.core-properties+xml`));let t=Ii(e.xmlParts.core,`coreProperties`);if(!t)throw Error(`docProps/core.xml has no coreProperties root`);for(let[e,n]of Object.entries(Hi))S(t,e)===void 0&&C(t,e,n);return t}function Wi(e){let t={},n=Ii(e.xmlParts.core,`coreProperties`);if(!n)return t;for(let e of Object.keys(Ni)){let r=Li(n,$(Ni[e]));r&&(t[e]=Ri(r))}return t}function Gi(e,t){let n=Ui(e);for(let e of Object.keys(t)){let r=t[e];if(r===void 0||!(e in Ni))continue;let i=Ni[e],a=Li(n,$(i));a||(a=O(i,[]),y(n).push(a)),zi(a,r),(e===`created`||e===`modified`)&&C(a,`xsi:type`,`dcterms:W3CDTF`)}}function Ki(e){let t=[],n=Ii(e.xmlParts.core,`coreProperties`);if(n){for(let e of Pi)Bi(n,$(Ni[e]));Gi(e,{revision:`1`})}let r=Ii(e.xmlParts.app,`Properties`);if(r){let e=y(r);for(let t=e.length-1;t>=0;t--){let n=_(e[t]);n!==void 0&&Fi.has($(n))&&e.splice(t,1)}}let i=wr(e.xmlParts.rootRels,`http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties`);Er(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 qi=class e{zipEntries;doc;removedParts=new Set;constructor(e,t){this.zipEntries=e,this.doc=t}static async load(t,n){let r;r=typeof t==`string`?await(0,c.readFile)(t):t;let i=await d(r);return new e(i,Xe(i,n))}static async create(t,n){if(t!==void 0)return e.load(t,n);let r=new l.default;r.file(`[Content_Types].xml`,`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
17
19
  <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
18
20
  <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
19
21
  <Default Extension="xml" ContentType="application/xml"/>
@@ -31,4 +33,4 @@ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.
31
33
  </w:r>
32
34
  </w:p>
33
35
  </w:body>
34
- </w:document>`);let i=await r.generateAsync({type:`nodebuffer`,compression:`DEFLATE`});return e.load(i,n)}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 Ye(this.doc),t}rejectAllRevisions(e){let t=li(this.doc,e);return Ye(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 g(this.doc.xmlParts.document)}async toBuffer(){let e=g(this.doc.xmlParts.document),t=new Map;for(let[e,n]of this.doc.xmlParts.headers)t.set(e,g(n));let n=new Map;for(let[e,t]of this.doc.xmlParts.footers)n.set(e,g(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?g(e):void 0,a=this.doc.xmlParts;return m({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 L(e,this.doc)}findParagraphByParaId(e){return dt(e,this.doc)}findTableById(e){return R(e,this.doc)}getStyles(){return this.doc.styles}validatePatches(e){return e.map(e=>lt(e,this.doc))}};exports.Docxtor=Bi,exports.acceptAllRevisions=ci,exports.applyPatches=Dr,exports.cloneDocument=Ar,exports.findParagraphById=L,exports.findParagraphByParaId=dt,exports.findTableById=R,exports.getCoreProperties=Li,exports.normalizeDocument=Je,exports.parseXml=_,exports.project=ni,exports.projectToJson=Vr,exports.projectToMarkdown=Yr,exports.reNormalizeDocument=Ye,exports.readDocx=d,exports.rejectAllRevisions=li,exports.scrubMetadata=zi,exports.serializeXml=g,exports.setCoreProperties=Ri,exports.validatePatch=lt,exports.writeDocx=m;
36
+ </w:document>`);let i=await r.generateAsync({type:`nodebuffer`,compression:`DEFLATE`});return e.load(i,n)}project(e=`json`,t={}){return ci(this.doc,e,t)}applyPatches(e,t){return Nr(this.doc,e,t)}acceptAllRevisions(e){let t=mi(this.doc,e);return P(this.doc),t}rejectAllRevisions(e){let t=hi(this.doc,e);return P(this.doc),t}getCoreProperties(){return Wi(this.doc)}setCoreProperties(e){Gi(this.doc,e)}scrubMetadata(){let{removedParts:e}=Ki(this.doc);for(let t of e)this.removedParts.add(t)}toString(){return h(this.doc.xmlParts.document)}async toBuffer(){let e=h(this.doc.xmlParts.document),t=new Map;for(let[e,n]of this.doc.xmlParts.headers)t.set(e,h(n));let n=new Map;for(let[e,t]of this.doc.xmlParts.footers)n.set(e,h(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?h(e):void 0,a=this.doc.xmlParts;return m({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 I(e,this.doc)}findParagraphByParaId(e){return ft(e,this.doc)}findTableById(e){return L(e,this.doc)}getStyles(){return this.doc.styles}validatePatches(e){return e.map(e=>ut(e,this.doc))}};exports.Docxtor=qi,exports.acceptAllRevisions=mi,exports.applyPatches=Nr,exports.cloneDocument=Ir,exports.findParagraphById=I,exports.findParagraphByParaId=ft,exports.findTableById=L,exports.getCoreProperties=Wi,exports.normalizeDocument=Xe,exports.parseXml=g,exports.project=ci,exports.projectToJson=qr,exports.projectToMarkdown=ti,exports.reNormalizeDocument=P,exports.readDocx=d,exports.rejectAllRevisions=hi,exports.scrubMetadata=Ki,exports.serializeXml=h,exports.setCoreProperties=Gi,exports.validatePatch=ut,exports.writeDocx=m;
package/dist/index.d.cts CHANGED
@@ -165,6 +165,12 @@ interface RevisionMark {
165
165
  author: string;
166
166
  /** The revision date (ISO 8601). */
167
167
  date: string;
168
+ /**
169
+ * The `w:id` of the `w:ins` / `w:del` element, when it has one. Pass it to
170
+ * `acceptAllRevisions({ ids })` / `rejectAllRevisions({ ids })` to resolve
171
+ * this change alone.
172
+ */
173
+ id?: string;
168
174
  }
169
175
  interface RunMapping {
170
176
  /** This run's text content. */
@@ -650,6 +656,13 @@ interface PatchOptions {
650
656
  interface RevisionFilterOptions {
651
657
  /** Only process revisions by these authors. Omit to process every revision. */
652
658
  authors?: string[];
659
+ /**
660
+ * Only process revisions whose `w:id` is in this list (combined with
661
+ * `authors` when both are given). Ids come from `RevisionMark.id` in a
662
+ * projection with `includeRevisions`. A replacement is usually two
663
+ * revisions, a `w:del` and a `w:ins`, each with its own id.
664
+ */
665
+ ids?: string[];
653
666
  }
654
667
  /** Core document properties from docProps/core.xml. */
655
668
  interface CoreProperties {