import{r as registerInstance,h as h$1,c as Host}from"./index-38ad4957.js";import{b as browser,k as useState,i as d,f as useEffect,n as h}from"./dom-context-hooks.module-50442785.js";import{_}from"./index.module-b23fdad8.js";import{g as getProps}from"./utils-334c1e34.js";import{p as pathToRegexp}from"./index-ffa26b43.js";var debug=browser("sq:useRouter");function matchPath(e,t){if(!e)return;var r=pathToRegexp(e);return r.exec(t)}function useContextRouter(e){var t=_();var r=useState(undefined),n=r[0],o=r[1];var a=useState(undefined),s=a[0],i=a[1];var u=e.contextName;var l=d(u);useEffect((function(){var e=t.querySelectorAll("sqm-route");var r=Array.from(e);r.forEach((function(e){var t=document.createElement("template");t.setAttribute("path",e.path);t.innerHTML=e.innerHTML;e.parentNode.appendChild(t);e.parentNode.removeChild(e)}))}),[]);useEffect((function(){var e,t;if(!s||!n){debug("DOM not ready for navigation rendering on:",l);return}var r=n.querySelectorAll("template");var o=Array.from(r);var a=o.find((function(e){var t,r,n;var o=(r=(t=e.attributes)===null||t===void 0?void 0:t.path)===null||r===void 0?void 0:r.nodeValue;if((n=matchPath(o,l))===null||n===void 0?void 0:n.length)return e}));var i=(t=(e=a===null||a===void 0?void 0:a.attributes)===null||e===void 0?void 0:e.path)===null||t===void 0?void 0:t.nodeValue;debug({containerDatasetPage:s.dataset.page,templatePath:i,page:l});if(!a){debug("No matching page found for ",l,", so render nothing");s.innerHTML="";s.dataset.page=l;return}debug("Page updated to ",l,a);if(!!matchPath(i,s.dataset.page)){debug("don't rerender")}else if(a){s.innerHTML=a.innerHTML;s.dataset.page=l}}),[n,s,l]);return{callbacks:{setSlot:o,setContainer:i}}}var sqmContextRouterCss="sqm-router{display:contents}";var SqmContextRouter=function(){function e(e){registerInstance(this,e);this.ignored=true;h(this)}e.prototype.disconnectedCallback=function(){};e.prototype.render=function(){var e=useContextRouter(getProps(this)).callbacks;return h$1(Host,null,h$1("div",{ref:e.setSlot,style:{display:"none"}},h$1("slot",null)),h$1("div",{style:{display:"contents"},ref:e.setContainer}))};return e}();SqmContextRouter.style=sqmContextRouterCss;export{SqmContextRouter as sqm_context_router};