<script setup lang="ts">
/// <reference types="@types/gtag.js" />

import { computed, toRefs } from 'vue'
import type { Extension } from '../../queries/useExtensionsRepositoryQuery'

const props = defineProps<{ item: Extension }>()
const { item } = toRefs(props)

const pkgId = computed(() => {
  return item.value.pkg.replace('eu.kanade.tachiyomi.extension.', '')
})

const pkgName = computed(() => item.value.name.split(': ')[1])

const iconUrl = computed(() => {
  return `https://raw.githubusercontent.com/tachiyomiorg/extensions/repo/icon/${item.value.pkg}.png`
})

const apkUrl = computed(() => {
  return `https://raw.githubusercontent.com/tachiyomiorg/extensions/repo/apk/${item.value.apk}`
})

function handleAnalytics() {
  window.gtag?.('event', 'Download', {
    event_category: 'Extension',
    event_label: pkgName.value,
    version: item.value.version,
  })
}
</script>

<template>
  <div class="extension" tabindex="-1">
    <a :href="`#${pkgId}`" class="anchor" aria-hidden="true" @click.stop>#</a>
    <img class="extension-icon" :src="iconUrl" loading="lazy" width="42" height="42">
    <div class="extension-text">
      <div class="upper">
        {{ pkgName }}
      </div>
      <div class="lower">
        {{ pkgId }}
      </div>
    </div>
    <Badge type="info" :text="item.version" />
    <a
      :href="apkUrl"
      class="extension-download"
      title="Download APK"
      :download="item.apk"
      @click="handleAnalytics"
    >
      ↓
    </a>
  </div>
</template>

<style lang="stylus">
.extension {
  position: relative
  align-items: center
  display: flex
  width: calc(100% + 1em)
  padding: 0.5em
  margin: 0.8em -0.5em
  border-radius: 8px
  gap: 0.675rem
  scroll-margin-top: calc(var(--vp-nav-height) + 24px)

  &:hover {
    background-color: var(--vp-c-bg-soft)
  }

  &:target,
  &:focus {
    background-color: var(--vp-c-brand-soft)
    border-radius: 8px
    transition: 500ms background-color
  }

  &:focus {
    outline: none
  }

  .anchor {
    position: absolute
    left: 0
    margin-left: -1em
    font-size: 1.4em
    opacity: 0
  }

  &:hover .anchor,
  .anchor:focus-visible {
    opacity: 1
  }

  .extension-icon {
    flex-shrink: 0
    margin-left: -4px
  }

  .extension-text {
    flex-grow: 1
    min-width: 0

    .upper {
      color: var(--vp-c-text-1)
      font-weight: 600
      overflow: hidden
      text-overflow: ellipsis
      white-space: nowrap

      .badge {
        font-weight: 400
      }
    }

    .lower {
      color: var(--vp-c-text-2)
      font-family: var(--vp-font-family-mono)
      font-size: 0.75rem
      overflow: hidden
      text-overflow: ellipsis
      white-space: nowrap
      line-height: 1rem
    }
  }

  .extension-download {
    padding: 0.4em
    font-weight: 700
    font-size: 1.4em
    border-radius: 4px
    flex-shrink: 0
    margin-right: -0.4em

    .material-icons {
      color: white
      max-width: 18px
    }

    &:hover {
      .VPBadge {
        background-color: var(--vp-c-brand-darker)
        text-decoration: none
      }
    }
  }

  @media (max-width 767px) {
    padding: 0.4em 0
  }
}

@media (max-width 767px) {
  .anchor {
    display: none
  }

  .extension {
    border: 1px solid var(--vp-c-divider)
    border-radius: 8px
    padding: 0.5em
    margin: 0.8em 0
    width: 100%

    .extension-icon {
      margin-left: 0
    }

    .extension-download {
      margin-right: 0
    }
  }
}
</style>
