"use strict";

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

exports.__esModule = true;
exports.keyboardSortableContext = keyboardSortableContext;

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

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

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

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

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

function keyboardSortableContext(Component) {
  var _class, _temp;

  return _temp = _class =
  /*#__PURE__*/
  function (_React$Component) {
    (0, _inheritsLoose2.default)(KeyboardSortableContext, _React$Component);

    function KeyboardSortableContext(props, context) {
      var _this;

      _this = _React$Component.call(this, props, context) || this;
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "unregister", function (index) {
        var items = _this.state.items;

        if (items[index]) {
          delete items[index];
        }
      });
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "register", function (index, dndId) {
        var items = _this.state.items;
        items[index] = {
          dndId: dndId,
          index: index
        };
      });
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "getPrevDropTarget", function (index) {
        var items = _this.state.items;
        var firstIndex = Math.min.apply(null, Object.keys(items));

        if (index !== firstIndex) {
          for (var i = index - 1; i >= 0; i--) {
            var target = items[i];

            if (target) {
              return target;
            }
          }
        }

        return false;
      });
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "getNextDropTarget", function (index) {
        var items = _this.state.items;
        var lastIndex = Math.max.apply(null, Object.keys(items));

        if (index !== lastIndex) {
          for (var i = index + 1; i <= lastIndex; i++) {
            var target = items[i];

            if (target) {
              return target;
            }
          }
        }

        return false;
      });
      _this.state = {
        items: {}
      };
      return _this;
    }

    var _proto = KeyboardSortableContext.prototype;

    _proto.getChildContext = function getChildContext() {
      return {
        register: this.register,
        unregister: this.unregister,
        getPrevDropTarget: this.getPrevDropTarget,
        getNextDropTarget: this.getNextDropTarget
      };
    };

    _proto.render = function render() {
      return _react.default.createElement("div", {
        role: "select"
      }, _react.default.createElement(Component, this.props));
    };

    return KeyboardSortableContext;
  }(_react.default.Component), (0, _defineProperty2.default)(_class, "childContextTypes", {
    register: _propTypes.default.func.isRequired,
    unregister: _propTypes.default.func.isRequired,
    getPrevDropTarget: _propTypes.default.func.isRequired,
    getNextDropTarget: _propTypes.default.func.isRequired
  }), _temp;
}