import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
export function keyboardSortableContext(Component) {
  var _class, _temp;

  return _temp = _class =
  /*#__PURE__*/
  function (_React$Component) {
    _inheritsLoose(KeyboardSortableContext, _React$Component);

    function KeyboardSortableContext(props, context) {
      var _this;

      _this = _React$Component.call(this, props, context) || this;

      _defineProperty(_assertThisInitialized(_this), "unregister", function (index) {
        var items = _this.state.items;

        if (items[index]) {
          delete items[index];
        }
      });

      _defineProperty(_assertThisInitialized(_this), "register", function (index, dndId) {
        var items = _this.state.items;
        items[index] = {
          dndId: dndId,
          index: index
        };
      });

      _defineProperty(_assertThisInitialized(_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;
      });

      _defineProperty(_assertThisInitialized(_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.createElement("div", {
        role: "select"
      }, React.createElement(Component, this.props));
    };

    return KeyboardSortableContext;
  }(React.Component), _defineProperty(_class, "childContextTypes", {
    register: PropTypes.func.isRequired,
    unregister: PropTypes.func.isRequired,
    getPrevDropTarget: PropTypes.func.isRequired,
    getNextDropTarget: PropTypes.func.isRequired
  }), _temp;
}