"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread"));

var _upload = require("../actions/upload");

var _redux = require("redux");

/*
 * 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/>.
 */
function uploading() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  var action = arguments.length > 1 ? arguments[1] : undefined;

  switch (action.type) {
    case _upload.START_FILE_UPLOAD:
      return true;

    case _upload.FAIL_FILE_UPLOAD:
    case _upload.COMPLETE_FILE_UPLOAD:
    case _upload.QUOTA_EXCEEDED_UPLOAD:
      return false;

    default:
      return state;
  }
}

function error() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var action = arguments.length > 1 ? arguments[1] : undefined;

  switch (action.type) {
    case _upload.COMPLETE_FILE_UPLOAD:
      return {};

    case _upload.QUOTA_EXCEEDED_UPLOAD:
      return (0, _objectSpread2.default)({}, state, {
        type: action.type
      });

    default:
      return state;
  }
}

function formExpanded() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  var action = arguments.length > 1 ? arguments[1] : undefined;

  switch (action.type) {
    case _upload.COMPLETE_FILE_UPLOAD:
      return false;

    case _upload.TOGGLE_UPLOAD_FORM:
      return !state;

    default:
      return state;
  }
}

function folders() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var action = arguments.length > 1 ? arguments[1] : undefined;

  switch (action.type) {
    case _upload.RECEIVE_FOLDER:
      return (0, _objectSpread2.default)({}, state, (0, _defineProperty2.default)({}, action.id, {
        id: action.id,
        name: action.name,
        parentId: action.parentId
      }));

    case _upload.FAIL_FOLDERS_LOAD:
    default:
      return state;
  }
}

function rootFolderId() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  var action = arguments.length > 1 ? arguments[1] : undefined;

  switch (action.type) {
    case _upload.RECEIVE_FOLDER:
      if (action.parentId === null) {
        return action.id;
      } else {
        return state;
      }

    default:
      return state;
  }
} // Returns an mapping of folder id -> list of children ids,
// with the children sorted alphabetically by name.


function folderTree() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var action = arguments.length > 1 ? arguments[1] : undefined;

  switch (action.type) {
    case _upload.PROCESSED_FOLDER_BATCH:
      {
        var _ret = function () {
          var folders = action.folders;
          var tree = {};

          for (var folderId in folders) {
            var folder = folders[folderId];
            tree[folder.id] = tree[folder.id] || [];

            if (folder.parentId) {
              tree[folder.parentId] = tree[folder.parentId] || [];
              tree[folder.parentId].push(folder.id);
            }
          }

          for (var parentFolderId in tree) {
            var children = tree[parentFolderId];
            children.sort(function (a, b) {
              return folders[a].name.localeCompare(folders[b].name);
            });
          }

          return {
            v: tree
          };
        }();

        if (typeof _ret === "object") return _ret.v;
      }

    default:
      return state;
  }
}

function loadingFolders() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  var action = arguments.length > 1 ? arguments[1] : undefined;

  switch (action.type) {
    case _upload.START_LOADING:
      return true;

    case _upload.STOP_LOADING:
      return false;

    case _upload.FAIL_FOLDERS_LOAD:
      {
        return false;
      }

    default:
      return state;
  }
}

var _default = (0, _redux.combineReducers)({
  uploading: uploading,
  formExpanded: formExpanded,
  folders: folders,
  rootFolderId: rootFolderId,
  folderTree: folderTree,
  error: error,
  loadingFolders: loadingFolders
});

exports.default = _default;