var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import React from 'react';
import PropTypes from 'prop-types';
export function keyboardSortableContext(Component) {
    var _a;
    return _a = /** @class */ (function (_super) {
            __extends(KeyboardSortableContext, _super);
            function KeyboardSortableContext(props, context) {
                var _this = _super.call(this, props, context) || this;
                _this.unregister = function (index) {
                    var items = _this.state.items;
                    if (items[index]) {
                        delete items[index];
                    }
                };
                _this.register = function (index, dndId) {
                    var items = _this.state.items;
                    items[index] = {
                        dndId: dndId,
                        index: index
                    };
                };
                _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;
                };
                _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;
            }
            KeyboardSortableContext.prototype.getChildContext = function () {
                return {
                    register: this.register,
                    unregister: this.unregister,
                    getPrevDropTarget: this.getPrevDropTarget,
                    getNextDropTarget: this.getNextDropTarget
                };
            };
            KeyboardSortableContext.prototype.render = function () {
                return (React.createElement("div", { role: "select" },
                    React.createElement(Component, __assign({}, this.props))));
            };
            return KeyboardSortableContext;
        }(React.Component)),
        _a.childContextTypes = {
            register: PropTypes.func.isRequired,
            unregister: PropTypes.func.isRequired,
            getPrevDropTarget: PropTypes.func.isRequired,
            getNextDropTarget: PropTypes.func.isRequired
        },
        _a;
}
//# sourceMappingURL=keyboardSortableContext.js.map