import {toRaw} from 'vue'
import {packsGroups} from './StaticData'
import {equals, isEmpty, uniq} from 'ramda'

const initState = {
  
  isOpen: false,
  searchValue: null,

  packsGroupsDefault: [],
  packsGroups: null,
};

const state = {
  ...initState
};

const getters = {
  isOpen: (state) => state.isOpen,
  searchValue: (state) => state.searchValue,

  packsGroups: (state) => state.packsGroups,
};

const mutations = {
  setIsOpen: (state, updval) =>  state.isOpen = updval,
  setSearchValue: (state, updval) =>  state.searchValue = updval,

  setPacksGroups: (state, updval) =>  state.packsGroups = updval,
  setPacksGroupsDefault: (state, updval) =>  state.packsGroupsDefault = updval,
  resetStore: (state) => {
    Object.entries(initState).forEach(([k,v]) => {
      state[k] = v
    })
  },
};

const actions = {
  uploadData: async ({commit, getters, state}, historyData) => {
    try{

      /*
      Получаем все группы пакетов

        pack_groups =
      LiveMonitor.Packages.list_pack_group()
      |> LiveMonitor.Repo.preload(:pack_structs)
      |> Enum.map(fn
        g_struct ->
          %{
            link: link,
            title: title,
            is_online: is_online,
            pack_structs: pack_structs,
            id: pack_group_id,
            order_index: order_index
          } = Map.from_struct(g_struct)

          %{
            link: link,
            order_index: order_index,
            title: title,
            is_online: is_online,
            pack_group_id: pack_group_id,
            pack_structs:
              Enum.map(pack_structs, fn struct ->
                %{id: id, is_prod: is_prod, name: name, pack: pack} = Map.from_struct(struct)
                %{id: id, is_prod: is_prod, name: name, pack: pack}
              end)
              |> Enum.sort_by(& &1.pack)
          }
      end)
      |> Enum.sort_by(& &1.order_index)
      */
     
      const packsGroupsData = !isEmpty(packsGroups) ? packsGroups : []

      commit('setPacksGroups', packsGroupsData)
      commit('setPacksGroupsDefault', packsGroupsData)
    } catch (err) {
      console.error(err)
    }
  },
  updateSearch: async ({commit, getters, state}, updatedValue) => {
    try {
      const groups = state.packsGroupsDefault
      const readyValue = updatedValue?.toLowerCase().split(' ')
      if (readyValue.length >= 1) {
        const readyGroups = groups.reduce((acc, group) => {
          const filteredPackStructs = group.pack_structs.reduce((acc, packStruct) => {
            const packStructName = packStruct.name?.toLowerCase()
            console.log('packStructName', packStructName)
            if (packStructName) {
              const isfilteredName = readyValue.every(value => {
                return packStructName.includes(value)
              })
              if (isfilteredName) {
                console.log('filteredName - ready', isfilteredName)
                acc.push(packStruct)
              } else {
                acc
              }
              return acc
            }
          }, [])
          console.log('filteredPackStructs', filteredPackStructs)
          if (filteredPackStructs?.length > 0) {
            acc.push({...group, pack_structs: filteredPackStructs})
          } else {
            acc
          }
          return acc
        }, [])
        // console.log('readyGroups', readyGroups)
        const isOpen = updatedValue && readyGroups.length > 0 ? true : false
        commit('setIsOpen', isOpen)
        commit('setSearchValue', updatedValue)
        commit('setPacksGroups', readyGroups)
      }
    } catch (err) {
      console.error(err)
    }
  },
  resetStore: ({commit}) => {
    commit('resetStore')
  }
};

export const store = {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
};
