import * as _ from 'lodash'
import { baseOrganizationState, organizationState } from '~/types/interfaces'

export const state = () => ({
  id: null,
  name: '',
  product: '',
  currentActivityYear: '',
  modules: [],
  hasChildren: false,
  showAdherentList: false,
  networks: [],
  website: '',
  subDomain: '',
  parents: []
})

export const mutations = {
  setId (state: organizationState, id: number) {
    state.id = id
  },
  setName (state: organizationState, name: string) {
    state.name = name
  },
  setProduct (state: organizationState, product: string) {
    state.product = product
  },
  setCurrentActivityYear (state: organizationState, currentActivityYear: number) {
    state.currentActivityYear = currentActivityYear
  },
  setModules (state: organizationState, modules: Array<string>) {
    state.modules = modules
  },
  setHasChildren (state: organizationState, hasChildren: boolean) {
    state.hasChildren = hasChildren
  },
  setShowAdherentList(state:organizationState, showAdherentList:boolean) {
    state.showAdherentList = showAdherentList
  },
  setNetworks(state:organizationState, networks:Array<string>) {
    state.networks = networks
  },
  setParents (state: organizationState, parents: Array<organizationState>) {
    state.parents = parents
  },
  setWebsite (state: organizationState, website: string) {
    state.website = website
  },
  setSubDomain (state: organizationState, subDomain: string) {
    state.subDomain = subDomain
  },
  addParent (state: organizationState, parent: organizationState) {
    state.parents.push(parent)
  }
}

export const actions = {
  setProfile (context: any, profile: any) {
    context.commit('setId', profile.id)
    context.commit('setName', profile.name)
    context.commit('setProduct', profile.product)
    context.commit('setCurrentActivityYear', profile.currentYear)
    context.commit('setWebsite', profile.website)
    context.commit('setSubDomain', profile.subDomain)
    context.commit('setModules', profile.modules)
    context.commit('setHasChildren', profile.hasChildren)
    context.commit('setShowAdherentList', profile.showAdherentList)
    context.commit('setNetworks', profile.networks)

    _.each(profile.parents, (parent) => {
      const p: baseOrganizationState = {
        id: parent.id,
        name: parent.name,
        website: parent.website,
        subDomain: parent.subDomain
      }
      context.commit('addParent', p)
    })
  }
}
