import {defineStore} from 'pinia';
import moment from 'moment-timezone';
import axios from 'axios';

moment.tz.setDefault('America/Chicago')

const apikey = import.meta.env.VITE_API_KEY;
const apiUrl = import.meta.env.PROD
  ? "https://api.digisnaxx.com"
  : "http://localhost:8000";

export const useEventsStore = defineStore('Events', {
  state: () => {
    return {
      headers: {
            "content-type": "application/json",
            "Authorization": `${apikey}`,
          },
      events: [],
      promo: [],
      errors: [],
      search_term: '',
      the_day: "",
      new_msg: "",
      today: "",
      shortcode: "",
    };
  },

  actions: {
    setCalendar(cal) {
      this.shortcode = cal;
      console.log("CAL: ", this.shortcode);
      return this.shortcode;
    },

    async fetchPromo() {
      // const results = await fetch(`${apiUrl}/socials/promo/`, {headers: this.headers});
      const results = await fetch(`${apiUrl}/socials/links/`, {headers: this.headers});
      const data = await results.json();
      this.promo = data;
    },

    async fetchEvents() {
      const results = await fetch(`${apiUrl}/events/events/`, {headers: this.headers});
      const data = await results.json();
      this.events = data.slice(0,16);
    },
  }
});