<script setup lang="ts">
import { computed } from 'vue'
import { useData } from 'vitepress/dist/client/theme-default/composables/data'
import { useEditLink } from 'vitepress/dist/client/theme-default/composables/edit-link'
import { usePrevNext } from 'vitepress/dist/client/theme-default/composables/prev-next'
import VPIconEdit from 'vitepress/dist/client/theme-default/components/icons/VPIconEdit.vue'
import VPLink from 'vitepress/dist/client/theme-default/components/VPLink.vue'
import VPDocFooterLastUpdated from 'vitepress/dist/client/theme-default/components/VPDocFooterLastUpdated.vue'
import CustomIcon from './CustomIcon.vue'
import { Icons } from '@beeline/design-tokens/js/iconfont/icons'

const { theme, page, frontmatter } = useData()

const editLink = useEditLink()
const control = usePrevNext()

const hasEditLink = computed(() => {
  return theme.value.editLink && frontmatter.value.editLink !== false
})
const hasLastUpdated = computed(() => {
  return page.value.lastUpdated && frontmatter.value.lastUpdated !== false
})
const showFooter = computed(() => {
  return hasEditLink.value || hasLastUpdated.value || control.value.prev || control.value.next
})
</script>

<template>
  <footer v-if="showFooter" class="VPDocFooter CustomDocFooter">
    <slot name="doc-footer-before" />

    <div v-if="hasEditLink || hasLastUpdated" class="edit-info">
      <div v-if="hasEditLink" class="edit-link">
        <VPLink class="edit-link-button" :href="editLink.url" :no-icon="true">
          <VPIconEdit class="edit-link-icon" aria-label="edit icon"/>
          {{ editLink.text }}
        </VPLink>
      </div>

      <div v-if="hasLastUpdated" class="last-updated">
        <VPDocFooterLastUpdated />
      </div>
    </div>

    <nav v-if="control.prev?.link || control.next?.link" class="prev-next">
      <div class="pager">
        <VPLink v-if="control.prev?.link" class="pager-link prev" :href="control.prev.link">
          <CustomIcon class="desc-prev" :icon="Icons.ArrowRight" />
          <span class="title" v-html="control.prev.text"></span>
        </VPLink>
      </div>
      <div class="pager">
        <VPLink v-if="control.next?.link" class="pager-link next" :href="control.next.link">
          <span class="title" v-html="control.next.text"></span>
          <CustomIcon class="desc-next" :icon="Icons.ArrowRight" />
        </VPLink>
      </div>
    </nav>
  </footer>
</template>

<style lang="scss" scoped>
.VPDocFooter {
  margin-top: 40px;
}

.edit-info {
  padding-bottom: 18px;
}

@media (min-width: 640px) {
  .edit-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 14px;
  }
}

.edit-link-button {
  display: flex;
  align-items: center;
  border: 0;
  line-height: 32px;
  font-size: 14px;
  font-weight: 500;
  color: var(--vp-c-brand-1);
  transition: color 0.25s;
}

.edit-link-button:hover {
  color: var(--vp-c-brand-2);
}

.edit-link-icon {
  margin-right: 8px;
  width: 14px;
  height: 14px;
  fill: currentColor;
}

.prev-next {
  display: grid;
  grid-row-gap: 8px;
}

@media (min-width: 640px) {
  .prev-next {
    grid-template-columns: repeat(2, 1fr);
    grid-column-gap: 16px;
  }
}

.pager-link {
  display: block;
  border: 1px solid var(--vp-c-divider);
  border-radius: 8px;
  padding: 10px 16px 10px 12px;
  width: 100%;
  height: 100%;
  transition: border-color 0.25s;
  display: flex;
  gap: 8px;
  
  color: #1a73e8;

  &.next {
    padding: 10px 12px 10px 16px;
    justify-content: flex-end;
  }
}

.pager-link:hover {
  border-color: var(--vp-c-divider);
}

.pager-link.next {
  margin-left: auto;
  text-align: right;
}

.desc {
  display: block;
  line-height: 20px;
  font-size: 14px;
  font-weight: 500;
  color: var(--vp-c-text-2);

  &-next {
    rotate: -270deg;
  }

  &-prev {
    rotate: -90deg;
  }
}

.title {
  display: block;
  line-height: 20px;
  font-size: 16px;
  font-weight: 500;
  color: #1a73e8;
  transition: color 0.25s;
}
</style>
