import { doFetchCountries, doFetchStates, doFetchBasicInfo, doFetchChangePassword } from '@/services/user.service';
import { getField, updateField } from 'vuex-map-fields';
import humps from 'lodash-humps';
const addresses = {
    "regionId": "",
    "region": "",
    "countryId": "",
    "postcode": "",
    "city": "",
    "telephone": "",
    "firstname": "",
    "lastname": "",
    "defaultShipping": "false",
    "defaultBilling": "false"
}
const streetNew = {
    "streetNoOne": "",
    "streetNoTwo": "",
}
export const accountOne = {
    namespaced: true,
    state: {
        basicInfo: {},
        address: {},
        shippingMethods: null,
        changedPasswordFlag: true,
        creditBalance: "$0.00",
        allCountries: [],
        countryWiseStates: [],
        newsLetterSubscription: null,
        errorMessage: null,
    },
    getters: {
        getField,
    },
    mutations: {
        updateField,
        setallCountries(state, allCountries) {
            state.allCountries = allCountries
        },
        setCountryWiseStates(state, countryWiseStates) {
            state.countryWiseStates = countryWiseStates
        },
        setOne(state, basicInfo) {
            state.basicInfo = basicInfo
        },
        setAddress(state, address) {
            if (address) {
                state.address = Object.assign({}, state.address, address, streetNew);
                state.address.streetNoOne = address.street[0];
                state.address.streetNoTwo = address.street[1];
            } else {
                state.address = Object.assign({}, state.address, addresses, streetNew);
                state.address.firstname = state.basicInfo.firstname;
                state.address.lastname = state.basicInfo.lastname;
            }
            /*  let setAddress = basicInfo.addresses[0];
              if (setAddress === '') {
                  state.address.regionId = '',
                      state.address.region = '',
                      state.address.countryId = '',
                      state.address.postcode = '',
                      state.address.city = '',
                      state.address.telephone = '',
                      state.address.firstname = basicInfo.firstname,
                      state.address.lastname = basicInfo.lastname,
                      state.address.street = '',
                      state.address.house = '',
                      state.address.defaultShipping = '',
                      state.address.defaultBilling = ''
              } else {
                  state.address.regionId = (setAddress.regionId).toString(),
                      state.address.region = setAddress.region,
                      state.address.countryId = setAddress.countryId,
                      state.address.postcode = setAddress.postcode,
                      state.address.city = setAddress.city,
                      state.address.telephone = setAddress.telephone,
                      state.address.firstname = setAddress.firstname,
                      state.address.lastname = setAddress.lastname,
                      state.address.street = setAddress.street[0],
                      state.address.house = setAddress.street[1],
                      state.address.defaultShipping = setAddress.defaultShipping,
                      state.address.defaultBilling = setAddress.defaultBilling
              }*/
        },
        setChangePasswordFlag(state, changedPasswordFlag) {
            state.changedPasswordFlag = changedPasswordFlag;
        }
    },
    actions: {
        fetchStates: async({ state, commit }) => {
            try {
                const countryAllStates = await doFetchStates();
                if (countryAllStates) {
                    commit('setCountryWiseStates', humps(countryAllStates));
                }
            } catch (err) {
                state.errorMessage = err.message;
            }
        },
        fetchCountries: async({ state, commit }) => {
            try {
                const allCountries = await doFetchCountries();
                if (allCountries) {
                    commit('setallCountries', humps(allCountries));
                }
            } catch (err) {
                state.errorMessage = err.message;
            }
        },
        basicInfo: async({ state, commit }) => {
            try {
                const basicInfo = await doFetchBasicInfo();
                if (basicInfo) {
                    commit('setOne', humps(basicInfo));
                    commit('setAddress', humps(basicInfo.addresses[0]));
                }
            } catch (err) {
                state.errorMessage = err.message;
            }
        },
        changePassword: async({ state, commit }, { currentPassword, newPassword }) => {
            try {
                const changedPassword = await doFetchChangePassword(currentPassword, newPassword);
                commit('setChangePasswordFlag', changedPassword);
            } catch (err) {
                state.errorMessage = err.message;
            }
        },
        save: async({ state }) => {
            /* eslint-disable no-alert, no-console ,  no-debugger */
            console.log("currentPassword: ", state.address)
        },
    }
}