import{b as j}from"./index-3fevySbJ.js";import{n as R,a as M,U,b as L,V as z,h as C,H as P,e as I,v as $}from"./index-kIfVvmeP.js";function T(o,e={},t){for(const r in o){const c=o[r],d=t?`${t}:${r}`:r;typeof c=="object"&&c!==null?T(c,e,d):typeof c=="function"&&(e[d]=c)}return e}const D={run:o=>o()},F=()=>D,O=typeof console.createTask<"u"?console.createTask:F;function G(o,e){const t=e.shift(),r=O(t);return o.reduce((c,d)=>c.then(()=>r.run(()=>d(...e))),Promise.resolve())}function N(o,e){const t=e.shift(),r=O(t);return Promise.all(o.map(c=>r.run(()=>c(...e))))}function v(o,e){for(const t of[...o])t(e)}class q{constructor(){this._hooks={},this._before=void 0,this._after=void 0,this._deprecatedMessages=void 0,this._deprecatedHooks={},this.hook=this.hook.bind(this),this.callHook=this.callHook.bind(this),this.callHookWith=this.callHookWith.bind(this)}hook(e,t,r={}){if(!e||typeof t!="function")return()=>{};const c=e;let d;for(;this._deprecatedHooks[e];)d=this._deprecatedHooks[e],e=d.to;if(d&&!r.allowDeprecated){let p=d.message;p||(p=`${c} hook has been deprecated`+(d.to?`, please use ${d.to}`:"")),this._deprecatedMessages||(this._deprecatedMessages=new Set),this._deprecatedMessages.has(p)||(console.warn(p),this._deprecatedMessages.add(p))}if(!t.name)try{Object.defineProperty(t,"name",{get:()=>"_"+e.replace(/\W+/g,"_")+"_hook_cb",configurable:!0})}catch{}return this._hooks[e]=this._hooks[e]||[],this._hooks[e].push(t),()=>{t&&(this.removeHook(e,t),t=void 0)}}hookOnce(e,t){let r,c=(...d)=>(typeof r=="function"&&r(),r=void 0,c=void 0,t(...d));return r=this.hook(e,c),r}removeHook(e,t){if(this._hooks[e]){const r=this._hooks[e].indexOf(t);r!==-1&&this._hooks[e].splice(r,1),this._hooks[e].length===0&&delete this._hooks[e]}}deprecateHook(e,t){this._deprecatedHooks[e]=typeof t=="string"?{to:t}:t;const r=this._hooks[e]||[];delete this._hooks[e];for(const c of r)this.hook(e,c)}deprecateHooks(e){Object.assign(this._deprecatedHooks,e);for(const t in e)this.deprecateHook(t,e[t])}addHooks(e){const t=T(e),r=Object.keys(t).map(c=>this.hook(c,t[c]));return()=>{for(const c of r.splice(0,r.length))c()}}removeHooks(e){const t=T(e);for(const r in t)this.removeHook(r,t[r])}removeAllHooks(){for(const e in this._hooks)delete this._hooks[e]}callHook(e,...t){return t.unshift(e),this.callHookWith(G,e,...t)}callHookParallel(e,...t){return t.unshift(e),this.callHookWith(N,e,...t)}callHookWith(e,t,...r){const c=this._before||this._after?{name:t,args:r,context:{}}:void 0;this._before&&v(this._before,c);const d=e(t in this._hooks?[...this._hooks[t]]:[],r);return d instanceof Promise?d.finally(()=>{this._after&&c&&v(this._after,c)}):(this._after&&c&&v(this._after,c),d)}beforeEach(e){return this._before=this._before||[],this._before.push(e),()=>{if(this._before!==void 0){const t=this._before.indexOf(e);t!==-1&&this._before.splice(t,1)}}}afterEach(e){return this._after=this._after||[],this._after.push(e),()=>{if(this._after!==void 0){const t=this._after.indexOf(e);t!==-1&&this._after.splice(t,1)}}}}function K(){return new q}const E=(o,e)=>o._w===e._w?o._p-e._p:o._w-e._w,A={base:-10,title:10},V={critical:-8,high:-1,low:2},S={meta:{"content-security-policy":-30,charset:-20,viewport:-15},link:{preconnect:20,stylesheet:60,preload:70,modulepreload:70,prefetch:90,"dns-prefetch":90,prerender:90},script:{async:30,defer:80,sync:50},style:{imported:40,sync:60}},J=/@import/,H=o=>o===""||o===!0;function B(o,e){if(typeof e.tagPriority=="number")return e.tagPriority;let t=100;const r=V[e.tagPriority]||0,c=o.resolvedOptions.disableCapoSorting?{link:{},script:{},style:{}}:S;if(e.tag in A)t=A[e.tag];else if(e.tag==="meta"){const d=e.props["http-equiv"]==="content-security-policy"?"content-security-policy":e.props.charset?"charset":e.props.name==="viewport"?"viewport":null;d&&(t=S.meta[d])}else e.tag==="link"&&e.props.rel?t=c.link[e.props.rel]:e.tag==="script"?H(e.props.async)?t=c.script.async:e.props.src&&!H(e.props.defer)&&!H(e.props.async)&&e.props.type!=="module"&&!e.props.type?.endsWith("json")?t=c.script.sync:H(e.props.defer)&&e.props.src&&!H(e.props.async)&&(t=c.script.defer):e.tag==="style"&&(t=e.innerHTML&&J.test(e.innerHTML)?c.style.imported:c.style.sync);return(t||100)+r}function x(o,e){const t=typeof e=="function"?e(o):e,r=t.key||String(o.plugins.size+1);o.plugins.get(r)||(o.plugins.set(r,t),o.hooks.addHooks(t.hooks||{}))}function Q(o={}){const e=K();e.addHooks(o.hooks||{});const t=!o.document,r=new Map,c=new Map,d=new Set,p={_entryCount:1,plugins:c,dirty:!1,resolvedOptions:o,hooks:e,ssr:t,entries:r,headEntries(){return[...r.values()]},use:a=>x(p,a),push(a,m){const y={...m||{}};delete y.head;const g=y._index??p._entryCount++,k={_i:g,input:a,options:y},l={_poll(s=!1){p.dirty=!0,!s&&d.add(g),e.callHook("entries:updated",p)},dispose(){r.delete(g)&&p.invalidate()},patch(s){(!y.mode||y.mode==="server"&&t||y.mode==="client"&&!t)&&(k.input=s,r.set(g,k),l._poll())}};return l.patch(a),l},async resolveTags(){const a={tagMap:new Map,tags:[],entries:[...p.entries.values()]};for(await e.callHook("entries:resolve",a);d.size;){const l=d.values().next().value;d.delete(l);const s=r.get(l);if(s){const n={tags:R(s.input,o.propResolvers||[]).map(i=>Object.assign(i,s.options)),entry:s};await e.callHook("entries:normalize",n),s._tags=n.tags.map((i,u)=>(i._w=B(p,i),i._p=(s._i<<10)+u,i._d=M(i),i))}}let m=!1;a.entries.flatMap(l=>(l._tags||[]).map(s=>({...s,props:{...s.props}}))).sort(E).reduce((l,s)=>{const n=String(s._d||s._p);if(!l.has(n))return l.set(n,s);const i=l.get(n);if((s?.tagDuplicateStrategy||(U.has(s.tag)?"merge":null)||(s.key&&s.key===i.key?"merge":null))==="merge"){const f={...i.props};Object.entries(s.props).forEach(([h,_])=>f[h]=h==="style"?new Map([...i.props.style||new Map,..._]):h==="class"?new Set([...i.props.class||new Set,..._]):_),l.set(n,{...s,props:f})}else s._p>>10===i._p>>10&&s.tag==="meta"&&L(n)?(l.set(n,Object.assign([...Array.isArray(i)?i:[i],s],s)),m=!0):(s._w===i._w?s._p>i._p:s?._w<i?._w)&&l.set(n,s);return l},a.tagMap);const y=a.tagMap.get("title"),g=a.tagMap.get("titleTemplate");if(p._title=y?.textContent,g){const l=g?.textContent;if(p._titleTemplate=l,l){let s=typeof l=="function"?l(y?.textContent):l;typeof s=="string"&&!p.plugins.has("template-params")&&(s=s.replace("%s",y?.textContent||"")),y?s===null?a.tagMap.delete("title"):a.tagMap.set("title",{...y,textContent:s}):(g.tag="title",g.textContent=s)}}a.tags=Array.from(a.tagMap.values()),m&&(a.tags=a.tags.flat().sort(E)),await e.callHook("tags:beforeResolve",a),await e.callHook("tags:resolve",a),await e.callHook("tags:afterResolve",a);const k=[];for(const l of a.tags){const{innerHTML:s,tag:n,props:i}=l;if(z.has(n)&&!(Object.keys(i).length===0&&!l.innerHTML&&!l.textContent)&&!(n==="meta"&&!i.content&&!i["http-equiv"]&&!i.charset)){if(n==="script"&&s){if(i.type?.endsWith("json")){const u=typeof s=="string"?s:JSON.stringify(s);l.innerHTML=u.replace(/</g,"\\u003C")}else typeof s=="string"&&(l.innerHTML=s.replace(new RegExp(`</${n}`,"g"),`<\\/${n}`));l._d=M(l)}k.push(l)}}return k},invalidate(){for(const a of r.values())d.add(a._i);p.dirty=!0,e.callHook("entries:updated",p)}};return(o?.plugins||[]).forEach(a=>x(p,a)),p.hooks.callHook("init",p),o.init?.forEach(a=>a&&p.push(a)),p}async function W(o,e={}){const t=e.document||o.resolvedOptions.document;if(!t||!o.dirty)return;const r={shouldRender:!0,tags:[]};if(await o.hooks.callHook("dom:beforeRender",r),!!r.shouldRender)return o._domUpdatePromise||(o._domUpdatePromise=new Promise(async c=>{const d=new Map,p=new Promise(s=>{o.resolveTags().then(n=>{s(n.map(i=>{const u=d.get(i._d)||0,f={tag:i,id:(u?`${i._d}:${u}`:i._d)||C(i),shouldRender:!0};return i._d&&L(i._d)&&d.set(i._d,u+1),f}))})});let a=o._dom;if(!a){a={title:t.title,elMap:new Map().set("htmlAttrs",t.documentElement).set("bodyAttrs",t.body)};for(const s of["body","head"]){const n=t[s]?.children;for(const i of n){const u=i.tagName.toLowerCase();if(!P.has(u))continue;const f=I({tag:u,props:{}},{innerHTML:i.innerHTML,...i.getAttributeNames().reduce((h,_)=>(h[_]=i.getAttribute(_),h),{})||{}});if(f.key=i.getAttribute("data-hid")||void 0,f._d=M(f)||C(f),a.elMap.has(f._d)){let h=1,_=f._d;for(;a.elMap.has(_);)_=`${f._d}:${h++}`;a.elMap.set(_,i)}else a.elMap.set(f._d,i)}}}a.pendingSideEffects={...a.sideEffects},a.sideEffects={};function m(s,n,i){const u=`${s}:${n}`;a.sideEffects[u]=i,delete a.pendingSideEffects[u]}function y({id:s,$el:n,tag:i}){const u=i.tag.endsWith("Attrs");a.elMap.set(s,n),u||(i.textContent&&i.textContent!==n.textContent&&(n.textContent=i.textContent),i.innerHTML&&i.innerHTML!==n.innerHTML&&(n.innerHTML=i.innerHTML),m(s,"el",()=>{n?.remove(),a.elMap.delete(s)}));for(const f in i.props){if(!Object.prototype.hasOwnProperty.call(i.props,f))continue;const h=i.props[f];if(f.startsWith("on")&&typeof h=="function"){const b=n?.dataset;if(b&&b[`${f}fired`]){const w=f.slice(0,-5);h.call(n,new Event(w.substring(2)))}n.getAttribute(`data-${f}`)!==""&&((i.tag==="bodyAttrs"?t.defaultView:n).addEventListener(f.substring(2),h.bind(n)),n.setAttribute(`data-${f}`,""));continue}const _=`attr:${f}`;if(f==="class"){if(!h)continue;for(const b of h)u&&m(s,`${_}:${b}`,()=>n.classList.remove(b)),!n.classList.contains(b)&&n.classList.add(b)}else if(f==="style"){if(!h)continue;for(const[b,w]of h)m(s,`${_}:${b}`,()=>{n.style.removeProperty(b)}),n.style.setProperty(b,w)}else h!==!1&&h!==null&&(n.getAttribute(f)!==h&&n.setAttribute(f,h===!0?"":String(h)),u&&m(s,_,()=>n.removeAttribute(f)))}}const g=[],k={bodyClose:void 0,bodyOpen:void 0,head:void 0},l=await p;for(const s of l){const{tag:n,shouldRender:i,id:u}=s;if(i){if(n.tag==="title"){t.title=n.textContent,m("title","",()=>t.title=a.title);continue}s.$el=s.$el||a.elMap.get(u),s.$el?y(s):P.has(n.tag)&&g.push(s)}}for(const s of g){const n=s.tag.tagPosition||"head";s.$el=t.createElement(s.tag.tag),y(s),k[n]=k[n]||t.createDocumentFragment(),k[n].appendChild(s.$el)}for(const s of l)await o.hooks.callHook("dom:renderTag",s,t,m);k.head&&t.head.appendChild(k.head),k.bodyOpen&&t.body.insertBefore(k.bodyOpen,t.body.firstChild),k.bodyClose&&t.body.appendChild(k.bodyClose);for(const s in a.pendingSideEffects)a.pendingSideEffects[s]();o._dom=a,await o.hooks.callHook("dom:rendered",{renders:l}),c()}).finally(()=>{o._domUpdatePromise=void 0,o.dirty=!1})),o._domUpdatePromise}function X(o={}){const e=o.domOptions?.render||W;o.document=o.document||(typeof window<"u"?document:void 0);const t=o.document?.head.querySelector('script[id="unhead:payload"]')?.innerHTML||!1;return Q({...o,plugins:[...o.plugins||[],{key:"client",hooks:{"entries:updated":e}}],init:[t?JSON.parse(t):!1,...o.init||[]]})}function Y(o,e){let t=0;return()=>{const r=++t;e(()=>{t===r&&o()})}}function Z(o={}){const e=X({domOptions:{render:Y(()=>W(e),t=>setTimeout(t,0))},...o});return e.install=$(e),e}const se=j(({app:o})=>{const e=Z();o.use(e)});export{se as default};