export class PageBreakManagerEl extends HTMLElement{constructor(){super(),this.windowControllers=new AbortController,this.target=null,this.breaks=[],this._timer=0}getParent(e,t=null){var a=null,r=null;return this.target&&("indent"===t?this.target.querySelectorAll("page-break").forEach((t=>{t===e&&(r=a),a=t})):"outdent"===t?this.target.querySelector(`page-break[path="${e.parent}"]`)&&(r=this.target.querySelector(`page-break[path="${e.parent}"]`),this.target.querySelector(`page-break[path="${r.parent}"]`)&&(r=this.target.querySelector(`page-break[path="${r.parent}"]`)),r=null):this.target.querySelector(`page-break[path="${e.parent}"]`)&&(r=this.target.querySelector(`page-break[path="${e.parent}"]`))),r}elementsBetween(e,t="page-break",a,r="nextElementSibling"){var n=[];for(e=e[r];e&&!e.matches(t);)!a||e.matches(a)?(n.push(e),e=e[r]):e=e[r];return n}associatedPageBreak(e){for(e=e.previousElementSibling;e&&!e.matches("page-break");)e=e.previousElementSibling;return e}betweenElementsQuery(e="all"){var t=[];switch(e){case"headings":this.breaks.forEach((e=>{t=[...t,...this.elementsBetween(e,"page-break","h1,h2,h3,h4,h5,h6")]}));break;case"noheadings":this.breaks.forEach((e=>{t=[...t,...this.elementsBetween(e,"page-break",":not(h1,h2,h3,h4,h5,h6)")]}));break;case"titles":this.breaks.forEach((e=>{t=[...t,...this.elementsBetween(e,"page-break","[data-page-break-title]")]}));break;case"notitles":this.breaks.forEach((e=>{t=[...t,...this.elementsBetween(e,"page-break",":not([data-page-break-title])")]}));break;case"all":this.breaks.forEach((e=>{t=[...t,...this.elementsBetween(e)]}))}return t}connectedCallback(){globalThis.addEventListener("page-break-registration",this.registerPageBreak.bind(this),{signal:this.windowControllers.signal})}disconnectedCallback(){this.windowControllers.abort()}registerPageBreak(e){"add"===e.detail.action?this.breaks.find((t=>t===e.detail.value))||this.breaks.push(e.detail.value):this.breaks.map(((t,a)=>{t===e.detail.value&&this.breaks.splice(a,1)})),1===this.breaks.length?this.target=e.detail.value.parentNode:0===this.breaks.length&&(this.target=null),this.__lock||(this.__lock=!0,setTimeout((()=>{if(this.breaks.forEach((e=>{this.elementsBetween(e,"page-break","h1,h2,h3,h4,h5,h6").forEach((t=>{let a=(t.getAttribute("data-original-level")?new Number(t.getAttribute("data-original-level").replace("H","")):new Number(t.tagName.replace("H","")))+e.depth;a=a>6?6:a;const r=globalThis.document.createElement(`h${a}`);r.setAttribute("data-original-level",t.tagName);for(var n=0,l=t.attributes.length;n<l;++n)r.setAttribute(t.attributes.item(n).nodeName,t.attributes.item(n).nodeValue);r.innerHTML=t.innerHTML,t.parentNode.replaceChild(r,t),e.target=r}))})),this.target&&this.target.children&&this.target.children.length>0){var e=[];const t=this.target.querySelectorAll("page-break");for(let a=0;a<t.length;a++){let r=t[a];if(e.length>0){if(r.parent&&-1!==e.indexOf(r.parent))for(;-1!==e.indexOf(r.parent);)e.shift();else if(r.parent&&-1===e.indexOf(r.parent));else for(;e.length>0;)e.shift();let n=0;0!==a&&r.parent&&this.target.querySelector(`page-break[path="${r.parent}"]`)&&(n=this.target.querySelector(`page-break[path="${r.parent}"]`).depth+1),r.depth=n,a!=t.length&&t[a+1]&&t[a+1].parent===r.path&&r.parent&&e.unshift(r.parent)}}}this.__lock=!1}),10))}}globalThis.PageBreakManager=globalThis.PageBreakManager||{},globalThis.PageBreakManager.requestAvailability=()=>(globalThis.PageBreakManager.instance||(globalThis.PageBreakManager.instance=globalThis.document.createElement("page-break-manager"),globalThis.document.body.appendChild(globalThis.PageBreakManager.instance)),globalThis.PageBreakManager.instance),customElements.define("page-break-manager",PageBreakManagerEl);export const pageBreakManager=globalThis.PageBreakManager.requestAvailability();