/**
 * Single place that defines default primary and secondary CTA URLs.
 * Primary = external management (booking) URL when present, else /contact.
 * Secondary = /contact.
 */

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 CtaUrlOverrides {
  primaryHrefOverride?: string | null;
  secondaryHrefOverride?: string | null;
}

export interface CtaButtonLike {
  href?: string | null;
  secondary_href?: string | null;
}

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

/**
 * Resolve primary and secondary CTA hrefs from company info and optional overrides.
 * - primaryHref = external_management_url (if set) else /contact
 * - secondaryHref = /contact
 * Overrides win when provided.
 */
export function resolveCtaUrls(
  companyInformation?: CompanyInformation | null,
  _ctaButton?: CtaButtonLike | null,
  overrides?: CtaUrlOverrides | null
): ResolvedCtaUrls {
  const externalUrl = companyInformation?.external_management_url?.trim() || null;
  const hasSecondary = !!externalUrl;

  const primaryHref =
    overrides?.primaryHrefOverride !== undefined && overrides.primaryHrefOverride !== null
      ? overrides.primaryHrefOverride
      : (externalUrl || CONTACT_PATH);

  const secondaryHref =
    overrides?.secondaryHrefOverride !== undefined && overrides.secondaryHrefOverride !== null
      ? overrides.secondaryHrefOverride
      : CONTACT_PATH;

  return {
    primaryHref,
    secondaryHref,
    hasSecondary,
  };
}
