"use strict";

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

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

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

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

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

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

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _react = _interopRequireWildcard(require("react"));

var _File = _interopRequireDefault(require("./File"));

var _Loading = _interopRequireDefault(require("../Loading"));

var _aphrodite = require("aphrodite");

var _styles = _interopRequireDefault(require("./styles"));

var _IconMiniArrowDown = _interopRequireDefault(require("@instructure/ui-icons/lib/Line/IconMiniArrowDown"));

var _IconMiniArrowEnd = _interopRequireDefault(require("@instructure/ui-icons/lib/Line/IconMiniArrowEnd"));

var _IconFolder = _interopRequireDefault(require("@instructure/ui-icons/lib/Line/IconFolder"));

/*
 * 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 Folder =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(Folder, _Component);

  function Folder() {
    var _getPrototypeOf2;

    var _this;

    (0, _classCallCheck2.default)(this, Folder);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = (0, _possibleConstructorReturn2.default)(this, (_getPrototypeOf2 = (0, _getPrototypeOf2.default)(Folder)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _this.handleToggle = function () {
      var _this$props = _this.props,
          onToggle = _this$props.onToggle,
          folder = _this$props.folder;

      if (onToggle) {
        onToggle(folder.id);
      }
    };

    return _this;
  }

  (0, _createClass2.default)(Folder, [{
    key: "files",
    value: function files() {
      var _this2 = this;

      return this.props.folder.fileIds.map(function (id) {
        return _this2.props.files[id];
      }).filter(function (file) {
        return file != null;
      });
    }
  }, {
    key: "subFolders",
    value: function subFolders() {
      var _this3 = this;

      return this.props.folder.folderIds.map(function (id) {
        return _this3.props.folders[id];
      }).filter(function (folder) {
        return folder != null;
      });
    }
  }, {
    key: "toggleIcon",
    value: function toggleIcon() {
      var expanded = this.props.folder.expanded;
      return expanded ? _react.default.createElement(_IconMiniArrowDown.default, null) : _react.default.createElement(_IconMiniArrowEnd.default, null);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          folders = _this$props2.folders,
          folder = _this$props2.folder,
          files = _this$props2.files,
          onSelect = _this$props2.onSelect,
          onToggle = _this$props2.onToggle;
      return _react.default.createElement("div", {
        className: (0, _aphrodite.css)(_styles.default.node)
      }, _react.default.createElement("button", {
        className: (0, _aphrodite.css)(_styles.default.button),
        onClick: this.handleToggle,
        "aria-expanded": !!folder.expanded
      }, this.toggleIcon(), " ", _react.default.createElement(_IconFolder.default, null), " ", folder.name), folder.expanded && _react.default.createElement("ul", {
        className: (0, _aphrodite.css)(_styles.default.list)
      }, this.subFolders().map(function (folder) {
        return _react.default.createElement("li", {
          key: "folder-".concat(folder.id),
          className: (0, _aphrodite.css)(_styles.default.node)
        }, _react.default.createElement(Folder, {
          folders: folders,
          files: files,
          folder: folder,
          onToggle: onToggle,
          onSelect: onSelect
        }));
      }), this.files().map(function (file) {
        return _react.default.createElement("li", {
          key: "file-".concat(file.id),
          className: (0, _aphrodite.css)(_styles.default.node)
        }, _react.default.createElement(_File.default, {
          onSelect: onSelect,
          file: file
        }));
      })), folder.expanded && folder.loading && _react.default.createElement(_Loading.default, {
        className: (0, _aphrodite.css)(_styles.default.loading)
      }));
    }
  }]);
  Folder.displayName = "Folder";
  return Folder;
}(_react.Component);

exports.default = Folder;

var folderPropType = _propTypes.default.shape({
  id: _propTypes.default.number,
  name: _propTypes.default.string,
  loading: _propTypes.default.bool,
  fileIds: _propTypes.default.arrayOf(_propTypes.default.number),
  folderIds: _propTypes.default.arrayOf(_propTypes.default.number)
});

Folder.propTypes = {
  folders: _propTypes.default.objectOf(folderPropType),
  files: _propTypes.default.objectOf(_File.default.propTypes.file),
  folder: folderPropType.isRequired,
  onToggle: _propTypes.default.func,
  onSelect: _File.default.propTypes.onSelect
};
Folder.defaultProps = {
  files: [],
  folders: []
};