const axios = require("axios");
export default {
  async GET_AVAILABLE_CATEGORIES({ commit }) {
    try {
      let myResult = await axios.get(
        `${this.$config.CMS_ENDPOINT}/api/trigger-categories`
      );
      if (myResult) {
        let myResList = [];
        for (let i = 0; i < myResult.data.data.length; i++) {
          myResList.push(myResult.data.data[i].attributes.description);
        }

        commit("SET_AVAILABLE_CATEGORIES", myResList);
      }
    } catch (err) {
      console.log(err);
    }
  },
  async GET_CURRENT_USER_CATEGORIES({commit}) {
    try {
      let myResult = await axios.get(
        `${this.$config.CMS_ENDPOINT}/api/trigger-categories`
      );
      if (myResult) {
        let myResList = [];
        for (let i = 0; i < myResult.data.data.length; i++) {
          myResList.push(myResult.data.data[i].attributes);
        }

        commit("SET_USER_CATEGORIES", myResList);
      }
    } catch (err) {
      console.log(err);
    }
  },
  async SAVE_NEW_CATEGORIES({ commit }, { payload }) {
    try {
      let myResult = await axios.post(
        `${this.$config.CMS_ENDPOINT}/api/trigger-categories`,
        { data: { description: payload } }
      );
      if (myResult) {
        console.log("Succesfully Added New Category");
      }
    } catch (err) {
      console.log(err);
    }
  },
  async GET_AVAILABLE_MAPPINGS({commit}) {
    try {
      let myResult = await axios.get(
        `${this.$config.CMS_ENDPOINT}/api/trigger-mappings`
      );
      if (myResult) {
        let myResList = [];
        for (let i = 0; i < myResult.data.data.length; i++) {
          let myAttr = {};
          myAttr.id = myResult.data.data[i].id;
          myAttr.rfid = myResult.data.data[i].attributes.rfid;
          myAttr.triggername = myResult.data.data[i].attributes.triggername;
          myAttr.triggervalue = myResult.data.data[i].attributes.triggervalue;
          myAttr.description = myResult.data.data[i].attributes.description;
          myResList.push(myAttr);
        }
        commit("SET_MAPPING_CATEGORIES", myResList);      
      }
    } catch (err) {
      console.log(err);
    }
  },
  async SAVE_NEW_MAPPING({commit},{triggernameVal, triggerVal, rfidVal}) {
    try {
      let myResult = await axios.post(
        `${this.$config.CMS_ENDPOINT}/api/trigger-mappings`,
        {
          data: {
            triggername: triggernameVal,
            triggervalue: triggerVal,
            rfid: rfidVal,
          },
        }
      );
      if(myResult){
        let myResList = [];
        for (let i = 0; i < myResult.data.data.length; i++) {
          myResList.push(myResult.data.data[i].attributes);
        }

        commit("SET_MAPPING_CATEGORIES", myResList);        
      }
    } catch (err) {
      console.log(err);
    }
  },
  async REMOVE_MAPPING({commit},{id}){
    try{
      await axios.delete(`${this.$config.CMS_ENDPOINT}/api/trigger-mappings/${id}`);
    }catch(err){
      console.log(err);
    }
  }
};
