import {
  CHANGE_OPERATION_ITEM,
  CLEAR_OPERATION_ITEM,
  LOAD_COMPLETED,
  CHANGE_CREATE_NODE_TYPE
} from '../mutation-types.js'

// initial state
const state = {
  operationItem: {},
  loading: true,
  createNodeType: ''
}

// getters
const getters = {
  operationItem: state => state.operationItem,
  loading: state => state.loading,
  createNodeType: state => state.createNodeType
}

// actions
const actions = {
  changeOperationItem ({commit}, item) {
    commit(CHANGE_OPERATION_ITEM, item)
  },
  clearOperationItem ({commit}) {
    commit(CLEAR_OPERATION_ITEM)
    return true
  },
  loadCompleted ({commit}) {
    commit(LOAD_COMPLETED)
  },
  changeCreateNodeType ({commit}, type) {
    commit(CHANGE_CREATE_NODE_TYPE, type)
  }
}

// mutations
const mutations = {
  [CHANGE_OPERATION_ITEM] (state, item) {
    state.operationItem = item
  },
  [CLEAR_OPERATION_ITEM] (state) {
    state.operationItem = {}
  },
  [LOAD_COMPLETED] (state) {
    state.loading = false
  },
  [CHANGE_CREATE_NODE_TYPE] (state, type) {
    state.createNodeType = type
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
