import wordBankApi from '../api/wordBankApi'

const state = {
    words: [],
    count: 0,
    isLoading: true
}
const mutations = {
    setWords(state, data) {
        state.words = data;
    },
    setCount(state, data) {
        state.count = data;
    },
    setLoading(state, status) {
        state.isLoading = status
    }
}

const actions = {
    queryPageable({commit}, pageData) {
        commit('setLoading', true);
        wordBankApi.queryPageable(pageData.data, pageData.index, pageData.size, (response) => {
            if (response.data.code === 200) {
                let content = response.data.content
                if (pageData.showSensitiveWord === false) {
                    for (let i = 0; i < content.length; ++i) {
                        if (content[i].category === "0") {
                            content[i].word = "******";
                        }
                    }
                }
                commit('setWords', content);
                commit('setCount', parseInt(response.data.message));
                commit('setLoading', false);
            } else {
                commit('setLoading', false);
            }
        })
    },
    deleteByIds(ids) {
        wordBankApi.deleteByIds(ids, (response) => {
            return response.data.code === 200;
        })
    }
}

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