import { createSelector } from 'reselect';
import produce from 'immer';
import { all, takeLatest, call, put } from 'redux-saga/effects';
import { A as API_PREFIX } from './index-9c274173.js';
import { r as request } from './request-a3c74421.js';

/*
 * AppConstants
 * Each action has a corresponding type, which the reducer knows and picks up on.
 * To avoid weird typos between the reducer and the actions, we save them as
 * constants here. We prefix them with 'yourproject/YourComponent' so we avoid
 * reducers accidentally picking up actions they shouldn't.
 *
 * Follow this format:
 * export const YOUR_ACTION_CONSTANT = 'yourproject/YourContainer/YOUR_ACTION_CONSTANT';
 */
const LOAD_TOPICS = 'reactCommentsDjango/Topics/LOAD_TOPICS';
const LOAD_TOPICS_SUCCESS = 'reactCommentsDjango/Topics/LOAD_TOPICS_SUCCESS';
const LOAD_TOPICS_ERROR = 'reactCommentsDjango/Topics/LOAD_TOPICS_ERROR';
const LOAD_TOPIC = 'reactCommentsDjango/Topics/LOAD_TOPIC';
const LOAD_TOPIC_SUCCESS = 'reactCommentsDjango/Topics/LOAD_TOPIC_SUCCESS';
const LOAD_TOPIC_ERROR = 'reactCommentsDjango/Topics/LOAD_TOPIC_ERROR';

/*
 * TopicListReducer
 *
 * The reducer takes care of our data. Using actions, we can
 * update our application state. To add a new action,
 * add it to the switch statement in the reducer function
 *
 */

const initialState = {
  topicsList: false,
  topic: false
};
/* eslint-disable default-case, no-param-reassign */

const topicsListReducer = (state = initialState, action) => produce(state, draft => {
  switch (action.type) {
    case LOAD_TOPICS:
      draft.loading = true;
      draft.error = false;
      draft.topicsList = false;
      break;

    case LOAD_TOPICS_SUCCESS:
      draft.topicsList = action.topicsList;
      draft.loading = false;
      break;

    case LOAD_TOPICS_ERROR:
      draft.error = action.error;
      draft.loading = false;
      break;

    case LOAD_TOPIC:
      draft.loading = true;
      draft.error = false;
      draft.topic = false;
      break;

    case LOAD_TOPIC_SUCCESS:
      draft.topic = action.topic;
      draft.loading = false;
      break;

    case LOAD_TOPIC_ERROR:
      draft.error = action.error;
      draft.loading = false;
      break;
  }
});

/**
 * topics list selectors
 */

const selectTopics = state => state.topics || initialState;

const makeSelectTopicsList = () => createSelector(selectTopics, topicsState => topicsState.topicsList);

const makeSelectTopic = () => createSelector(selectTopics, topicsState => topicsState.topic);

/*
 * App Actions
 *
 * Actions change things in your application
 * Since this boilerplate uses a uni-directional data flow, specifically redux,
 * we have these actions which are the only way your application interacts with
 * your application state. This guarantees that your state is up to date and nobody
 * messes it up weirdly somewhere.
 *
 * To add a new Action:
 * 1) Import your constant
 * 2) Add a function like this:
 *    export function yourAction(var) {
 *        return { type: YOUR_ACTION_CONSTANT, var: var }
 *    }
 */
/**
 * Load the topics List, this action starts the request saga
 *
 * @return {object} An action object with a type of LOAD_TOPICS
 */

function loadTopics(nextHref) {
  return {
    type: LOAD_TOPICS,
    nextHref
  };
}
/**
 * Dispatched when the topics List are loaded by the request saga
 *
 * @param  {array} topicsList The topics List data
 *
 * @return {object}      An action object with a type of LOAD_TOPICS_SUCCESS passing the topicsList
 */

function topicsListLoaded(topicsList) {
  return {
    type: LOAD_TOPICS_SUCCESS,
    topicsList
  };
}
/**
 * Dispatched when loading the topicsListitories fails
 *
 * @param  {object} error The error
 *
 * @return {object}       An action object with a type of LOAD_TOPICS_ERROR passing the error
 */

function topicsListLoadingError(error) {
  return {
    type: LOAD_TOPICS_ERROR,
    error
  };
}
function loadTopic(slug) {
  return {
    type: LOAD_TOPIC,
    slug
  };
}
function topicLoaded(topic) {
  return {
    type: LOAD_TOPIC_SUCCESS,
    topic
  };
}
function topicLoadingError(error) {
  return {
    type: LOAD_TOPIC_ERROR,
    error
  };
}

var loadTopicsActionsCreator = /*#__PURE__*/Object.freeze({
  __proto__: null,
  loadTopics: loadTopics,
  topicsListLoaded: topicsListLoaded,
  topicsListLoadingError: topicsListLoadingError,
  loadTopic: loadTopic,
  topicLoaded: topicLoaded,
  topicLoadingError: topicLoadingError
});

function getTopics(nextHref) {
  let url;

  if (nextHref) {
    url = nextHref;
  } else {
    url = `${API_PREFIX}topics/`;
  }

  return request(url, {
    method: 'GET',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      'X-Requested-With': 'XMLHttpRequest'
    }
  });
}

function getTopic$1(slug) {
  const url = `${API_PREFIX}topics/${slug}/`;
  return request(url, {
    method: 'GET',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      'X-Requested-With': 'XMLHttpRequest'
    }
  });
}

const Api = {
  getTopics,
  getTopic: getTopic$1
};

function* getTopicsList(sagaArgs) {
  const {
    nextHref
  } = sagaArgs;

  try {
    const topicsList = yield call(Api.getTopics, nextHref);
    yield put(topicsListLoaded(topicsList));
  } catch (err) {
    yield put(topicsListLoadingError(err));
  }
}
function* getTopic(sagaArgs) {
  const {
    slug
  } = sagaArgs;

  try {
    const topic = yield call(Api.getTopic, slug);
    yield put(topicLoaded(topic));
  } catch (err) {
    yield put(topicLoadingError(err));
  }
}
/**
 * Root saga manages watcher lifecycle
 */

function* topicsData() {
  // Watches for LOAD_REPOS actions and calls getTopicsList when one comes in.
  // By using `takeLatest` only the result of the latest API call is applied.
  // It returns task descriptor (just like fork) so we can continue execution
  // It will be cancelled automatically on component unmount
  // yield takeLatest(LOAD_TOPICS, getTopicsList)
  // yield takeLatest(LOAD_TOPIC, getTopic)
  yield all([takeLatest(LOAD_TOPICS, getTopicsList), takeLatest(LOAD_TOPIC, getTopic)]);
}

export { topicsData as a, makeSelectTopicsList as b, loadTopicsActionsCreator as l, makeSelectTopic as m, topicsListReducer as t };
//# sourceMappingURL=saga-f1165c7a.js.map
