<script lang="ts">
  import { GitBranch, LoaderCircle, X } from "@lucide/svelte";
  import type { GitCommit } from "../types";

  interface Props {
    commit: GitCommit;
    isBusy: boolean;
    onCreate: (name: string) => void;
    onClose: () => void;
  }

  let {
    commit,
    isBusy = false,
    onCreate = () => {},
    onClose = () => {},
  }: Props = $props();

  let name = $state("");

  function submit(event: SubmitEvent) {
    event.preventDefault();
    const value = name.trim();
    if (!value) return;
    onCreate(value);
  }
</script>

<div
  class="dialog-backdrop"
  role="presentation"
>
  <div class="dialog new-branch-dialog" role="dialog" aria-modal="true" aria-label="Create branch from commit" tabindex="-1">
    <header class="dialog-header">
      <div>
        <span class="eyebrow">New branch</span>
        <h2 class="mt-0.5 text-ink text-base font-bold leading-tight">From commit</h2>
      </div>
      <button class="dialog-close" type="button" onclick={onClose} title="Close">
        <X size={18} aria-hidden="true" />
      </button>
    </header>

    <form class="new-branch-form" onsubmit={submit}>
      <div class="new-branch-target">
        <span class="hash">{commit.short_hash}</span>
        <span class="new-branch-summary" title={commit.summary}>{commit.summary}</span>
      </div>

      <label class="new-branch-field">
        <span>Branch name</span>
        <!-- svelte-ignore a11y_autofocus -->
        <input
          bind:value={name}
          autocomplete="off"
          spellcheck="false"
          placeholder="feature/my-branch"
          disabled={isBusy}
          autofocus
        />
      </label>

      <div class="new-branch-actions">
        <button class="btn-secondary" type="button" onclick={onClose} disabled={isBusy}>
          Cancel
        </button>
        <button class="btn-primary" type="submit" disabled={isBusy || name.trim().length === 0}>
          {#if isBusy}
            <LoaderCircle class="spin" size={16} aria-hidden="true" />
          {:else}
            <GitBranch size={16} aria-hidden="true" />
          {/if}
          Create branch
        </button>
      </div>
    </form>
  </div>
</div>
