import {LOADED_ALL_RESULTS, LOADED_ALL_TOPICS, START_LOADING_ALL_RESULTS} from "./action_types";
import {fetchVideosByTopic} from "../utils/api";
import {APIError} from "./errors";
import {setItem} from "../utils/safe-storage";

export function getSearchResults(categoryKey, searchValue) {
  // get the stuff from the api
  // dispatch loaded

  switch (categoryKey) {
    case 'topic':
      console.log('topic')
      return dispatch => {
        dispatch(loadingAllResults())
        // this calls the API
        fetchVideosByTopic(searchValue)
          .then(videos => {
            dispatch(loadedResults(videos));
          }).catch(error => {
            dispatch(APIError(error));
          })
      };
    case 'date':
      console.log('date')
    case 'location':
      console.log('location')
    default:
      console.log('none')
  }

}
function loadingAllResults() {
  return {
    type: START_LOADING_ALL_RESULTS
  }  
}

function loadedResults(videos) {
  return {
    type: LOADED_ALL_RESULTS,
    payload: videos
  };
};