'use strict';

// src/platform-domains.ts
var PLATFORM_DOMAINS = [
  { key: "marketing-hub", defaultUrl: "https://marketing-hub.flamingo.so", envVar: "NEXT_PUBLIC_MARKETING_HUB_URL" },
  { key: "company-hub", defaultUrl: "https://company-hub.flamingo.so", envVar: "NEXT_PUBLIC_COMPANY_HUB_URL" },
  { key: "product-hub", defaultUrl: "https://product-hub.flamingo.so", envVar: "NEXT_PUBLIC_PRODUCT_HUB_URL" },
  { key: "revenue-hub", defaultUrl: "https://revenue-hub.flamingo.so", envVar: "NEXT_PUBLIC_REVENUE_HUB_URL" },
  { key: "people-hub", defaultUrl: "https://people-hub.flamingo.so", envVar: "NEXT_PUBLIC_PEOPLE_HUB_URL" },
  { key: "openmsp", defaultUrl: "https://www.openmsp.ai", envVar: "NEXT_PUBLIC_OPENMSP_URL" },
  // ORDERING INVARIANT (first-wins, load-bearing): `flamingo` MUST precede `flamingo-teaser` + `universal`.
  // All three resolve to www.flamingo.run; the reverse index is first-wins → flamingo claims the shared host,
  // teaser keeps only its unique flamingo.cx aliases, universal contributes no unique host.
  // ⚠️ DO NOT REORDER — enforced by the module-load self-check at the bottom of this file.
  { key: "flamingo", defaultUrl: "https://www.flamingo.run", envVar: "NEXT_PUBLIC_FLAMINGO_URL" },
  { key: "tmcg", defaultUrl: "https://www.tmcg.miami", envVar: "NEXT_PUBLIC_TMCG_URL" },
  { key: "flamingo-teaser", defaultUrl: "https://www.flamingo.run", envVar: "NEXT_PUBLIC_FLAMINGO_URL", aliasHostnames: ["flamingo.cx", "www.flamingo.cx"] },
  { key: "openframe", defaultUrl: "https://openframe.ai", envVar: "NEXT_PUBLIC_OPENFRAME_URL", aliasHostnames: ["openframe.ai", "www.openframe.ai", "hub.openframe.ai"] },
  { key: "openframe-dashboard", defaultUrl: "https://openframe.ai", envVar: "NEXT_PUBLIC_OPENFRAME_DASHBOARD_URL", pseudo: true },
  { key: "universal", defaultUrl: "https://www.flamingo.run", envVar: "NEXT_PUBLIC_FLAMINGO_URL" }
];
var ENV_OVERRIDES = {
  NEXT_PUBLIC_MARKETING_HUB_URL: process.env.NEXT_PUBLIC_MARKETING_HUB_URL,
  NEXT_PUBLIC_COMPANY_HUB_URL: process.env.NEXT_PUBLIC_COMPANY_HUB_URL,
  NEXT_PUBLIC_PRODUCT_HUB_URL: process.env.NEXT_PUBLIC_PRODUCT_HUB_URL,
  NEXT_PUBLIC_REVENUE_HUB_URL: process.env.NEXT_PUBLIC_REVENUE_HUB_URL,
  NEXT_PUBLIC_PEOPLE_HUB_URL: process.env.NEXT_PUBLIC_PEOPLE_HUB_URL,
  NEXT_PUBLIC_OPENMSP_URL: process.env.NEXT_PUBLIC_OPENMSP_URL,
  NEXT_PUBLIC_FLAMINGO_URL: process.env.NEXT_PUBLIC_FLAMINGO_URL,
  NEXT_PUBLIC_TMCG_URL: process.env.NEXT_PUBLIC_TMCG_URL,
  NEXT_PUBLIC_OPENFRAME_URL: process.env.NEXT_PUBLIC_OPENFRAME_URL,
  NEXT_PUBLIC_OPENFRAME_DASHBOARD_URL: process.env.NEXT_PUBLIC_OPENFRAME_DASHBOARD_URL
};
function byKey(key) {
  return PLATFORM_DOMAINS.find((e) => e.key === key);
}
function envOverrideFor(key) {
  const envVar = byKey(key)?.envVar;
  if (!envVar) return null;
  return ENV_OVERRIDES[envVar] || null;
}
function ensureScheme(url) {
  const trimmed = url.trim();
  if (/^[a-z][a-z0-9+.-]*:\/\//i.test(trimmed)) return trimmed;
  return `https://${trimmed.replace(/^\/+/, "")}`;
}
function getPlatformProductionUrl(platform) {
  const resolved = envOverrideFor(platform) ?? byKey(platform)?.defaultUrl ?? envOverrideFor("flamingo") ?? "https://www.flamingo.run";
  return ensureScheme(resolved);
}
function hostOf(value) {
  if (!value) return null;
  try {
    return new URL(value).hostname.toLowerCase();
  } catch {
    return null;
  }
}
function expandWwwApex(host) {
  if (host.startsWith("www.")) return [host, host.slice(4)];
  if (host.split(".").length === 2) return [host, `www.${host}`];
  return [host];
}
function toRegistrableBaseDomain(host) {
  const parts = host.split(".");
  if (parts.length >= 2) return parts.slice(-2).join(".");
  return void 0;
}
function aliasHostsOf(key) {
  return byKey(key)?.aliasHostnames ?? [];
}
function hostsForEntry(entry, opts) {
  const resolved = hostOf(getPlatformProductionUrl(entry.key));
  const hosts = resolved ? expandWwwApex(resolved) : [];
  hosts.push(...aliasHostsOf(entry.key));
  return hosts;
}
function getPlatformByHostname(hostname) {
  const host = hostname.toLowerCase();
  for (const entry of PLATFORM_DOMAINS) {
    if (entry.pseudo) continue;
    if (hostsForEntry(entry).includes(host)) return entry.key;
  }
  return null;
}
function isPreviewEnv() {
  return process.env.VERCEL_ENV === "preview";
}
function isPreviewHost(hostname) {
  return hostname.endsWith(".vercel.app");
}
function getAllPlatformBaseDomains() {
  if (typeof window === "undefined") return [];
  const hostname = window.location.hostname;
  if (hostname === "localhost" || hostname === "127.0.0.1" || hostname.startsWith("127.")) {
    return [];
  }
  const previewEnv = process.env.VERCEL_ENV === "preview" || process.env.NEXT_PUBLIC_VERCEL_ENV === "preview" || isPreviewHost(hostname);
  if (previewEnv) {
    return [".vercel.app", "vercel.app"];
  }
  const baseDomains = /* @__PURE__ */ new Set();
  for (const entry of PLATFORM_DOMAINS) {
    if (entry.pseudo) continue;
    const host = hostOf(getPlatformProductionUrl(entry.key));
    if (!host) continue;
    const base = toRegistrableBaseDomain(host);
    if (base) {
      baseDomains.add(`.${base}`);
      baseDomains.add(base);
    }
  }
  return Array.from(baseDomains);
}
function isNonCookieableHost(hostname) {
  return hostname === "localhost" || hostname === "127.0.0.1" || hostname.startsWith("127.") || hostname.startsWith("192.168.") || hostname.startsWith("10.") || hostname.includes(".vercel.app");
}
function matchCookieDomain(hostname, baseDomains) {
  for (const domain of baseDomains) {
    const bare = domain.startsWith(".") ? domain.slice(1) : domain;
    if (hostname === bare || hostname.endsWith(`.${bare}`)) {
      return domain.startsWith(".") ? domain : `.${domain}`;
    }
  }
  return void 0;
}
var _orderIdx = (k) => PLATFORM_DOMAINS.findIndex((e) => e.key === k);
if (_orderIdx("flamingo") > _orderIdx("flamingo-teaser") || _orderIdx("flamingo") > _orderIdx("universal") || getPlatformByHostname("flamingo.cx") !== "flamingo-teaser" || getPlatformByHostname("hub.openframe.ai") !== "openframe") {
  console.error(
    "[platform-domains] \u26A0\uFE0F PLATFORM_DOMAINS ordering invariant violated \u2014 `flamingo` must precede `flamingo-teaser`/`universal`, and the openframe/teaser aliases must be intact. Do not reorder the table."
  );
}

exports.PLATFORM_DOMAINS = PLATFORM_DOMAINS;
exports.aliasHostsOf = aliasHostsOf;
exports.byKey = byKey;
exports.ensureScheme = ensureScheme;
exports.expandWwwApex = expandWwwApex;
exports.getAllPlatformBaseDomains = getAllPlatformBaseDomains;
exports.getPlatformByHostname = getPlatformByHostname;
exports.getPlatformProductionUrl = getPlatformProductionUrl;
exports.hostOf = hostOf;
exports.isNonCookieableHost = isNonCookieableHost;
exports.isPreviewEnv = isPreviewEnv;
exports.isPreviewHost = isPreviewHost;
exports.matchCookieDomain = matchCookieDomain;
exports.toRegistrableBaseDomain = toRegistrableBaseDomain;
//# sourceMappingURL=platform-domains.cjs.map
//# sourceMappingURL=platform-domains.cjs.map