import { defineStore } from 'pinia'
import { print } from 'graphql/language/printer'
import gql from 'graphql-tag'

// import REST from '@api/rest-axios'
import GQL from '@api/graphql-axios'
// import JSONAPI from '@api/json-axios'

import StaticsFields from '@api/gql/statics.fragment.gql'

export const StaticsStore = defineStore({
  id: 'statics',
  state: () => ({
    loaded: false,
    statics: [],
    statics_byid: {}
  }),
  getters: {
    
  },
  actions: {
    loadStatics () {
      console.log('statics store loadStatics');
      return new Promise((resolve, reject) => {
        const ast = gql`{
          allstatics {
            ...StaticsFields
          }
        }
        ${StaticsFields}
        `
        console.log('ast', ast);
        GQL.post('', { query: print(ast) })
          .then(({ data : { data  : { allstatics } } }) => {
            console.log('loadstatics loaded', allstatics)
            this.statics = allstatics
            
            allstatics.forEach((s) => {
              // console.log("s", s);
              this.statics_byid[s.id] = s
            });
            console.log("statics_byid", this.statics_byid);
            this.loaded = true;
          })
          .catch(error => {
            console.warn('Issue with loadStatics', error)
            Promise.reject(error)
          })
      })
    }
  }
})