/**
 * Strapi runtime configuration.
 *
 * Values are read from environment variables. When
 * `NEXT_PUBLIC_STRAPI_URL` is not set (or empty) the whole CMS layer
 * is considered "disabled" and every fetcher gracefully returns null,
 * letting the UI fall back to its bundled default content.
 */

const RAW_URL = process.env.NEXT_PUBLIC_STRAPI_URL ?? ""

function normaliseUrl(url: string): string {
  if (!url) return ""
  return url.replace(/\/+$/, "")
}

export const strapiConfig = {
  /** Base URL of the Strapi backend (no trailing slash). Empty when disabled. */
  url: normaliseUrl(RAW_URL),
  /** REST API prefix, defaults to `/api`. */
  apiPrefix: process.env.NEXT_PUBLIC_STRAPI_API_PREFIX || "/api",
  /** Optional read-only token for protected CMS content fetches. */
  readToken: process.env.STRAPI_READ_TOKEN || "",
  /** Write token used by server-side form submissions. */
  token: process.env.STRAPI_TOKEN || "",
  /** ISR revalidate window in seconds. */
  revalidate: Number(process.env.STRAPI_REVALIDATE_SECONDS ?? 60),
} as const

/** True when a Strapi URL is configured and the integration is active. */
export const isStrapiEnabled = strapiConfig.url.length > 0
