"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

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

var actions = _interopRequireWildcard(require("../actions/files"));

/*
 * 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 defaultState = {
  id: null,
  name: null,
  loadingCount: 0,
  loading: false,
  requested: false,
  expanded: false,
  filesUrl: null,
  foldersUrl: null,
  parentId: null,
  fileIds: [],
  folderIds: []
};

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

  switch (action.type) {
    case actions.ADD_FOLDER:
      return (0, _objectSpread2.default)({}, state, {
        id: action.id,
        name: action.name,
        parentId: action.parentId,
        filesUrl: action.filesUrl,
        foldersUrl: action.foldersUrl
      });

    case actions.RECEIVE_FILES:
      loadingCount = state.loadingCount - 1;
      return (0, _objectSpread2.default)({}, state, {
        loadingCount: loadingCount,
        loading: !!loadingCount,
        fileIds: state.fileIds.concat(action.fileIds)
      });

    case actions.INSERT_FILE:
      return (0, _objectSpread2.default)({}, state, {
        fileIds: state.fileIds.concat(action.fileId)
      });

    case actions.RECEIVE_SUBFOLDERS:
      loadingCount = state.loadingCount - 1;
      return (0, _objectSpread2.default)({}, state, {
        loadingCount: loadingCount,
        loading: !!loadingCount,
        folderIds: state.folderIds.concat(action.folderIds)
      });

    case actions.REQUEST_FILES:
      loadingCount = state.loadingCount + 1;
      return (0, _objectSpread2.default)({}, state, {
        requested: true,
        loadingCount: loadingCount,
        loading: !!loadingCount
      });

    case actions.REQUEST_SUBFOLDERS:
      loadingCount = state.loadingCount + 1;
      return (0, _objectSpread2.default)({}, state, {
        requested: true,
        loadingCount: loadingCount,
        loading: !!loadingCount
      });

    case actions.TOGGLE:
      return (0, _objectSpread2.default)({}, state, {
        expanded: !state.expanded
      });

    default:
      return state;
  }
}