import { defineStore } from 'pinia'
// import REST from '@api/rest-axios'
// import JSONAPI from '@api/json-axios'
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 ConcernementFields from '@api/gql/concernement.fragment.gql'
// import EntityFields from '@api/gql/entitydef.fragment.gql'

export const ConcernementsStore = defineStore({
  id: 'concernements',
  state: () => ({
    concernements: [],
    concernementsByID: {},
    allEntitesById: {},
    opened: false,
    opened_entite_id: null,
    ct_concernement: {},
    concernements_loaded: false
  }),
  getters: {
    
  },
  actions: {
    loadConcernements () {
      console.log('concernements store loadConcernements');
      return new Promise((resolve, reject) => {
        const ast = gql`{
          allconcernements {
            ...ConcernementFields
          }
        }
        ${ConcernementFields}
        `
        console.log('ast', ast);
        GQL.post('', { query: print(ast) })
          .then(({ data : { data  : { allconcernements } } }) => {
            console.log('loadconcernements loaded', allconcernements)
            
            this.concernements = [];
            allconcernements.forEach(concernement => {
              concernement.entites_byid = {};
              concernement.entites.forEach(entite => {
                concernement.entites_byid[entite.entite.id] = entite;
                // record a flat list of all entités of all concernement for map-popup
                this.allEntitesById[entite.entite.id] = entite;
              });
              this.concernements.push(concernement);
              this.concernementsByID[concernement.id] = concernement;
            });
            this.concernements_loaded = true;
          })
          .catch(error => {
            console.warn('Issue with loadConcernements', error)
            Promise.reject(error)
          })
      })
    },
    loadContentTypeDefinition () {
      const body = {
        query: `
          query EntityDef($type: String!, $bundle: String!){
            entitydef(type: $type, bundle: $bundle) {
              fields {
                type
                field_name
                label
                description
              }
            }
          }`,
        variables: { type: 'node', bundle: 'concernement' }
      } 
      GQL.post('', body)
        .then(({ data: { data: { entitydef }}}) => {
          // console.log('loadContentTypeDefinition entitydef', entitydef);
          entitydef.fields.forEach(field => {
            this.ct_concernement[field.field_name] = field;
          });
          console.log('loadContentTypeDefinition entitydef', this.ct_concernement);
          
        })

    },
    openCloseConcernements (id) {
      console.log(`openCloseConcernements id: ${id}`);
      var state;
      this.concernements.forEach((c, i) => {
        state = id == c.id;
        this.concernements[i].opened = this.concernementsByID[c.id].opened = state;
        if (state) {
          this.opened = c;
        }
      });
    },
    resetConcernementOpened () {
      this.opened = null;
      this.openCloseConcernements();
    },
    setOpenedEntityId(id){
      this.opened_entite_id = id;
    }
  }
})