import { dropWhile } from "lodash/array";

const state = {
    show_chismosa: false,
    show_devoluciones: false,
    migas: [{ nombre: 'Pedidos', action: 'pedido/resetear' }],
};

const mutations = {
    toggleChismosa(state) {
        state.show_chismosa = !state.show_chismosa;
    },
    toggleDevoluciones(state) {
        state.show_devoluciones = !state.show_devoluciones;
    },
    addMiga(state, miga) {
        state.migas.push(miga);
    },
};

const actions = {
    clickMiga({ dispatch }, { miga }) {
        dispatch(miga.action, null, { root: true });
        state.migas = dropWhile(state.migas.reverse(),(m => m.nombre !== miga.nombre)).reverse();
    },
    toast(_, { mensaje }) {
        return window.bulmaToast.toast({
            message: mensaje,
            duration: 2000,
            type: 'is-danger',
            position: 'bottom-center',
        });
    },
    error({ dispatch }, { error }) {
        const errorMsg = error.response && error.response.data && error.response.data.message
            ? error.response.data.message
            : error.message;
        dispatch("toast", { mensaje: errorMsg });
    },
};

export default {
    namespaced: true,
    state,
    mutations,
    actions,
};