<script
  module
  lang="ts"
>
/** A run of banner copy for the `announcement` variant. */
export interface SiteBannerSegment {
  /** The run's text. */
  text: string;
  /** Visual emphasis. Omit for default body copy. */
  style?: 'strong' | 'subtle';
  /** When set, this run renders as a link opened in a new tab. */
  href?: string;
}
</script>

<script lang="ts">
import type { ClassValue } from 'svelte/elements';

import { Button } from './button';
import { Icon, type IconName } from './icon';

interface Props {
  /** The text in the banner. Used by the `warning` and `info` variants. */
  value?: string;
  /** Optional variant for visual distinction */
  variant?: 'warning' | 'info' | 'announcement';
  /** When set, the banner text becomes a link to this target, opened in a new tab. */
  href?: string;
  /** Leading badge for the `announcement` variant. Omit to hide the badge. */
  badge?: string;
  /** Styled copy runs for the `announcement` variant. */
  segments?: SiteBannerSegment[];
  /** When provided, a dismiss button is shown and called on click. */
  onDismiss?: () => void;
}
const {
  value,
  variant = 'warning',
  href,
  badge,
  segments,
  onDismiss,
}: Props = $props();

interface VariantProps {
  // The background color of the banner.
  bgColor: ClassValue;
  // The icon name
  iconName: IconName;
  // The color of the icon
  iconColor: ClassValue;
  // The heading that will appear on the top of the banner (optional)
  heading?: string;
  // The anchor text for a trailing hyperlink (optional)
  linkText?: string;
  // The trailing link target that will open in a new tab (optional)
  linkTarget?: string;
}

const variantProps = $derived<VariantProps | undefined>(
  variant === 'announcement'
    ? undefined
    : {
        warning: {
          bgColor: 'bg-warning-light',
          iconName: 'alert' as IconName,
          iconColor: 'text-warning-bright',
          heading: 'We are currently investigating an issue with our platform',
          linkText: 'Viam status',
          linkTarget: 'https://status.viam.com/',
        },
        info: {
          bgColor: 'bg-info-light',
          iconName: 'information' as IconName,
          iconColor: 'text-info-dark',
        },
      }[variant]
);
</script>

{#if variant === 'announcement'}
  <div
    role="banner"
    aria-label="site banner"
    class="text-gray-4 relative isolate flex shrink-0 flex-wrap items-center justify-center gap-x-2 gap-y-0.5 overflow-hidden bg-black px-4 py-3 pr-9 font-mono text-xs sm:px-10"
  >
    <!-- Decorative grid backdrop, faded to the right; not expressible as Tailwind utilities. -->
    <div
      class="pointer-events-none absolute inset-0 -z-10"
      style="
        background-image:
          linear-gradient(to right, rgba(255, 255, 255, 0.12) 1px, transparent 1px),
          linear-gradient(to bottom, rgba(255, 255, 255, 0.12) 1px, transparent 1px);
        background-size: 16px 16px;
        background-position: 0 -8px;
        -webkit-mask-image: linear-gradient(to right, transparent, black);
        mask-image: linear-gradient(to right, transparent, black);
      "
    ></div>
    {#if badge}
      <span
        class="bg-white px-1.5 py-0.5 text-[10px] font-semibold text-black uppercase"
      >
        {badge}
      </span>
    {/if}
    {#each segments ?? [] as segment (segment)}
      {#if segment.href}
        <a
          href={segment.href}
          target="_blank"
          rel="noopener noreferrer"
          class="decoration-gray-7 font-semibold text-white underline underline-offset-4 hover:decoration-white"
        >
          {segment.text}
        </a>
      {:else}
        <span
          class={{
            'font-semibold text-white': segment.style === 'strong',
            'text-gray-7': segment.style === 'subtle',
          }}
        >
          {segment.text}
        </span>
      {/if}
    {/each}
    {#if onDismiss}
      <button
        type="button"
        class="text-gray-6 absolute top-1/2 right-3 -translate-y-1/2 hover:text-white"
        aria-label="Dismiss announcement"
        onclick={onDismiss}
      >
        <Icon name="close" />
      </button>
    {/if}
  </div>
{:else if variantProps}
  <div
    role="banner"
    aria-label="site banner"
    class="{variantProps.bgColor} flex items-center justify-between gap-3 px-3 py-2.5"
  >
    <div class="flex items-center gap-3">
      <Icon
        size="lg"
        name={variantProps.iconName}
        cx={variantProps.iconColor}
      />
      <div class="text-default flex flex-col justify-center text-sm">
        {#if variantProps.heading}
          <p class="font-semibold">{variantProps.heading}</p>
        {/if}
        {#if href}
          <a
            {href}
            target="_blank"
            rel="noopener noreferrer"
            class={[
              'text-link hover:underline',
              {
                'text-xs': variantProps.heading,
                'font-semibold': !variantProps.heading,
              },
            ]}
          >
            {value}
          </a>
        {:else}
          <p
            class={[
              {
                'text-xs': variantProps.heading,
                'font-semibold': !variantProps.heading,
              },
            ]}
          >
            {value}
          </p>
        {/if}
      </div>
    </div>
    {#if variantProps.linkTarget !== undefined || onDismiss !== undefined}
      <div class="flex items-center gap-3">
        {#if variantProps.linkTarget}
          <div class="text-link flex gap-1">
            <a
              class="text-xs hover:underline"
              href={variantProps.linkTarget}
            >
              {variantProps.linkText}
            </a>
            <Icon name="open-in-new" />
          </div>
        {/if}
        {#if onDismiss}
          <Button
            variant="ghost"
            class="text-gray-7"
            aria-label="Dismiss banner"
            onclick={onDismiss}
          >
            <Icon name="close" />
          </Button>
        {/if}
      </div>
    {/if}
  </div>
{/if}
