<script lang="ts">
  import { timefmt } from '../../../../helpers.js';
  import Nav from '../../Nav.svelte';
  import Tabs from '../../Tabs.svelte';
  import Title from '../Title.svelte';
  import CommentC from '../Comment.svelte';
  import Tag from '../Tag.svelte';
  import Patch from '../Patch.svelte';
  import type { Comment } from '../../../../helpers.ts';

  type Patch_ = {
    id: string;
    hash: string;
    can_add: boolean;
    can_remove: boolean;
    timestamp: number;
    pushed_by: string;
    removed?: number;
    authors: {
      login?: string;
      name?: string;
      key: string;
    }[];
    header: {
      message: string;
      timestamp: string;
    };
  };

  type Tag_ = {
    timestamp: number;
    author: string;
    added: boolean;
    id?: string;
    tag?: {
      name: string;
      fg: string;
      color: string;
    };
  };

  type DiscussionItem = {
    comment?: Comment;
    patch?: Patch_;
    tag?: Tag_;
  };

  type DiscussionT = {
    n: number;
    id: string;
    title: string;
    author: string;
    closed?: number;
    opened: number;
  };

  export let data: {
    owner: string;
    repo: string;
    login: string;
    email: string;
    edit_comment?: string;
    d: DiscussionT;
    channels: string[];
    default_channel: string;
    comments: DiscussionItem[];
    uniq: number;
    token: string;
  };
</script>

<svelte:head>
  <title>{data.owner}/{data.repo} - Discussion #{data.d.n} - {data.d.title}</title>
</svelte:head>
<div class="p-3">
  <Nav user={data.owner} repo={data.repo} link={true} />
  <Tabs user={data.owner} repo={data.repo} active="discussion" />
  <div class="pt-3">
    <Title n={data.d.n} title={data.d.title} />
    <div class="my-3">
      {#if data.d.closed}
        Closed {timefmt(data.d.closed * 1000)}
      {:else}
        Opened by <a class="author" href="/{data.d.author}">{data.d.author}</a>
        {timefmt(data.d.opened * 1000)}
      {/if}
    </div>
  </div>
  {#if data.comments.length}
    {#each data.comments as c}
      {#if c.comment}
        <CommentC
          disc={data.d}
          owner={data.owner}
          login={data.login}
          comment={c.comment}
          repo={data.repo}
          edit={c.comment.id == data.edit_comment}
          token={data.token}
          uniq={data.uniq} />
      {:else if c.tag}
        <Tag owner={data.owner} repo={data.repo} tag={c.tag} />
      {:else if c.patch}
        <Patch
          can_add={c.patch.can_add}
          can_remove={c.patch.can_remove}
          id={c.patch.id}
          disc={data.d.n}
          removed={c.patch.removed}
          hash={c.patch.hash}
          authors={c.patch.authors}
          timestamp={c.patch.timestamp}
          pushed_by={c.patch.pushed_by}
          owner={data.owner}
          repo={data.repo}
          header={c.patch.header}
          token={data.token} />
      {/if}
    {/each}
  {/if}
  {#if data.login}
    <CommentC
      owner={data.owner}
      repo={data.repo}
      login={data.login}
      edit={true}
      comment={{
        author: data.login,
        id: '',
        content: '',
        content_html: '',
        timestamp: 0
      }}
      disc={data.d}
      token={data.token}
      uniq={data.uniq} />
  {/if}
</div>