"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createAddImage = createAddImage;
exports.requestImages = requestImages;
exports.receiveImages = receiveImages;
exports.failImagesLoad = failImagesLoad;
exports.fetchImages = fetchImages;
exports.FAIL_IMAGES_LOAD = exports.RECEIVE_IMAGES = exports.REQUEST_IMAGES = exports.ADD_IMAGE = 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 ADD_IMAGE = "action.images.add_image";
exports.ADD_IMAGE = "action.images.add_image";
var REQUEST_IMAGES = "action.images.request_images";
exports.REQUEST_IMAGES = "action.images.request_images";
var RECEIVE_IMAGES = "action.images.receive_images";
exports.RECEIVE_IMAGES = "action.images.receive_images";
var FAIL_IMAGES_LOAD = "action.images.fail_images_load";
exports.FAIL_IMAGES_LOAD = "action.images.fail_images_load";

function createAddImage(_ref) {
  var id = _ref.id,
      filename = _ref.filename,
      display_name = _ref.display_name,
      url = _ref.url,
      thumbnail_url = _ref.thumbnail_url;
  return {
    type: ADD_IMAGE,
    id: id,
    filename: filename,
    display_name: display_name,
    preview_url: url,
    thumbnail_url: thumbnail_url
  };
}

function requestImages(bookmark) {
  return {
    type: REQUEST_IMAGES,
    bookmark: bookmark
  };
}

function receiveImages(response) {
  return {
    type: RECEIVE_IMAGES,
    imageRecords: response.images,
    bookmark: response.bookmark
  };
}

function failImagesLoad(error) {
  return {
    type: FAIL_IMAGES_LOAD,
    error: error
  };
} // gets images to embed for context


function fetchImages(fetchEvent) {
  return function (dispatch, getState) {
    var calledFromRender = fetchEvent.calledFromRender;

    var _getState = getState(),
        source = _getState.source,
        images = _getState.images,
        host = _getState.host,
        contextId = _getState.contextId,
        contextType = _getState.contextType;

    if (!images.requested) {
      dispatch(requestImages(images.bookmark));
      return source.fetchImages({
        host: host,
        contextId: contextId,
        contextType: contextType
      }).then(function (imageBody) {
        dispatch(receiveImages(imageBody));
      }).catch(function (error) {
        dispatch(failImagesLoad(error));
      });
    } else if (!calledFromRender && images.hasMore) {
      dispatch(requestImages(images.bookmark));
      return source.fetchImages({
        bookmark: images.bookmark
      }).then(function (imageBody) {
        dispatch(receiveImages(imageBody));
      }).catch(function (error) {
        dispatch(failImagesLoad(error));
      });
    } else {
      return new Promise(function (resolve) {
        resolve();
      });
    }
  };
}