const state = {
  user_personal_info: {
    avatar: "media/users/blank.jpg",
    first_name: "",
    last_name: "",
    email: "",
    mobile: "",
  },
};

const getters = {
  currentUserPersonalInfo(state) {
    return state.user_personal_info;
  },
};
const actions = {
  SAVE_USER_PROPERTIES(context, properties) {
    let url = "profile";

    // if(properties.skills.row_id || properties.favorites.row_id){
    //     let userId = '';
    //     if(properties.skills.user_id)
    //       userId = properties.skills.user_id;
    //     if(properties.favorites.user_id)
    //       userId = properties.favorites.user_id;

    //     url = "api/profile/" + userId;
    //     properties._method = 'PUT';
    // }

    properties.skills = JSON.stringify(properties.skills);
    properties.favorites = JSON.stringify(properties.favorites);
    return new Promise((resolve, reject) => {
      ApiService.post(
        null,
        url,
        properties,
        function (response) {
          resolve(response);
        },
        function (errors) {
          reject(errors);
        },
        true
      );
    });
  },

  UPDATE_PERSONAL_INFO(context, payload) {
    ApiService.post(
      null,
      "profile/update",
      payload,
      null,
      null,
      true,
      "ویرایش مشخصات"
    );
    context.commit(SET_PERSONAL_INFO, payload);
  },
  logOut(state) {
    state.isAuthenticated = false;
    state.user = {};
    state.errors = {};
    destroyToken();
    window.localStorage.removeItem("USER_TYPE");
  },
  GET_PERSONAL_INFO(context) {
    ApiService.get(
      null,
      "profile/get",
      function (response) {
        var payload = response.data.data;
        context.commit(SET_PERSONAL_INFO, payload);
      },
      function () {
        logout();
      }
    );
  },
};
const mutations = {
  [SET_PERSONAL_INFO](state, user_personal_info) {
    state.user_personal_info = user_personal_info;
  },
  SET_USER_PROPERTIES(state, properties) {
    state.user_personal_info.properties = properties;
  },
};
export default {
  state,
  actions,
  mutations,
  getters,
};