import VapiWrapper from './../VapiWrapper'
import UcebnyMaterial from './../models/UcebnyMaterial'

export default class UcebneMaterialyStore extends VapiWrapper {
  constructor (baseUrl) {
    super (
      baseUrl,
      {
        state: {
          ucebnyMaterial: new UcebnyMaterial({}),
          ucebneMaterialy: []
        }
      }
    )
    .get({
      action: 'ziskajUcebnyMaterial',
      property: 'ucebnyMaterial',
      path: ({id}) => `/ucebne-materialy/${id}`,
      onSuccess: (state, payload) => {
        state.ucebnyMaterial = new UcebnyMaterial(payload.data.data)
      }
    })
    .get({
      action: 'ziskajUcebneMaterialy',
      property: 'ucebneMaterialy',
      path: () => `/ucebne-materialy`,
      onSuccess: (state, payload) => {
        if (Array.isArray(payload.data.data)) {
          state.ucebneMaterialy = payload.data.data.map((ucebnyMaterial) => {
            return new UcebnyMaterial(ucebnyMaterial)
          })
        } else {
          state.ucebneMaterialy = []
        }
      }
    })
  }
}
