<script lang="ts">
  import Icon from '$lib/components/Icon.svelte';
  import { getEnhancedImage } from '$lib/enhanced-images';
  import type { CallToAction, HeroContent } from '$lib/types';

  export let hero: HeroContent;
  export let reviewCta: CallToAction | undefined = undefined;

  $: titleParts = splitTitle(hero.title);
  $: mobileTitle = hero.mobileTitle?.trim() || `${hero.title} ${hero.highlight}`.trim();
  $: mobileLead = mobileTitle.includes(hero.highlight)
    ? mobileTitle.slice(0, mobileTitle.lastIndexOf(hero.highlight))
    : mobileTitle;
  $: heroEnhanced = getEnhancedImage(hero.imageUrl);

  function splitTitle(title: string) {
    const trimmed = title.trim();

    if (trimmed.toLowerCase().endsWith(' in')) {
      return {
        lead: trimmed.slice(0, -3),
        connector: 'in'
      };
    }

    return {
      lead: trimmed,
      connector: ''
    };
  }

  function linkTarget(external?: boolean) {
    return external ? '_blank' : undefined;
  }

  function linkRel(external?: boolean) {
    return external ? 'noopener' : undefined;
  }
</script>

<section id="hero">
  <!-- hero-img is a direct child of #hero so it can be absolutely
       positioned relative to the section on mobile without being
       constrained by hero-inner's stacking context -->
  <div class="hero-img">
    <picture>
      {#if hero.desktopImageUrl}
        <source media="(min-width: 769px)" srcset={hero.desktopImageUrl} />
      {/if}
      <img
        src={hero.imageUrl}
        alt={hero.imageAlt}
        loading="eager"
        fetchpriority="high"
      />
    </picture>
  </div>

  {#if hero.floatingPill}
    <div class="hero-floating-pill">{hero.floatingPill}</div>
  {/if}

  <div class="hero-inner">
    <div class="hero-text">
      {#if hero.kicker}
        <p class="hero-kicker">{hero.kicker}</p>
      {/if}

      <h1 class="hero-heading">
        <span class="hero-heading-desktop">
          <span class="hero-title-main">{titleParts.lead}</span>
          {#if titleParts.connector}
            <span class="hero-title-connector"> {titleParts.connector}</span>
          {/if}
          <br />
          <span class="hero-title-highlight">{hero.highlight}</span>
        </span>
        <span class="hero-heading-mobile">
          {mobileLead}<span class="hero-title-highlight">{hero.highlight}</span>
        </span>
      </h1>

      {#if hero.subtitle}
        <p class="hero-subtitle hero-subtitle-desktop">{hero.subtitle}</p>
      {/if}

      {#if hero.subtitleChips && hero.subtitleChips.length}
        <div class="hero-chips">
          {#each hero.subtitleChips as chip}
            <span class="hero-chip">
              <Icon name={chip.icon} />
              {chip.label}
            </span>
          {/each}
        </div>
      {/if}

      {#if reviewCta}
        <a
          class="hero-trust-chip"
          href={reviewCta.href}
          target={reviewCta.external ? '_blank' : undefined}
          rel={reviewCta.external ? 'noopener' : undefined}
          aria-label="Read our five-star Google reviews"
        >
          <img
            class="hero-trust-logo"
            src="/images/google-g-logo.svg"
            alt=""
            width="18"
            height="19"
          />
          <span class="hero-trust-stars" aria-hidden="true">
            {#each Array(5) as _}
              <Icon name="fas fa-star" />
            {/each}
          </span>
          <span>{reviewCta.label}</span>
        </a>
      {/if}

      <div class="hero-buttons">
        <a
          href={hero.primaryCta.href}
          target={linkTarget(hero.primaryCta.external)}
          rel={linkRel(hero.primaryCta.external)}
          class="btn btn-yellow"
        >
          {hero.primaryCta.label}
        </a>
        <a
          href={hero.secondaryCta.href}
          target={linkTarget(hero.secondaryCta.external)}
          rel={linkRel(hero.secondaryCta.external)}
          class="btn btn-outline"
        >
          {hero.secondaryCta.label}
          <Icon name="fas fa-arrow-down" className="hero-cta-arrow" />
        </a>
      </div>
    </div>
  </div>
</section>
