<template>
      <nav>

        <RouterLink to="/">{{ t("Home") }}</RouterLink>

        <RouterLink v-for="link in links" v-model="links" :to="`${link.page.path}`" :key="link.id">
            {{ link.Label }}
        </RouterLink>
        <select @change="changeLocale()" v-model="$i18n.locale">
          <option v-for="(lang, i) in $i18n.availableLocales" :key="`Lang${i}`" :value="lang">
            {{ lang }}
          </option>
        </select>
      </nav>
</template>

<style scoped>
nav {
  width: 100%;
  font-size: 12px;
  text-align: center;
  line-height: 90px;
  margin: 0;
  padding: 0;
}

nav a.router-link-exact-active {
  color: var(--color-text);
}

nav a.router-link-exact-active:hover {
  background-color: transparent;
}

nav a, nav select {
  display: inline-block;
  padding: 0 1rem;
  border-left: 1px solid var(--color-border);
}
nav select{
      width: 70px;
      height: 40px;
      font-size: 16px;
      border-radius: 4px;
      padding-left: 10px;
      color: #fff;

      border: 1px solid #767676;
      background-color: transparent ;
}
nav a:first-of-type {
  border: 0;
}

/*select{
      width: 70px;
      height: 40px;
      -moz-padding-start: calc(10px - 3px);
      padding-left: 10px;
      background-color: transparent ;
      color: #fff;
      font-family: 'Open Sans', sans-serif;
      font-size: 16px;
      border-radius: 4px;
    }*/
</style>

<script lang="ts">
    import { useFooterLinksStore } from '@/stores/footer-content-pages'
    import { useTimelineStore } from '@/stores/timeline'
    import { useComponentStore } from '@/stores/component'
    import { usePageStore } from '@/stores/pages'
    import {loadDynamicPages} from '@/router'
    import { useI18n } from 'vue-i18n'
    import { useRoute } from 'vue-router'
import type { FooterLink } from '@/types/footer-link'

    export default {
        data () {
            return {
            links: [] as Array<FooterLink>,
            }
        },
        setup(){
          const { t } = useI18n({
            inheritLocale: true
          })
          const route = useRoute()
          const footerLinksStroe = useFooterLinksStore()
          return{
            t,
            route,
            footerLinksStroe
          }
        },
        async mounted () {
            this.links = await this.footerLinksStroe.getLinks(this.$i18n.locale)
        },
        methods:{
          async changeLocale(){
            const locale = this.$i18n.locale
            localStorage.setItem('lang', locale)
            const pageStore = usePageStore()
            await pageStore.setLocale(locale)
            await useTimelineStore().setLocale(locale)
            await useComponentStore().setLocale(locale)
            loadDynamicPages(pageStore.pages)
            const oldLink = this.footerLinksStroe.getLinkByLabel(this.route.name as string) as FooterLink
            this.links = await this.footerLinksStroe.getLinks(locale)
            if(oldLink){
              const newLink = this.footerLinksStroe.getLinkByLabel(oldLink.localizations[0].Label) as FooterLink
              setTimeout(() => {this.$router.push(newLink.page.path)}, 100)
            }
          }
        }
    }
</script>


