"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.requestPage = requestPage;
exports.receivePage = receivePage;
exports.failPage = failPage;
exports.fetchPage = fetchPage;
exports.fetchNextPage = fetchNextPage;
exports.fetchInitialPage = fetchInitialPage;
exports.FAIL_PAGE = exports.RECEIVE_PAGE = exports.REQUEST_PAGE = void 0;

/*
 * Copyright (C) 2018 - present Instructure, Inc.
 *
 * This file is part of Canvas.
 *
 * Canvas is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, version 3 of the License.
 *
 * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */
var REQUEST_PAGE = "REQUEST_PAGE";
exports.REQUEST_PAGE = "REQUEST_PAGE";
var RECEIVE_PAGE = "RECEIVE_PAGE";
exports.RECEIVE_PAGE = "RECEIVE_PAGE";
var FAIL_PAGE = "FAIL_PAGE";
exports.FAIL_PAGE = "FAIL_PAGE";

function requestPage(key) {
  return {
    type: REQUEST_PAGE,
    key: key
  };
}

function receivePage(key, page) {
  var links = page.links,
      bookmark = page.bookmark;
  return {
    type: RECEIVE_PAGE,
    key: key,
    links: links,
    bookmark: bookmark
  };
}

function failPage(key, error) {
  return {
    type: FAIL_PAGE,
    key: key,
    error: error
  };
} // dispatches the start of the load, requests a page for the collection from
// the source, then dispatches the loaded page to the store on success or
// clears the load on failure


function fetchPage(key) {
  return function (dispatch, getState) {
    var _getState = getState(),
        source = _getState.source,
        collections = _getState.collections;

    var bookmark = collections[key].bookmark;
    dispatch(requestPage(key));
    return source.fetchPage(bookmark).then(function (page) {
      return dispatch(receivePage(key, page));
    }).catch(function (error) {
      return dispatch(failPage(key, error));
    });
  };
} // fetches a page only if a page is not already being loaded and the
// collection is not yet completely loaded


function fetchNextPage(key) {
  return function (dispatch, getState) {
    var state = getState();
    var collection = state.collections[key];

    if (collection && !collection.loading && collection.bookmark) {
      return dispatch(fetchPage(key));
    }
  };
} // fetches the next page (subject to conditions on fetchNextPage) only if the
// collection is currently empty


function fetchInitialPage(key) {
  return function (dispatch, getState) {
    var state = getState();
    var collection = state.collections[key];

    if (collection && collection.links.length === 0) {
      return dispatch(fetchNextPage(key));
    }
  };
}