import { doFetchProductDetails, doFetchProductsAttributes, doFetchConfiguredProductDetails, doFetchPartPrice, doPatchOne } from '@/services/product.service';
import { getField, updateField } from 'vuex-map-fields';
import humps from 'lodash-humps';


export const productOne = {
    namespaced: true,
    state: {
        fetchInProgress: true,
        configuredProductProgress: true,
        productDetails: {},
        productMedia: {},
        errorMessage: null,
        productAttributes: {},
        configuredProductDetails: {},
        fetchPriceInProgress: false,
    },
    getters: {
        getField
    },
    mutations: {
        updateField,
        fetchInProgress(state, yesOrNo) {
            state.fetchInProgress = yesOrNo
        },
        setProductsDetails(state, details) {
            state.productDetails = details
        },
        setProductsAttributes(state, list) {
            state.productAttributes = list
        },
        setErrorMessage(state, err) {
            state.errorMessage = err
        },
        setConfiguredProductDetails(state, details) {
            state.configuredProductDetails = details
        },
        configuredProductProgress(state, yesOrNo) {
            state.configuredProductProgress = yesOrNo
        },
        fetchPriceInProgress(state, yesOrNo) {
            state.fetchPriceInProgress = yesOrNo
        },
    },
    actions: {
        fetchProductDetails: async ({ commit }, skuId) => {
            try {
                commit('setErrorMessage', null);
                commit('setProductsDetails', {});
                commit('fetchInProgress', true);
                const productDetails = await doFetchProductDetails(skuId);
                commit('fetchInProgress', false);
                if (productDetails) {
                    commit('setProductsDetails', humps(productDetails[0]));
                    return humps(productDetails[0]);
                }
            } catch (err) {
                // state.errorMessage = err.message;
                commit('fetchInProgress', false);
                commit('setErrorMessage', err.message);
            }
        },
        fetchProductAttribute: async ({ commit }, skuId) => {
            const productAttributes = await doFetchProductsAttributes(skuId);
            if (productAttributes) {
                commit('setProductsAttributes', humps(productAttributes));
                return ({ productAttributes })
            }
        },
        fetchConfiguredProductDetails: async ({ commit }, skuId) => {
            try {
                commit('configuredProductProgress', true);
                const configuredProductDetails = await doFetchConfiguredProductDetails(skuId);
                if (configuredProductDetails) {
                    commit('configuredProductProgress', false);
                    commit('setConfiguredProductDetails', humps(configuredProductDetails));
                }
            } catch (err) {
                commit('configuredProductProgress', false);
                throw err
            }
        },
        fetchPartPrice: async ({ commit }, orderObject) => {
            try {
                commit('fetchPriceInProgress', true);
                let price = await doFetchPartPrice({ order: orderObject });
                commit('fetchPriceInProgress', false);
                return price;
            } catch (err) {
                commit('fetchPriceInProgress', false);
                throw err;
            }
        },
        updateCatlog: async () => {
            try {
                await doPatchOne({ "element": "catalog", "requested_at": "" });
            }
            catch (err) {
                throw err;
            }
        },
    },

}