import { Action, handleActions } from 'redux-actions'
import {
  setRyeShopifyStoreType,
  triggerRyeCheckCollectionType
} from './actions'
import { RyeShopifyState } from './types'

const handlers = {
  [setRyeShopifyStoreType]: (
    state: RyeShopifyState,
    { payload }: Action<any>
  ): RyeShopifyState => {
    return {
      ...state,
      error: null,
      stores: payload
    }
  },
  [triggerRyeCheckCollectionType]: (
    state: RyeShopifyState,
    { payload }: Action<any>
  ): RyeShopifyState => {
    return {
      ...state,
      error: null,
      trigger: payload
    }
  }
}

const defaultState: RyeShopifyState = {
  loading: false,
  error: null,
  stores: []
}

export const ryeShopifyReducer = handleActions<RyeShopifyState>(
  handlers,
  defaultState
)
