<script lang="ts">
import { fileTypeFromBuffer } from 'file-type';
import type Embedding from './Embedding.svelte';
import type Embeddings from './Embeddings.svelte';

import { EmbeddedFile, EMBED_TYPES } from './main';

  import { settings } from './stores'

  export let id = ''
  export let files: EmbeddedFile[];
  export let inst: Embedding | Embeddings;

  let isVideo = false

  inst.$on("fileinfo", (info) => {
    isVideo = isVideo || info.detail.type.mime.startsWith('video/');
  })

  let visible = false
  function reveal() {
    visible = !visible
    document.dispatchEvent(new CustomEvent('reveal', { detail: { id } }))
  }
  const isNotChrome = !navigator.userAgent.includes("Chrome/");

  async function downloadFile(file: EmbeddedFile) {
    const a = document.createElement("a") as HTMLAnchorElement;
    document.body.appendChild(a);
    a.style.display = 'none';
    const thumb = Buffer.isBuffer(file.data) ? file.data : await file.data();
    const type = await fileTypeFromBuffer(thumb);
    const url = URL.createObjectURL(new Blob([thumb], { type: type?.mime }))
    a.href = url;
    a.download = file.filename;
    a.click();
    window.URL.revokeObjectURL(url);
  }
</script>

{#if $settings.eye}
  <span
    on:click={reveal}
    class:fa-eye={!visible}
    class:fa-eye-slash={visible}
    class="fa clickable"
  />
{/if}
{#each files as file}
<span
  title={file.filename}
  on:click={() => downloadFile(file)}
  class="fa fa-download clickable"
  class:hasembed={file?.embed_type === EMBED_TYPES.THIRD_EYE}
  class:hasext={file?.embed_type === EMBED_TYPES.MEDIA_EMBED}
  class:hasblack={file?.isBlacklisted === true}
/> <!-- hasblack is last for it to have greater weight than hasext, to override it -->

{#if file.source}
<!-- svelte-ignore a11y-missing-content -->
<a
  href={file.source}
  target="_blank"
  class="clickable"
  class:hasembed={file?.embed_type === EMBED_TYPES.THIRD_EYE}
  class:hasext={file?.embed_type === EMBED_TYPES.MEDIA_EMBED}
  class:hasblack={file?.isBlacklisted === true}
>Source</a>
{/if}
{#if file.page}
<!-- svelte-ignore a11y-missing-content -->
<a
  href={file.page.url}
  target="_blank"
  class="clickable"
  class:hasembed={file?.embed_type === EMBED_TYPES.THIRD_EYE}
  class:hasext={file?.embed_type === EMBED_TYPES.MEDIA_EMBED}
  class:hasblack={file?.isBlacklisted === true}
>{file.page.title}</a>
{/if}
{#if isNotChrome && isVideo}
  <!-- svelte-ignore a11y-missing-attribute -->
  <a on:click={(ev) => {
    inst.bepis(ev);
  }} alt="By clicking this you agree to stay hydrated"
  class="clickable"
  class:hasembed={file?.embed_type === EMBED_TYPES.THIRD_EYE}
  class:hasext={file?.embed_type === EMBED_TYPES.MEDIA_EMBED}
  class:hasblack={file?.isBlacklisted === true}
    >[PEE contract]</a
  >
{/if}
{/each}

<style scoped>
  .clickable {
    cursor: pointer;
    /* margin-left: 5px; */

    /* Custom */
    margin-left: 2px;
    margin-right: 2px;
  }

  .clickable:hover {
    text-shadow: 0 0 4px palevioletred;
  }
  .clickable.hasembed {
    color: deeppink;
  }
  .clickable.hasext {
    color: goldenrod;
  }
  .clickable.hasblack {
    color: black;
  }
  a.clickable.hasembed {
    color: deeppink !important;
  }
  a.clickable.hasext {
    color: goldenrod !important;
  }
  a.clickable.hasblack {
    color: black !important;
  }
</style>
