const e="attachShadow"in Element.prototype&&"getRootNode"in Element.prototype,t=!(!e||!document.createElement("div").attachShadow({mode:"open"}).getSelection),n=window.ShadyDOM&&window.ShadyDOM.inUse,o=/^((?!chrome|android).)*safari/i.test(navigator.userAgent)||/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream,r=!e||n||!t&&!o,i=/^(area|base|br|col|command|embed|hr|img|input|keygen|link|meta|param|script|source|style|template|track|wbr)$/;export const eventName="-shadow-selectionchange";const a=[Node.ELEMENT_NODE,Node.TEXT_NODE,Node.DOCUMENT_FRAGMENT_NODE];export function findCaretFocus(e,t){const n=[],pushAll=e=>{for(let t=0;t<e.length;++t)e[t].shadowRoot&&n.push(e[t].shadowRoot)};for(t.shadowRoot&&n.push(t.shadowRoot),pushAll(t.childNodes);n.length;){const t=n.shift();for(let n=0;n<t.childNodes.length;++n)if(e.containsNode(t.childNodes[n],!0))return t;pushAll(t.querySelectorAll("*"))}return null}function findNode(e,t,n){const o=t.childNodes||t.children;if(!o)return t;for(let t=0;t<o.length;++t){const d=o[n?t:o.length-1-t];if((r=d,a.includes(r.nodeType))&&e.containsNode(d,!0)){if(e.containsNode(d,!1))return d;if(!i.exec(d.localName||""))return findNode(e,d,n)}}var r;return t}let d={node:null,offset:-1};function containsNextElement(e,t,n){const o=t;for(;(t=walkFromNode(t,n))&&t.contains(o););return!!t&&(t instanceof Element&&e.containsNode(t,!0))}function walkFromNode(e,t){if(!t)return e.previousSibling||e.parentNode||null;for(;e;){if(e.nextSibling)return e.nextSibling;e=e.parentNode}return null}!function(){if(t||r)return document.addEventListener("selectionchange",(e=>{document.dispatchEvent(new CustomEvent(eventName))})),()=>{};let e=!1;document.addEventListener("selectionchange",(t=>{if(e)return;e=!0;const n=window.getSelection();if("Caret"===n.type){const e=findCaretFocus(n,n.anchorNode);if(e instanceof window.ShadowRoot){const t=getRange(e);if(t){const e=t.startContainer,n=t.startOffset;d={node:e,offset:n}}}}document.dispatchEvent(new CustomEvent("-shadow-selectionchange")),window.requestAnimationFrame((()=>{e=!1}))}))}();const c=new Map;export function getRange(e){if(r){const t=document.getSelection();return t.containsNode(e,!0)?t.getRangeAt(0):null}if(t){const t=e.getSelection();return t.rangeCount?t.getRangeAt(0):null}const n=c.get(e);if(n)return n;const o=function internalGetShadowSelection(e){const t=window.getSelection();if("None"===t.type)return{range:null,type:"none"};if("Caret"!==t.type&&"Range"!==t.type)throw new TypeError("unexpected type: "+t.type);const n=findNode(t,e,!0);if(n===e)return{range:null,mode:"none"};const o=document.createRange();let r,i=null;if("Range"===t.type&&(i=findNode(t,e,!1),r=function getSelectionDirection(e,t,n){if("Range"!==e.type)return;const measure=()=>e.toString().length,o=measure();let r;return e.modify("extend","forward","character"),r=measure(),r>o||containsNextElement(e,n,!0)?(e.modify("extend","backward","character"),!0):r<o||!e.containsNode(t)?(e.modify("extend","backward","character"),!1):(e.modify("extend","backward","character"),r=measure(),r>o||containsNextElement(e,t,!1)?(e.modify("extend","forward","character"),!1):r<o||!e.containsNode(n)?(e.modify("extend","forward","character"),!0):void 0)}(t,n,i),void 0===r))return o.setStart(n,0),o.setEnd(i,i.length),{range:o,mode:"all"};const a=t.toString().length;let c=0,l=0;if(null===i);else if(i.nodeType===Node.TEXT_NODE){const e=i.textContent,n=i.nextSibling;for(let n=e.length-1;n>=0;--n){i.splitText(n);if(t.toString().length!==a){l=n+1;break}}for(i.insertData(i.length,e.substr(i.length));i.nextSibling!==n;)i.nextSibling.remove()}if(n.nodeType===Node.TEXT_NODE){n!==i&&(n.appendData("?"),t.collapseToStart(),t.modify("extend","right","character"));const e=n.textContent,o=n.nextSibling;for(let o=n===i?l:e.length-1;o>=0;--o)if(n.splitText(o),""===t.toString()){c=o;break}for(n.insertData(n.length,e.substr(n.length));n.nextSibling!==o;)n.nextSibling.remove();n!==i&&n.deleteData(n.length-1,1),null===i&&(i=n,l=c)}else null===i&&(i=n);1===a&&d&&d.node===n&&d.offset>c&&r&&(r=!1);!0===r?(t.collapse(n,c),t.extend(i,l)):!1===r?(t.collapse(i,l),t.extend(n,c)):t.setPosition(n,c);return o.setStart(n,c),o.setEnd(i,l),{range:o,mode:"normal"}}(e);return c.set(e,o.range),window.setTimeout((()=>{c.delete(e)}),0),o.range}