import dashToCamel from"../utils/dash-to-camel.js";import{REGEX,ATTR,TAG,PROPS_ATTRIBUTES}from"../constants.js";import directive from"../directives/index.js";import{isDirective}from"../directives/helpers.js";import{tplCache}from"./stores.js";const regExcludeSpecial=new RegExp(`</?(${TAG.TEXT_NODE_PLACE}|${TAG.ITERATE_NODE_PLACE})?>$`),selfClosingElements={meta:!0,img:!0,link:!0,input:!0,area:!0,br:!0,hr:!0},elementsClosedByOpening={li:{li:!0},p:{p:!0,div:!0},td:{td:!0,th:!0},th:{td:!0,th:!0}},elementsClosedByClosing={li:{ul:!0,ol:!0},a:{div:!0},b:{div:!0},i:{div:!0},p:{div:!0},td:{tr:!0,table:!0},th:{tr:!0,table:!0}};function last(e){return e[e.length-1]}function removeNLS(e){return e.replace(REGEX.MATCH_NLS,"")}class Element{constructor(e,t,s,l,i){this.type=e,this.props=t,this.children=[],l&&(this.style=l),i&&(this.styleScoped=i),(s||"svg"===e)&&(this.isSVG=!0),void 0!==t.key&&(this.key=t.key)}appendChild(e){return e.props&&void 0!==e.props.key&&(this.hasKeys=!0),this.children.push(e),e}}function compile(e){if(!e)return"";if(tplCache[e])return tplCache[e];const t=new Element(null,{}),s=[t];let l,i,r=t,o=-1;for(;l=REGEX.HTML_MARKUP.exec(e);){if(o>-1&&o+l[0].length<REGEX.HTML_MARKUP.lastIndex){let t=removeNLS(e.substring(o,REGEX.HTML_MARKUP.lastIndex-l[0].length));t&&("style"===r.type&&void 0!==r.props["data-is-webcomponent"]&&(t=t.replace(/:(component|wrapper|root)/g,":host")),!0===r.style?r.style=t:(" e-0_"===t.substr(0,5)&&(t=t.trim()),r.appendChild(t)))}if(o=REGEX.HTML_MARKUP.lastIndex,"!"!==l[0][1]){if("</>"===l[0]){let e=last(s).type;e&&(l[0]=`</${e}>`,l[1]="/",l[2]=e)}if(!regExcludeSpecial.test(l[0])){if("slot"===l[2]&&(l[2]=TAG.SLOT),!l[1]&&l[0]){i={};for(let e;e=REGEX.HTML_ATTRIBUTE.exec(l[3]);)i[e[2]]=e[5]||e[6]||"",propsFixer(l[0].substring(1,l[0].length-1),e[2],i[e[2]],i,null);if(!l[4]&&elementsClosedByOpening[r.type]&&elementsClosedByOpening[r.type][l[2]]&&(s.pop(),r=last(s)),"style"===l[2]&&void 0===i["data-is-webcomponent"]){r.style=!0,""===i["data-scoped"]&&(r.styleScoped=!0);continue}r=r.appendChild(new Element(l[2],i,r.isSVG)),s.push(r)}if(l[1]||l[4]||selfClosingElements[l[2]])for(;;){if(r.type===l[2]){s.pop(),r=last(s);break}if(!elementsClosedByClosing[r.type]||!elementsClosedByClosing[r.type][l[2]])break;s.pop(),r=last(s)}}}}if(t.style&&"object"==typeof t.children[0]&&(t.children[0].style=t.style,t.children[0].styleScoped=t.styleScoped),t.children.length>1)t.type=TAG.ROOT;else if(t.children.length)return tplCache[e]=t.children[0],t.children[0];return tplCache[e]=t,t}function serializeProps(e){const t={};if(e._dozAttach[PROPS_ATTRIBUTES]){let s=Object.keys(e._dozAttach[PROPS_ATTRIBUTES]);for(let l=0;l<s.length;l++)propsFixer(e.nodeName,s[l],e._dozAttach[PROPS_ATTRIBUTES][s[l]],t,e)}else if(e.attributes){const s=Array.from(e.attributes);for(let l=s.length-1;l>=0;--l){let i=s[l];propsFixer(e.nodeName,i.name,i.nodeValue,t,e)}}return t}function propsFixer(e,t,s,l,i){"string"==typeof s&&REGEX.IS_STRING_QUOTED.test(s)&&(s=s.replace(REGEX.REPLACE_QUOT,"&quot;"));let r=isDirective(t),o=REGEX.IS_CUSTOM_TAG.test(e)&&!r?dashToCamel(t):t;i&&directive.callAppComponentPropsAssignName(i,t,s,r,l,(e=>{o=e})),l[o]=t===ATTR.FORCE_UPDATE||s}export{compile};export{serializeProps};export{propsFixer};export{Element};export{removeNLS};export{last};export default{compile:compile,serializeProps:serializeProps,propsFixer:propsFixer,Element:Element,removeNLS:removeNLS,last:last};