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


export const useTimelineStore = defineStore('timelines',{
  state: () => ({
    timelines: [] as Array<any>,
    locale: (localStorage.getItem('lang') || 'de')
  }),
  actions: {
    async getTimelines(locale: string){
      await this.setLocale(locale)
      return this.timelines

    },

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

    async fetchTimelines(){
      const response = await axios.get('https://strapi.kornblum.one/api/timelines?sort=date&locale='+this.locale, {
      headers: {
          Authorization: "Bearer "+ import.meta.env.VITE_STRAPI_TOKEN,
      },
      })
      this.timelines = response.data.data
    },
  }
})
