<template>
  <div class="editor-translations">
    <strong>Vertalingen</strong>
    <ul class="list-group list-group-flush py-2">
      <li class="list-group-item border-0 py-1" v-for="translation in translations" :key="translation.language">
        <button class="btn d-flex justify-content-between w-100 p-0" :class="{ active: isActive(translation) }" @click="$emit('click', translation)">
          <div class="flex-shrink-0 mr-4">
            {{ getLanguageName(translation) }}
          </div>
          <div>
            <page-translation-status v-if="type === entityType.Page" :translation="translation" />
            <item-translation-status v-else :translation="translation" />
          </div>
        </button>
      </li>
    </ul>
  </div>
</template>

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import { namespace } from 'vuex-class';

import { EntityType, SupportedLanguage, Translation } from '../../types/language';
import PageTranslationStatus from './page-translation-status.vue';
import ItemTranslationStatus from './item-translation-status.vue';

const LanguageStore = namespace('languages');

@Component({
  components: {
    PageTranslationStatus,
    ItemTranslationStatus
  }
})
export default class EditorTranslations extends Vue {
  @Prop({ type: Array, required: true }) readonly translations!: Translation[];
  @Prop({ type: String, required: true }) readonly type!: EntityType;

  entityType = EntityType;

  @LanguageStore.State('all') readonly allLanguages!: SupportedLanguage[];
  @LanguageStore.State('selected') selectedLanguage!: SupportedLanguage | null;

  getLanguageName(translation: Translation) {
    return this.allLanguages.find(language => language.key === translation.language)?.name || translation.language;
  }

  isActive(translation: Translation) {
    return this.selectedLanguage?.key === translation.language;
  }
}
</script>

<style lang="scss" scoped>
@import '../../variables';

.editor-translations {
  .btn {
    color: $brand-primary;

    &.active {
      color: $body-color;
      cursor: initial;
    }

    &:hover:not(.active) {
      text-decoration: underline;
    }
  }
}
</style>
