"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createToggle = createToggle;
exports.createAddFile = createAddFile;
exports.createRequestFiles = createRequestFiles;
exports.createReceiveFiles = createReceiveFiles;
exports.createInsertFile = createInsertFile;
exports.requestFiles = requestFiles;
exports.createAddFolder = createAddFolder;
exports.createRequestSubfolders = createRequestSubfolders;
exports.createReceiveSubfolders = createReceiveSubfolders;
exports.requestSubfolders = requestSubfolders;
exports.toggle = toggle;
exports.createSetRoot = createSetRoot;
exports.init = init;
exports.SET_ROOT = exports.TOGGLE = exports.REQUEST_SUBFOLDERS = exports.REQUEST_FILES = exports.RECEIVE_SUBFOLDERS = exports.INSERT_FILE = exports.RECEIVE_FILES = exports.ADD_FOLDER = exports.ADD_FILE = 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_FILE = "action.files.add_file";
exports.ADD_FILE = "action.files.add_file";
var ADD_FOLDER = "action.files.add_folder";
exports.ADD_FOLDER = "action.files.add_folder";
var RECEIVE_FILES = "action.files.receive_files";
exports.RECEIVE_FILES = "action.files.receive_files";
var INSERT_FILE = "action.files.insert_file";
exports.INSERT_FILE = "action.files.insert_file";
var RECEIVE_SUBFOLDERS = "action.files.receive_subfolders";
exports.RECEIVE_SUBFOLDERS = "action.files.receive_subfolders";
var REQUEST_FILES = "action.files.request_files";
exports.REQUEST_FILES = "action.files.request_files";
var REQUEST_SUBFOLDERS = "action.files.request_subfolders";
exports.REQUEST_SUBFOLDERS = "action.files.request_subfolders";
var TOGGLE = "action.files.toggle";
exports.TOGGLE = "action.files.toggle";
var SET_ROOT = "action.files.set_root";
exports.SET_ROOT = "action.files.set_root";

function createToggle(id) {
  return {
    type: TOGGLE,
    id: id
  };
}

function createAddFile(_ref) {
  var id = _ref.id,
      name = _ref.name,
      url = _ref.url,
      type = _ref.type,
      embed = _ref.embed;
  return {
    type: ADD_FILE,
    id: id,
    name: name,
    url: url,
    embed: embed,
    fileType: type
  };
}

function createRequestFiles(id) {
  return {
    type: REQUEST_FILES,
    id: id
  };
}

function createReceiveFiles(id, files) {
  return {
    type: RECEIVE_FILES,
    id: id,
    fileIds: files.map(function (file) {
      return file.id;
    })
  };
}

function createInsertFile(id, fileId) {
  return {
    type: INSERT_FILE,
    id: id,
    fileId: fileId
  };
}

function requestFiles(id, bookmark) {
  return function (dispatch, getState) {
    var _getState = getState(),
        source = _getState.source,
        folders = _getState.folders;

    dispatch(createRequestFiles(id));
    return source.fetchFiles(bookmark || folders[id].filesUrl).then(function (_ref2) {
      var files = _ref2.files,
          bookmark = _ref2.bookmark;
      dispatch(files.map(createAddFile).concat(createReceiveFiles(id, files)));

      if (bookmark) {
        // Page through all in folder, pagination links if a tree may be
        // weird, epecially since files and folders are independent.
        dispatch(requestFiles(id, bookmark));
      }
    });
  };
}

function createAddFolder(folder) {
  return {
    type: ADD_FOLDER,
    id: folder.id,
    name: folder.name,
    parentId: folder.parentId,
    filesUrl: folder.filesUrl,
    foldersUrl: folder.foldersUrl
  };
}

function createRequestSubfolders(id) {
  return {
    type: REQUEST_SUBFOLDERS,
    id: id
  };
}

function createReceiveSubfolders(id, folders) {
  return {
    type: RECEIVE_SUBFOLDERS,
    id: id,
    folderIds: folders.map(function (folder) {
      return folder.id;
    })
  };
}

function requestSubfolders(id, bookmark) {
  return function (dispatch, getState) {
    var _getState2 = getState(),
        source = _getState2.source,
        folders = _getState2.folders;

    dispatch(createRequestSubfolders(id));
    return source.fetchPage(bookmark || folders[id].foldersUrl).then(function (_ref3) {
      var folders = _ref3.folders,
          bookmark = _ref3.bookmark;
      dispatch(folders.map(createAddFolder).concat(createReceiveSubfolders(id, folders, bookmark)));

      if (bookmark) {
        // Page through all in folder, pagination links if a tree may be
        // weird, epecially since files and folders are independent.
        dispatch(requestSubfolders(id, bookmark));
      }
    });
  };
}

function toggle(id) {
  return function (dispatch, getState) {
    dispatch(createToggle(id));
    var folder = getState().folders[id];

    if (!folder.requested && folder.expanded) {
      dispatch(requestSubfolders(folder.id));
      dispatch(requestFiles(folder.id));
    }
  };
}

function createSetRoot(id) {
  return {
    type: SET_ROOT,
    id: id
  };
}

function init(dispatch, getState) {
  var props = getState();
  return props.source.fetchRootFolder(props).then(function (_ref4) {
    var folders = _ref4.folders;
    var root = folders[0];

    if (root) {
      dispatch([createAddFolder(root), createSetRoot(root.id)]);
      dispatch(toggle(root.id));
    }
  });
}