(function installStandaloneRouting(globalScope) {
  function folderUrl(fileUrl) {
    return new URL('./', fileUrl).href;
  }

  function isNestedDeployment(pageUrl) {
    return new URL('./', pageUrl).pathname !== '/';
  }

  function mapRequestUrl(requestUrl, standaloneRootUrl) {
    const request = new URL(requestUrl);
    const root = new URL(standaloneRootUrl);
    if (request.origin !== root.origin || request.href.startsWith(root.href)) {
      return null;
    }
    const relativePath = request.pathname.replace(/^\/+/, '');
    const mapped = new URL(relativePath, root);
    mapped.search = request.search;
    mapped.hash = request.hash;
    return mapped.href;
  }

  globalScope.LazySundayStandaloneRouting = Object.freeze({
    folderUrl,
    isNestedDeployment,
    mapRequestUrl,
  });
})(globalThis);
