/**
 * Shared helpers for building the Sitecore page render URL that the editor's page-viewer iframe
 * loads. Extracted from PageViewerFrame so the background/offscreen capture flow can build the same
 * URL for a page that isn't currently open in the editor.
 */
export function isLoopbackHost(hostname) {
    return hostname === "localhost" || hostname === "127.0.0.1";
}
export function alignLoopbackHostToEditor(url) {
    if (isLoopbackHost(window.location.hostname) &&
        isLoopbackHost(url.hostname)) {
        // Sitecore auth cookies are host-scoped, so localhost and 127.0.0.1 are
        // different origins even when they point at the same machine.
        url.hostname = window.location.hostname;
    }
}
export function isXmCloudManagedEditingHost(url) {
    return url.hostname.endsWith("-eh.sitecorecloud.io");
}
export function alignXmCloudManagedEditingHostToEditor(url) {
    if (window.location.hostname.endsWith(".sitecorecloud.io") &&
        isXmCloudManagedEditingHost(url)) {
        url.protocol = window.location.protocol;
        url.hostname = window.location.hostname;
        url.port = window.location.port;
    }
}
export function getEditorQueryParam(name) {
    try {
        return new URLSearchParams(window.location.search).get(name) || undefined;
    }
    catch {
        return undefined;
    }
}
export function inferRoutePathFromSitecoreItemPath(itemPath) {
    const segments = (itemPath || "")
        .split("/")
        .map((segment) => segment.trim())
        .filter(Boolean);
    const homeIndex = segments
        .map((segment) => segment.toLowerCase())
        .lastIndexOf("home");
    if (homeIndex < 0)
        return undefined;
    const routeSegments = segments.slice(homeIndex + 1);
    if (routeSegments.length === 0)
        return "/";
    return `/${routeSegments.map(encodeURIComponent).join("/")}`;
}
export function isRootRenderPath(pathname) {
    return !pathname || pathname === "/";
}
export function applyParheliaRenderContext(renderUrl, pageItemDescriptor, siteName) {
    if (siteName) {
        if (!renderUrl.searchParams.has("site")) {
            renderUrl.searchParams.set("site", siteName);
        }
        // Same-origin Sitecore fallback URLs use sc_site for site resolution and can
        // canonical-redirect to the public site host before the editor iframe loads.
        if (renderUrl.origin !== window.location.origin &&
            !renderUrl.searchParams.has("sc_site")) {
            renderUrl.searchParams.set("sc_site", siteName);
        }
    }
    // Proxied rendering-host requests (/parhelia/proxy/{name}/...) must not carry
    // sc_* context params: they run through Sitecore's httpRequestBegin pipeline
    // on the shell site, where language resolvers reject languages that are not
    // registered for that site (500 before the proxy controller runs). The host
    // app reads the plain itemid/lang/version params instead.
    const isProxiedHostPath = renderUrl.pathname.startsWith("/parhelia/proxy/");
    if (isProxiedHostPath) {
        renderUrl.searchParams.delete("sc_itemid");
        renderUrl.searchParams.delete("sc_lang");
        renderUrl.searchParams.delete("sc_version");
        if (!renderUrl.searchParams.has("itemid")) {
            renderUrl.searchParams.set("itemid", pageItemDescriptor.id);
        }
        if (!renderUrl.searchParams.has("lang")) {
            renderUrl.searchParams.set("lang", pageItemDescriptor.language);
        }
        if (!renderUrl.searchParams.has("version")) {
            renderUrl.searchParams.set("version", String(pageItemDescriptor.version));
        }
    }
    else {
        if (!renderUrl.searchParams.has("sc_itemid")) {
            renderUrl.searchParams.set("sc_itemid", pageItemDescriptor.id);
        }
        if (!renderUrl.searchParams.has("sc_lang")) {
            renderUrl.searchParams.set("sc_lang", pageItemDescriptor.language);
        }
        if (!renderUrl.searchParams.has("sc_version")) {
            renderUrl.searchParams.set("sc_version", String(pageItemDescriptor.version));
        }
    }
    const routePath = inferRoutePathFromSitecoreItemPath(pageItemDescriptor.path);
    if (!routePath)
        return;
    if (!renderUrl.searchParams.has("route")) {
        renderUrl.searchParams.set("route", routePath);
    }
    if (isRootRenderPath(renderUrl.pathname)) {
        renderUrl.pathname = routePath;
    }
}
//# sourceMappingURL=renderUrl.js.map