---
const webcamBase = (import.meta.env.PUBLIC_WEATHER_BASE ?? 'https://wx.swansea-airport.wales').replace(/\/$/, '');
const webcamImage = `${webcamBase}/webcam/apron.jpg`;
---

<section class="webcam-shell" aria-labelledby="webcam-heading">
  <div class="webcam-head">
    <div>
      <p class="eyebrow">Live apron webcam</p>
      <h1 id="webcam-heading" class="section-title">Swansea Airport apron</h1>
      <p class="section-copy">
        A live view across the apron. The image refreshes automatically every 30 seconds.
      </p>
    </div>
    <div class="webcam-status" aria-live="polite">
      <span class="status-dot waiting" id="webcam-dot"></span>
      <span id="webcam-status">Loading</span>
    </div>
  </div>

  <figure class="webcam-frame">
    <img
      id="webcam-image"
      src={webcamImage}
      alt="Live webcam view of Swansea Airport apron"
      decoding="async"
      fetchpriority="high"
    />
    <figcaption>
      <span id="webcam-updated">Waiting for latest image</span>
    </figcaption>
  </figure>
</section>

<script define:vars={{ webcamImage }}>
  (() => {
    const REFRESH_MS = 30000;
    const image = document.getElementById('webcam-image');
    const status = document.getElementById('webcam-status');
    const dot = document.getElementById('webcam-dot');
    const updated = document.getElementById('webcam-updated');

    function setStatus(text, className) {
      status.textContent = text;
      dot.className = `status-dot ${className}`;
    }

    function refreshImage() {
      const next = new URL(webcamImage);
      next.searchParams.set('t', Date.now().toString());
      image.src = next.toString();
    }

    image.addEventListener('load', () => {
      const time = new Intl.DateTimeFormat('en-GB', {
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit',
      }).format(new Date());

      setStatus('Live', 'ok');
      updated.textContent = `Last refreshed ${time}`;
    });

    image.addEventListener('error', () => {
      setStatus('Image unavailable', 'error');
      updated.textContent = 'The webcam image could not be loaded.';
    });

    refreshImage();
    window.setInterval(refreshImage, REFRESH_MS);
  })();
</script>

<style>
  .webcam-shell {
    display: grid;
    gap: 1rem;
    padding: 1.5rem 0 2.5rem;
  }

  .webcam-head {
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    align-items: start;
  }

  .webcam-status {
    display: inline-flex;
    align-items: center;
    gap: 0.45rem;
    flex: 0 0 auto;
    padding: 0.45rem 0.7rem;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.78);
    color: var(--muted);
    font-size: 0.84rem;
    font-weight: 800;
    box-shadow: 0 10px 22px rgba(16, 34, 51, 0.06);
  }

  .status-dot {
    width: 0.62rem;
    height: 0.62rem;
    border-radius: 999px;
    background: var(--muted);
    box-shadow: 0 0 0 0.25rem rgba(81, 100, 117, 0.14);
  }

  .status-dot.ok {
    background: #13834f;
    box-shadow: 0 0 0 0.25rem rgba(19, 131, 79, 0.15);
  }

  .status-dot.waiting {
    background: var(--warning);
    box-shadow: 0 0 0 0.25rem rgba(187, 104, 0, 0.15);
  }

  .status-dot.error {
    background: var(--critical);
    box-shadow: 0 0 0 0.25rem rgba(161, 31, 58, 0.14);
  }

  .webcam-frame {
    margin: 0;
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    background: rgba(255, 255, 255, 0.84);
    box-shadow: var(--shadow);
  }

  .webcam-frame img {
    width: 100%;
    aspect-ratio: 16 / 9;
    object-fit: cover;
    background: rgba(16, 34, 51, 0.08);
  }

  .webcam-frame figcaption {
    display: block;
    padding: 0.8rem 1rem;
    border-top: 1px solid var(--line);
    color: var(--muted);
    font-size: 0.9rem;
    font-weight: 700;
  }

  @media (max-width: 720px) {
    .webcam-head {
      display: grid;
    }

    .webcam-status {
      justify-self: start;
    }
  }
</style>
