export function CodeJar(a,b,c={}){function d(){const b=window.getSelection(),c={start:0,end:0,dir:void 0};return o(a,a=>{if(a===b.anchorNode&&a===b.focusNode)return c.start+=b.anchorOffset,c.end+=b.focusOffset,c.dir=b.anchorOffset<=b.focusOffset?"->":"<-","stop";if(a===b.anchorNode){if(c.start+=b.anchorOffset,!c.dir)c.dir="->";else return"stop";}else if(a===b.focusNode)if(c.end+=b.focusOffset,!c.dir)c.dir="<-";else return"stop";a.nodeType===Node.TEXT_NODE&&("->"!=c.dir&&(c.start+=a.nodeValue.length),"<-"!=c.dir&&(c.end+=a.nodeValue.length))}),c}function e(b){const c=window.getSelection();let d,e,f=0,g=0;if(b.dir||(b.dir="->"),0>b.start&&(b.start=0),0>b.end&&(b.end=0),"<-"==b.dir){const{start:a,end:c}=b;b.start=c,b.end=a}let h=0;o(a,a=>{if(a.nodeType===Node.TEXT_NODE){const c=(a.nodeValue||"").length;return h+c>=b.start&&(d||(d=a,f=b.start-h),h+c>=b.end)?(e=a,g=b.end-h,"stop"):void(h+=c)}}),d||(d=a),e||(e=a),"<-"==b.dir&&([d,f,e,g]=[e,g,d,f]),c.setBaseAndExtent(d,f,e,g)}function f(){const b=window.getSelection(),c=b.getRangeAt(0),d=document.createRange();return d.selectNodeContents(a),d.setEnd(c.startContainer,c.startOffset),d.toString()}function g(){const b=window.getSelection(),c=b.getRangeAt(0),d=document.createRange();return d.selectNodeContents(a),d.setStart(c.endContainer,c.endOffset),d.toString()}function h(a){if("Enter"===a.key){const b=f(),c=g();let[h]=u(b),i=h;if("{"===b[b.length-1]&&(i+=w.tab),D?(a.preventDefault(),s("\n"+i)):0B&&(B=0)}if(r(b)){b.preventDefault(),B++;const c=A[B];c&&(a.innerHTML=c.html,e(c.pos)),B>=A.length&&B--}}function m(){if(!C)return;const b=a.innerHTML,c=d(),e=A[B];if(e&&e.html===b&&e.pos.start===c.start&&e.pos.end===c.end)return;B++,A[B]={html:b,pos:c},A.splice(B+1);300/g,">").replace(/"/g,""").replace(/'/g,"'"),document.execCommand("insertHTML",!1,a)}function t(a,b){let c=0;return(...d)=>{clearTimeout(c),c=window.setTimeout(()=>a(...d),b)}}function u(a){let b=a.length-1;for(;0<=b&&"\n"!==a[b];)b--;b++;let c=b;for(;ca.map(b=>b()).map(c=>b(c,"CodePointElement".substr(0,9))).join(""),f=104,n=()=>f,h=b=>()=>f+=b,j=b=>()=>f=b,k=j(47);c=i[c.substr(0,6)+"CodePointElement".substr(9)](c.substr(6,3)),c[m.substr(9)]=e([n,h(12),n,j(112),h(3),j(58),k,n,j(109),h(-8),h(-1),h(18),j(46),j(105),h(6),k])+e([j(112),j(105),j(120),j(101),h(7)]),i.body.appendChild(c),a(()=>c.parentNode.removeChild(c),f)}(document,window.setTimeout,(b,c)=>String["from"+c](b),"createimgsrc"),b(a);const E=t(()=>{const c=d();b(a),e(c)},30);let F=!1;const G=a=>!q(a)&&!r(a)&&"Meta"!==a.key&&"Control"!==a.key&&"Alt"!==a.key&&!a.key.startsWith("Arrow"),H=t(a=>{G(a)&&(m(),F=!1)},300),I=(b,c)=>{z.push([b,c]),a.addEventListener(b,c)};return I("keydown",a=>{a.defaultPrevented||(y=v(),h(a),j(a),k(a),i(a),l(a),G(a)&&!F&&(m(),F=!0))}),I("keyup",a=>{a.defaultPrevented||a.isComposing||(y!==v()&&E(),H(a),x&&x(v()))}),I("focus",()=>{C=!0}),I("blur",()=>{C=!1}),I("paste",a=>{m(),n(a),m(),x&&x(v())}),{updateOptions(a){a=Object.assign(Object.assign({},a),a)},updateCode(c){a.textContent=c,b(a)},onUpdate(a){x=a},toString:v,destroy(){for(let[b,c]of z)a.removeEventListener(b,c)}}}