<script lang="ts">
  import {debounce} from "throttle-debounce"
  import Scanner from "@lib/components2/Scanner.svelte"
  import Button from "@lib/components2/Button.svelte"
  import Field from "@lib/components2/Field.svelte"
  import CpuBolt from "@assets/icons/cpu-bolt.svg?dataurl"
  import QrCode from "@assets/icons/qr-code.svg?dataurl"
  import Icon from "@lib/components2/Icon.svelte"
  import InfoBunker from "@app/components/InfoBunker.svelte"
  import type {Nip46Controller} from "@app/nip46"
  import {pushModal} from "@app/modal"

  type Props = {
    controller: Nip46Controller
  }

  const {controller}: Props = $props()
  const {loading, bunker} = controller

  const toggleScanner = () => {
    showScanner = !showScanner
  }

  const onScan = debounce(1000, async (data: string) => {
    showScanner = false
    $bunker = data
  })

  let showScanner = $state(false)
</script>

<Field>
  {#snippet label()}
    <p>Bunker Link*</p>
  {/snippet}
  {#snippet input()}
    <label class="input-cl input-cl-group">
      <Icon icon={CpuBolt} />
      <input disabled={$loading} bind:value={$bunker} placeholder="bunker://" />
      <Button onclick={toggleScanner}>
        <Icon icon={QrCode} />
      </Button>
    </label>
  {/snippet}
  {#snippet info()}
    <p>
      A login link provided by a nostr signing app.
      <Button class="cl-link" onclick={() => pushModal(InfoBunker)}>What is a bunker link?</Button>
    </p>
  {/snippet}
</Field>
{#if showScanner}
  <Scanner onscan={onScan} />
{/if}
