<script lang="ts">
  import type { ActivityEntry } from '$lib/stores/activity'
  import Spinner from './Spinner.svelte'
  import CheckIcon from '@lucide/svelte/icons/check'
  import XIcon from '@lucide/svelte/icons/x'

  let { active = false, lastActivity = null as ActivityEntry | null, error = '' }: { active?: boolean; lastActivity?: ActivityEntry | null; error?: string } = $props()

  let done = $state(false)
  let wasActive = $state(false)

  $effect(() => {
    if (active) { done = false; wasActive = true }
    if (!active && wasActive) {
      done = true
      const t = setTimeout(() => { done = false; wasActive = false }, 3000)
      return () => clearTimeout(t)
    }
  })

  const label = $derived.by(() => {
    if (error) return error
    if (!active && done) return 'Done'
    if (lastActivity) return lastActivity.description
    return 'Agent is thinking…'
  })
</script>

{#if active || done || error}
  <div class="flex items-center gap-2 py-2 text-xs transition-opacity {error ? 'text-destructive' : done ? 'text-success opacity-50' : 'text-muted-foreground'}">
    <span class="shrink-0">
      {#if error}
        <XIcon class="size-3" />
      {:else if done}
        <CheckIcon class="size-3" />
      {:else}
        <Spinner class="size-3 text-primary" />
      {/if}
    </span>
    <span>{label}</span>
  </div>
{/if}
