"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 _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread"));

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 _Folder = _interopRequireDefault(require("./Folder"));

var _aphrodite = require("aphrodite");

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

/*
 * 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 DOWN_KEY = 40;
var UP_KEY = 38;
var J_KEY = 74;
var K_KEY = 75;

var FileTree =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(FileTree, _Component);

  function FileTree() {
    var _getPrototypeOf2;

    var _this;

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

    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)(FileTree)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _this.handleKeyDown = function (event) {
      switch (event.keyCode) {
        case DOWN_KEY:
        case J_KEY:
          _this.moveFocus(1);

          break;

        case UP_KEY:
        case K_KEY:
          _this.moveFocus(-1);

          break;

        default:
          return;
      }

      event.stopPropagation();
    };

    return _this;
  }

  (0, _createClass2.default)(FileTree, [{
    key: "navigableNodes",
    value: function navigableNodes() {
      return Array.from(this.containerNode.querySelectorAll("button"));
    }
  }, {
    key: "moveFocus",
    value: function moveFocus(delta) {
      var nodes = this.navigableNodes();
      var active = nodes.indexOf(window.document.activeElement);
      var next = active + delta;

      if (next < 0) {
        next = 0;
      } else if (next >= nodes.length) {
        next = nodes.length - 1;
      }

      nodes[next].focus();
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var inlineStyles = {
        maxHeight: this.props.maxHeight
      };
      return _react.default.createElement("div", {
        className: (0, _aphrodite.css)(_styles.default.container),
        ref: function ref(c) {
          return _this2.containerNode = c;
        },
        onKeyDown: this.handleKeyDown,
        style: inlineStyles
      }, _react.default.createElement(_Folder.default, this.props));
    }
  }]);
  FileTree.displayName = "FileTree";
  return FileTree;
}(_react.Component);

exports.default = FileTree;
FileTree.propTypes = (0, _objectSpread2.default)({}, _Folder.default.propTypes, {
  maxHeight: _propTypes.default.string
});