"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.startFlickrSearch = startFlickrSearch;
exports.receiveFlickrResults = receiveFlickrResults;
exports.failFlickrSearch = failFlickrSearch;
exports.openOrCloseFlickrForm = openOrCloseFlickrForm;
exports.searchFlickr = searchFlickr;
exports.TOGGLE_FLICKR_FORM = exports.FAIL_FLICKR_SEARCH = exports.RECEIVE_FLICKR_RESULTS = exports.START_FLICKR_SEARCH = 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 START_FLICKR_SEARCH = "START_FLICKR_SEARCH";
exports.START_FLICKR_SEARCH = "START_FLICKR_SEARCH";
var RECEIVE_FLICKR_RESULTS = "RECEIVE_FLICKR_RESULTS";
exports.RECEIVE_FLICKR_RESULTS = "RECEIVE_FLICKR_RESULTS";
var FAIL_FLICKR_SEARCH = "FAIL_FLICKR_SEARCH";
exports.FAIL_FLICKR_SEARCH = "FAIL_FLICKR_SEARCH";
var TOGGLE_FLICKR_FORM = "TOGGLE_FLICKR_FORM";
exports.TOGGLE_FLICKR_FORM = "TOGGLE_FLICKR_FORM";

function startFlickrSearch(term) {
  return {
    type: START_FLICKR_SEARCH,
    term: term
  };
}

function receiveFlickrResults(results) {
  return {
    type: RECEIVE_FLICKR_RESULTS,
    results: results
  };
}

function failFlickrSearch(error) {
  return {
    type: FAIL_FLICKR_SEARCH,
    error: error
  };
}

function openOrCloseFlickrForm() {
  return {
    type: TOGGLE_FLICKR_FORM
  };
}

function searchFlickr(term) {
  return function (dispatch, getState) {
    var _getState = getState(),
        source = _getState.source,
        host = _getState.host,
        flickr = _getState.flickr;

    if (flickr && !flickr.searching) {
      dispatch(startFlickrSearch(term));
      return source.searchFlickr(term, {
        host: host
      }).then(function (results) {
        return dispatch(receiveFlickrResults(results));
      }).catch(function (error) {
        return dispatch(failFlickrSearch(error));
      });
    }
  };
}