import{waitingElementHanlders as e}from"../internal-state.mjs";import{isFunction as t}from"../utils/is-function.mjs";import{nonBubblingAttributes as s}from"../lifecycle/lifecycle-handler.mjs";import{delegateEvent as n}from"../lifecycle/event-delegation.mjs";import{errorSink as r}from"../sinks/error-sink.mjs";import{terminationSink as i}from"../sinks/termination-sink.mjs";const o=/\s*<!--\s*SINK:\s*(\w+)\s*-->\s*$/;let a=0;const l=(t,s)=>{const n=(e.get(t)||[]).concat(s);e.set(t,n)};function c(e,...p){let u="";for(let f=0;f<e.length;f++){const m=e[f],d=u+m,b=d.lastIndexOf("<"),g=p[f],y=g?.value,E=($=m,/\s+on(?<event>\w+)=['"]?$/.exec($)?.groups?.event),h=d.match(/<\w+\s+[^>]*RESOLVE="(?<existingRef>[^"]+)"\s*[^>]*(?:\s*>\s*)?$/),x=h&&h.groups?h.groups.existingRef:void 0,j=x||"#REF"+a++;if(g)if(E){const e=t(g)?g:t(g.next)?g.next.bind(g):null,r=s.has(E);e&&(r||n(E),l(j,{handler:e,type:"event",eventName:E})),u=d+("mount"==E||r?j:"")+(x?"":`" RESOLVE="${j}`)}else if(g.sink)l(j,{handler:g,type:g.sink}),u+=m.replace(/<(\w+)\s*([^>]*)/,`<$1 ${x?"":`RESOLVE="${j}" `}$2`);else if("function"==typeof(g.subscribe??g.then)&&f<e.length-1||"object"==typeof g||g.sink){const t=e[f+1],s=/(?<attribute>[a-z0-9\-_]+)\=(?<quote>['"]?)(?<otherValues>[^"]*)$/.exec(d);if(s&&new RegExp(`^(?:[^>${s.groups?.quote}]*)${s.groups?.quote}`).test(t)){let e=s.groups?.attribute??"";const t="class"==e||"style"==e||"value"==e?e:e.indexOf("data")?"attribute":"dataset";"dataset"==t&&(e=e.replace(/^data-/,""));const n="";l(j,{handler:g,type:t,attribute:e}),u=(d+n).replace(/<(\w+)\s+([^>]+)$/,`<$1 ${x?"":`RESOLVE="${j}" `}$2`)}else if(/<\s*\S+(?:\s+[^=>]+(?:=(?:'[^']*'|"[^"]*"|\S+|[^>]+)))*(?:\s+\.\.\.)?$/.test(d.substring(b))&&/^(?:[^<]*>|\s+\.\.\.)/.test(t))u+=m.replace(/\.\.\.$/,""),l(j,{handler:g,type:"attributeset",attribute:g}),u=u.replace(/<\s*(\w+)\s+([^<]*)$/,`<$1 ${x?"":`RESOLVE="${j}" `}$2`);else if(/>\s*$/.test(m)&&/^\s*<\s*/.test(t)){let e,t;t=m.replace(o,""),e=RegExp.$1||g.sink||"innerHTML",l(j,{handler:y&&g.skip?g.skip(1):g,type:e,error:r,..."collection"==e&&{attribute:g}||{},termination:i}),u+=x?t:t.replace(/\s*>\s*$/,` RESOLVE="${j}">`)+(y||"")}else/>\s*[^<]+$/m.test(m)&&/^\s*[^<]*\s*</m.test(t)?(l(j,{handler:g,type:"textContent"}),u=x?d:u+m.replace(/\s*>/,` RESOLVE="${j}">`)+j):u=d}else if(/\.\.\.$/.test(m))u+=m.substr(0,m.length-3).replace(/\.\.\.$/,""),t(g.next)||t(g.then)?l(j,{handler:g,type:"attributeset",attribute:g}):u+="string"==typeof g?g:Object.entries(g||{}).map((([e,t])=>`${e}="${t}"`)).join(" ");else if(Array.isArray(g))u=d+g.join("");else if("object"==typeof g){const[e,t]=Object.entries(g).reduce((([e,t],[s,n])=>[e.concat(s),t.concat(n)]),[[],[]]);u+=m+c(e,...t)}else u=d+g;else u=d}var $;return u}export{c as default};
//# sourceMappingURL=parser.mjs.map
