function n(n,t,e){const o=window.getSelection();if(!o||0===o.rangeCount)return;const c=o.getRangeAt(0),r={strong:["strong","b"],em:["em","i"],u:["u"],span:["span"]}[t]??[t],i=n=>r.includes(n.tagName.toLowerCase());function s(n){const t=n.parentNode;if(t){for(;n.firstChild;)t.insertBefore(n.firstChild,n);t.removeChild(n)}}function f(n){n.querySelectorAll(r.join(",")).forEach((n=>{if(!n.textContent?.trim())return void n.remove();Array.from(n.querySelectorAll(r.join(","))).forEach((t=>{if(t!==n){for(;t.firstChild;)n.insertBefore(t.firstChild,t);t.remove()}}))}))}function u(n){Array.from(n.querySelectorAll(r.join(","))).forEach((n=>{const t=n.nextSibling;if(t instanceof HTMLElement&&i(t)){for(;t.firstChild;)n.appendChild(t.firstChild);t.remove()}}))}if(c.collapsed){let r=!1,d=c.startContainer;for(;d&&d!==n;){if(d instanceof HTMLElement&&i(d)){r=!0;break}d=d.parentNode}if(r&&d instanceof HTMLElement)s(d);else{const n=document.createElement(t);e&&Object.entries(e).forEach((([t,e])=>n.setAttribute(t,e))),n.appendChild(document.createTextNode("​")),c.insertNode(n),c.setStart(n.firstChild,1),c.collapse(!0),o.removeAllRanges(),o.addRange(c)}return f(n),void u(n)}c.startContainer.nodeType===Node.TEXT_NODE&&c.startContainer.splitText(c.startOffset),c.endContainer.nodeType===Node.TEXT_NODE&&c.endContainer.splitText(c.endOffset);const d=document.createTreeWalker(c.commonAncestorContainer,NodeFilter.SHOW_TEXT,{acceptNode:n=>{if(!n.nodeValue?.trim())return NodeFilter.FILTER_REJECT;const t=document.createRange();t.selectNodeContents(n);return c.compareBoundaryPoints(Range.END_TO_START,t)<0&&c.compareBoundaryPoints(Range.START_TO_END,t)>0?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_REJECT}}),m=[];let a;for(;a=d.nextNode();)m.push(a);if(m.length>0&&m.every((t=>{let e=t;for(;e&&e!==n;){if(e instanceof HTMLElement&&i(e))return!0;e=e.parentNode}return!1}))){const e=m[0],o=m[m.length-1],c=e.parentElement?.closest(t);if(c&&c.contains(o)){const n=c.parentNode;if(n){const r=document.createRange();r.setStartBefore(c),r.setEndBefore(e);const i=r.cloneContents(),s=document.createRange();s.setStartAfter(o),s.setEndAfter(c);const f=s.cloneContents(),u=document.createRange();u.setStartBefore(e),u.setEndAfter(o);const d=u.extractContents(),m=document.createDocumentFragment();if(i.childNodes.length){const n=document.createElement(t);for(;i.firstChild;)n.appendChild(i.firstChild);m.appendChild(n)}for(;d.firstChild;)m.appendChild(d.firstChild);if(f.childNodes.length){const n=document.createElement(t);for(;f.firstChild;)n.appendChild(f.firstChild);m.appendChild(n)}n.insertBefore(m,c),n.removeChild(c)}}else m.forEach((t=>{let e=t;for(;e&&e!==n;){if(e instanceof HTMLElement&&i(e)){s(e);break}e=e.parentNode}}))}else m.forEach((n=>{const o=n.parentElement?.closest(".mention, nile-mention");if(o)return;const c=n.parentElement?.closest("a");if(c&&"span"===t)return;const r=document.createElement(t);e&&Object.entries(e).forEach((([n,t])=>r.setAttribute(n,t))),c?(c.insertBefore(r,n),r.appendChild(n)):(n.parentNode?.insertBefore(r,n),r.appendChild(n))}));f(n),u(n),o.removeAllRanges(),o.addRange(c)}function t(n){const t=n.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/i);if(!t)return"#000000";return`#${Number(t[1]).toString(16).padStart(2,"0")}${Number(t[2]).toString(16).padStart(2,"0")}${Number(t[3]).toString(16).padStart(2,"0")}`}function e(n,t){const e=document.getSelection();if(!e||0===e.rangeCount)return;const o=e.getRangeAt(0);if(o.collapsed){const n=document.createElement("span");n.style.fontFamily=t,n.appendChild(document.createTextNode("​")),o.insertNode(n);const c=document.createRange();return c.setStart(n.firstChild,1),c.collapse(!0),e.removeAllRanges(),void e.addRange(c)}!function(n,t,e){const o=document.createElement(t);e&&Object.entries(e).forEach((([n,t])=>o.setAttribute(n,t)));try{n.surroundContents(o)}catch{const t=n.extractContents();o.appendChild(t),n.insertNode(o)}}(o,"span",{style:`font-family:${t}`})}export{t as r,e as s,n as t};
