import { defineStore } from 'pinia'
import axios from 'axios'


export const useFooterLinksStore = defineStore('footerLinks',{
  state: () => ({
                locale:  (localStorage.getItem('lang') || 'de'),
                links: []
              }),

   actions: {
    async getLinks(locale:string){
      await this.setLocale(locale)
      return this.links

    },
    getLinkByLabel(oldLabel:string){
      return this.links.find((link:any) => (link.Label as string) == oldLabel)

    },

    async setLocale(locale:string){
      if (this.links.length == 0 || this.locale != locale){
        this.locale = locale
        await this.fetchLinks()
      }
    },

      async fetchLinks(){
            const response = await axios.get('https://strapi.kornblum.one/api/footer-content-pages?&populate[localizations][fields][0]=Label&populate[page][fields][1]=path&locale='+this.locale, {
            headers: {
                Authorization: "Bearer " + import.meta.env.VITE_STRAPI_TOKEN,
            },
            })
            this.links = response.data.data
        },
    }
 })
