'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.RESET_ERROR_MESSAGE = exports.STARGAZERS_FAILURE = exports.STARGAZERS_SUCCESS = exports.STARGAZERS_REQUEST = exports.STARRED_FAILURE = exports.STARRED_SUCCESS = exports.STARRED_REQUEST = exports.REPO_FAILURE = exports.REPO_SUCCESS = exports.REPO_REQUEST = exports.USER_FAILURE = exports.USER_SUCCESS = exports.USER_REQUEST = exports.ARTICLE_FAILURE = exports.ARTICLE_SUCCESS = exports.ARTICLE_REQUEST = undefined;
exports.loadUser = loadUser;
exports.loadRepo = loadRepo;
exports.loadStarred = loadStarred;
exports.loadStargazers = loadStargazers;
exports.resetErrorMessage = resetErrorMessage;

var _api = require('../middleware/api');

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var ARTICLE_REQUEST = exports.ARTICLE_REQUEST = 'ARTICLE_REQUEST';
var ARTICLE_SUCCESS = exports.ARTICLE_SUCCESS = 'ARTICLE_SUCCESS';
var ARTICLE_FAILURE = exports.ARTICLE_FAILURE = 'ARTICLE_FAILURE';

var USER_REQUEST = exports.USER_REQUEST = 'USER_REQUEST';
var USER_SUCCESS = exports.USER_SUCCESS = 'USER_SUCCESS';
var USER_FAILURE = exports.USER_FAILURE = 'USER_FAILURE';

// Fetches a single user from Github API.
// Relies on the custom API middleware defined in ../middleware/api.js.
function fetchUser(login) {
  return _defineProperty({}, _api.CALL_API, {
    types: [USER_REQUEST, USER_SUCCESS, USER_FAILURE],
    endpoint: 'users/' + login,
    schema: _api.Schemas.USER
  });
}

// Fetches a single user from Github API unless it is cached.
// Relies on Redux Thunk middleware.
function loadUser(login) {
  var requiredFields = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1];

  return function (dispatch, getState) {
    var user = getState().entities.users[login];
    if (user && requiredFields.every(function (key) {
      return user.hasOwnProperty(key);
    })) {
      return null;
    }

    return dispatch(fetchUser(login));
  };
}

var REPO_REQUEST = exports.REPO_REQUEST = 'REPO_REQUEST';
var REPO_SUCCESS = exports.REPO_SUCCESS = 'REPO_SUCCESS';
var REPO_FAILURE = exports.REPO_FAILURE = 'REPO_FAILURE';

// Fetches a single repository from Github API.
// Relies on the custom API middleware defined in ../middleware/api.js.
function fetchRepo(fullName) {
  return _defineProperty({}, _api.CALL_API, {
    types: [REPO_REQUEST, REPO_SUCCESS, REPO_FAILURE],
    endpoint: 'repos/' + fullName,
    schema: _api.Schemas.REPO
  });
}

// Fetches a single repository from Github API unless it is cached.
// Relies on Redux Thunk middleware.
function loadRepo(fullName) {
  var requiredFields = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1];

  return function (dispatch, getState) {
    var repo = getState().entities.repos[fullName];
    if (repo && requiredFields.every(function (key) {
      return repo.hasOwnProperty(key);
    })) {
      return null;
    }

    return dispatch(fetchRepo(fullName));
  };
}

var STARRED_REQUEST = exports.STARRED_REQUEST = 'STARRED_REQUEST';
var STARRED_SUCCESS = exports.STARRED_SUCCESS = 'STARRED_SUCCESS';
var STARRED_FAILURE = exports.STARRED_FAILURE = 'STARRED_FAILURE';

// Fetches a page of starred repos by a particular user.
// Relies on the custom API middleware defined in ../middleware/api.js.
function fetchStarred(login, nextPageUrl) {
  return _defineProperty({
    login: login
  }, _api.CALL_API, {
    types: [STARRED_REQUEST, STARRED_SUCCESS, STARRED_FAILURE],
    endpoint: nextPageUrl,
    schema: _api.Schemas.REPO_ARRAY
  });
}

// Fetches a page of starred repos by a particular user.
// Bails out if page is cached and user didn’t specifically request next page.
// Relies on Redux Thunk middleware.
function loadStarred(login, nextPage) {
  return function (dispatch, getState) {
    var _ref4 = getState().pagination.starredByUser[login] || {};

    var _ref4$nextPageUrl = _ref4.nextPageUrl;
    var nextPageUrl = _ref4$nextPageUrl === undefined ? 'users/' + login + '/starred' : _ref4$nextPageUrl;
    var _ref4$pageCount = _ref4.pageCount;
    var pageCount = _ref4$pageCount === undefined ? 0 : _ref4$pageCount;


    if (pageCount > 0 && !nextPage) {
      return null;
    }

    return dispatch(fetchStarred(login, nextPageUrl));
  };
}

var STARGAZERS_REQUEST = exports.STARGAZERS_REQUEST = 'STARGAZERS_REQUEST';
var STARGAZERS_SUCCESS = exports.STARGAZERS_SUCCESS = 'STARGAZERS_SUCCESS';
var STARGAZERS_FAILURE = exports.STARGAZERS_FAILURE = 'STARGAZERS_FAILURE';

// Fetches a page of stargazers for a particular repo.
// Relies on the custom API middleware defined in ../middleware/api.js.
function fetchStargazers(fullName, nextPageUrl) {
  return _defineProperty({
    fullName: fullName
  }, _api.CALL_API, {
    types: [STARGAZERS_REQUEST, STARGAZERS_SUCCESS, STARGAZERS_FAILURE],
    endpoint: nextPageUrl,
    schema: _api.Schemas.USER_ARRAY
  });
}

// Fetches a page of stargazers for a particular repo.
// Bails out if page is cached and user didn’t specifically request next page.
// Relies on Redux Thunk middleware.
function loadStargazers(fullName, nextPage) {
  return function (dispatch, getState) {
    var _ref6 = getState().pagination.stargazersByRepo[fullName] || {};

    var _ref6$nextPageUrl = _ref6.nextPageUrl;
    var nextPageUrl = _ref6$nextPageUrl === undefined ? 'repos/' + fullName + '/stargazers' : _ref6$nextPageUrl;
    var _ref6$pageCount = _ref6.pageCount;
    var pageCount = _ref6$pageCount === undefined ? 0 : _ref6$pageCount;


    if (pageCount > 0 && !nextPage) {
      return null;
    }

    return dispatch(fetchStargazers(fullName, nextPageUrl));
  };
}

var RESET_ERROR_MESSAGE = exports.RESET_ERROR_MESSAGE = 'RESET_ERROR_MESSAGE';

// Resets the currently visible error message.
function resetErrorMessage() {
  return {
    type: RESET_ERROR_MESSAGE
  };
}