/** Stored slug for the site homepage (admin enters `/`). */
export const HOMEPAGE_SLUG = "";

declare global {
  interface Window {
    __FORCE_NOT_FOUND__?: boolean;
  }
}

/** Set by the server when REQUEST_URI was malformed but the SPA shell is still served. */
export function isServerForcedNotFound(): boolean {
  return typeof window !== "undefined" && window.__FORCE_NOT_FOUND__ === true;
}

/** True when a browser path must not be resolved to CMS content. */
export function isMalformedUrlPath(pathname: string): boolean {
  if (!pathname) {
    return true;
  }

  if (!pathname.startsWith("/")) {
    return true;
  }

  if (pathname.includes("//")) {
    return true;
  }

  return false;
}

/** Normalize a page slug for URLs and API lookups. `/` becomes the homepage slug. */
export function normalizePageSlug(slug: string): string {
  const trimmed = slug.trim();
  if (trimmed === "" || trimmed === "/") {
    return HOMEPAGE_SLUG;
  }
  return trimmed.replace(/^\/+|\/+$/g, "");
}

export function isHomepageSlug(slug: string): boolean {
  return slug.trim() === "/";
}

/** Slug value sent to the admin API (homepage is stored as empty string). */
export function slugForApiPayload(slug: string): string | undefined {
  if (isHomepageSlug(slug)) {
    return HOMEPAGE_SLUG;
  }
  const normalized = normalizePageSlug(slug);
  return normalized || undefined;
}

/** Display slug in the admin UI. */
export function slugForDisplay(storedSlug: string): string {
  return storedSlug === HOMEPAGE_SLUG ? "/" : storedSlug;
}

/** Build the public API path for a CMS page slug (supports nested paths). */
export function pageBySlugApiPath(slug: string): string {
  if (isHomepageSlug(slug) || normalizePageSlug(slug) === HOMEPAGE_SLUG) {
    return "/api/pages/home";
  }
  const normalized = normalizePageSlug(slug);
  return `/api/pages/by-slug/${normalized.split("/").map(encodeURIComponent).join("/")}`;
}

/** Build the preview API path (draft or published). */
export function pagePreviewApiPath(slug: string): string {
  if (isHomepageSlug(slug) || normalizePageSlug(slug) === HOMEPAGE_SLUG) {
    return "/api/pages/preview/home";
  }
  const normalized = normalizePageSlug(slug);
  return `/api/pages/preview/${normalized.split("/").map(encodeURIComponent).join("/")}`;
}

/** Resolve API path from the current browser pathname. */
export function pageApiPathFromPathname(pathname: string): string | null {
  if (isMalformedUrlPath(pathname)) {
    return null;
  }

  const path = pathname.replace(/\/+$/, "") || "/";
  if (path === "/") {
    return "/api/pages/home";
  }
  return pageBySlugApiPath(normalizePageSlug(path));
}

export function pagePreviewApiPathFromPathname(pathname: string): string | null {
  if (isMalformedUrlPath(pathname)) {
    return null;
  }

  const previewPath = pathname.replace(/^\/preview\/?/, "");
  if (!previewPath || previewPath === "/") {
    return "/api/pages/preview/home";
  }

  const slugPath = previewPath.startsWith("/") ? previewPath : `/${previewPath}`;
  if (isMalformedUrlPath(slugPath)) {
    return null;
  }

  return pagePreviewApiPath(previewPath);
}
