function getNodeData(e){return e.data}export class Fragment{constructor(e){this._element=e,this._elementsById=new Map}element(){return this._element}$(e){return this._elementsById.get(e)}static build(e,...t){return Fragment._render(Fragment._template(e),t)}static cached(e,...t){let r=_templateCache.get(e);return r||(r=Fragment._template(e),_templateCache.set(e,r)),Fragment._render(r,t)}static _template(e){let t="",r=!0;for(let d=0;d<e.length-1;d++){t+=e[d];const n=e[d].lastIndexOf(">"),a=e[d].indexOf("<",n+1);-1!==n&&-1===a?r=!0:-1!==a&&(r=!1),t+=r?_textMarker:_attributeMarker(d)}t+=e[e.length-1];const n=document.createElement("template");n.innerHTML=t;const a=n.ownerDocument.createTreeWalker(n.content,NodeFilter.SHOW_ELEMENT|NodeFilter.SHOW_TEXT,null,!1);let o=0;const l=[],s=[],i=[];for(;a.nextNode();){const e=a.currentNode;if(e.nodeType===Node.ELEMENT_NODE&&e.hasAttributes()){e.hasAttribute("$")&&(i.push(e),s.push({replaceNodeIndex:void 0,attr:void 0,elementId:e.getAttribute("$")||""}),e.removeAttribute("$"));const t=[];for(let r=0;r<e.attributes.length;r++){const n=e.attributes[r].name;if(!_attributeMarkerRegex.test(n)&&!_attributeMarkerRegex.test(e.attributes[r].value))continue;t.push(n),i.push(e);const a={index:o,names:n.split(_attributeMarkerRegex),values:e.attributes[r].value.split(_attributeMarkerRegex)};o+=a.names.length-1,o+=a.values.length-1;const l={elementId:void 0,replaceNodeIndex:void 0,attr:a};s.push(l)}for(let r=0;r<t.length;r++)e.removeAttribute(t[r])}if(e.nodeType===Node.TEXT_NODE&&-1!==getNodeData(e).indexOf(_textMarker)){const t=getNodeData(e).split(_textMarkerRegex);e.data=t[t.length-1];const r=e.parentNode;for(let n=0;n<t.length-1;n++){t[n]&&r.insertBefore(document.createTextNode(t[n]),e);const a=document.createElement("span");i.push(a),s.push({attr:void 0,elementId:void 0,replaceNodeIndex:o++}),r.insertBefore(a,e)}}e.nodeType!==Node.TEXT_NODE||e.previousSibling&&e.previousSibling.nodeType!==Node.ELEMENT_NODE||e.nextSibling&&e.nextSibling.nodeType!==Node.ELEMENT_NODE||!/^\s*$/.test(getNodeData(e))||l.push(e)}for(let d=0;d<i.length;d++)i[d].classList.add(_class(d));for(const d of l)d.remove();return{template:n,binds:s}}static _render(e,t){const r=e.template.ownerDocument.importNode(e.template.content,!0),n=r.firstChild===r.lastChild?r.firstChild:r,a=new Fragment(n),o=[];for(let l=0;l<e.binds.length;l++){const e=_class(l),t=r.querySelector("."+e);t.classList.remove(e),o.push(t)}for(let l=0;l<e.binds.length;l++){const r=e.binds[l],n=o[l];if(void 0!==r.elementId)a._elementsById.set(r.elementId,n);else if(void 0!==r.replaceNodeIndex){const e=t[r.replaceNodeIndex];n.parentNode.replaceChild(this._nodeForValue(e),n)}else{if(void 0===r.attr)throw new Error("Unexpected bind");if(2===r.attr.names.length&&1===r.attr.values.length&&"function"===typeof t[r.attr.index])t[r.attr.index].call(null,n);else{let e=r.attr.names[0];for(let n=1;n<r.attr.names.length;n++)e+=t[r.attr.index+n-1],e+=r.attr.names[n];if(e){let a=r.attr.values[0];for(let e=1;e<r.attr.values.length;e++)a+=t[r.attr.index+r.attr.names.length-1+e-1],a+=r.attr.values[e];n.setAttribute(e,a)}}}}return a}static _nodeForValue(e){if(e instanceof Node)return e;if(e instanceof Fragment)return e._element;if(Array.isArray(e)){const t=document.createDocumentFragment();for(const r of e)t.appendChild(this._nodeForValue(r));return t}return document.createTextNode(String(e))}}export const _textMarker="{{template-text}}";const _textMarkerRegex=/{{template-text}}/;export const _attributeMarker=e=>"template-attribute"+e;const _attributeMarkerRegex=/template-attribute\d+/,_class=e=>"template-class-"+e,_templateCache=new Map;export const html=(e,...t)=>Fragment.cached(e,...t).element();export let TemplateDefinition;export let _Bind;export let _Template;