/**
 * Resolves the primary and secondary CTA hrefs used across the site.
 *
 * Resolution order for primaryHref:
 *   1. portal_url            — set by backend when account has consumer portal enabled
 *   2. external_management_url — booking platform URL from the API
 *   3. /contact               — final fallback
 *
 * No per-site configuration needed. Enable the portal in the admin console and
 * every CTA across the site automatically routes to the portal.
 */

import type { CompanyInformation } from '../types/api/company-information';

const CONTACT_PATH = '/contact';

/** True when the href is absolute (external); use for target="_blank" and rel="noopener noreferrer". */
export function isExternalCtaUrl(href: string): boolean {
  return href.startsWith('http://') || href.startsWith('https://');
}

export interface ResolvedCtaUrls {
  primaryHref: string;
  secondaryHref: string;
  hasSecondary: boolean;
}

export function resolveCtaUrls(
  companyInformation?: CompanyInformation | null
): ResolvedCtaUrls {
  const portalUrl = companyInformation?.portal_url?.trim() || null;
  const externalUrl = companyInformation?.external_management_url?.trim() || null;
  const primaryHref = portalUrl ?? externalUrl ?? CONTACT_PATH;
  const secondaryHref = CONTACT_PATH;
  const hasSecondary = primaryHref !== CONTACT_PATH;

  return { primaryHref, secondaryHref, hasSecondary };
}
