'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.DraggableTabs = undefined;

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _antd = require('antd');

var _reactDnd = require('react-dnd');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

// Drag & Drop node
var TabNode = function (_Component) {
    (0, _inherits3['default'])(TabNode, _Component);

    function TabNode() {
        (0, _classCallCheck3['default'])(this, TabNode);
        return (0, _possibleConstructorReturn3['default'])(this, (TabNode.__proto__ || Object.getPrototypeOf(TabNode)).apply(this, arguments));
    }

    (0, _createClass3['default'])(TabNode, [{
        key: 'render',
        value: function render() {
            var _props = this.props,
                connectDragSource = _props.connectDragSource,
                connectDropTarget = _props.connectDropTarget,
                children = _props.children;

            return connectDragSource(connectDropTarget(children));
        }
    }]);
    return TabNode;
}(_react.Component);

var cardTarget = {
    drop: function drop(props, monitor) {
        var dragKey = monitor.getItem().index;
        var hoverKey = props.index;
        if (dragKey === hoverKey) {
            return;
        }
        props.moveTabNode(dragKey, hoverKey);
        monitor.getItem().index = hoverKey;
    }
};
var cardSource = {
    beginDrag: function beginDrag(props) {
        return {
            id: props.id,
            index: props.index
        };
    }
};
var WrapTabNode = (0, _reactDnd.DropTarget)('DND_NODE', cardTarget, function (connect) {
    return {
        connectDropTarget: connect.dropTarget()
    };
})((0, _reactDnd.DragSource)('DND_NODE', cardSource, function (connect, monitor) {
    return {
        connectDragSource: connect.dragSource(),
        isDragging: monitor.isDragging()
    };
})(TabNode));

var DraggableTabs = exports.DraggableTabs = function (_React$Component) {
    (0, _inherits3['default'])(DraggableTabs, _React$Component);

    function DraggableTabs() {
        (0, _classCallCheck3['default'])(this, DraggableTabs);

        var _this2 = (0, _possibleConstructorReturn3['default'])(this, (DraggableTabs.__proto__ || Object.getPrototypeOf(DraggableTabs)).apply(this, arguments));

        _this2.state = {
            order: []
        };
        _this2.moveTabNode = function (dragKey, hoverKey) {
            var newOrder = _this2.state.order.slice();
            var children = _this2.props.children;

            _react2['default'].Children.forEach(children, function (c) {
                if (newOrder.indexOf(c.key) === -1) {
                    newOrder.push(c.key);
                }
            });
            var dragIndex = newOrder.indexOf(dragKey);
            var hoverIndex = newOrder.indexOf(hoverKey);
            newOrder.splice(dragIndex, 1);
            newOrder.splice(hoverIndex, 0, dragKey);
            _this2.setState({
                order: newOrder
            });
        };
        _this2.renderTabBar = function (props, DefaultTabBar) {
            return _react2['default'].createElement(
                DefaultTabBar,
                props,
                function (node) {
                    return _react2['default'].createElement(
                        WrapTabNode,
                        { key: node.key, index: node.key, moveTabNode: _this2.moveTabNode },
                        node
                    );
                }
            );
        };
        return _this2;
    }

    (0, _createClass3['default'])(DraggableTabs, [{
        key: 'render',
        value: function render() {
            var order = this.state.order;
            var children = this.props.children;

            var tabs = [];
            _react2['default'].Children.forEach(children, function (c) {
                tabs.push(c);
            });
            var orderTabs = tabs.slice().sort(function (a, b) {
                var orderA = order.indexOf(a.key);
                var orderB = order.indexOf(b.key);
                if (orderA !== -1 && orderB !== -1) {
                    return orderA - orderB;
                }
                if (orderA !== -1) {
                    return -1;
                }
                if (orderB !== -1) {
                    return 1;
                }
                var ia = tabs.indexOf(a);
                var ib = tabs.indexOf(b);
                return ia - ib;
            });
            return _react2['default'].createElement(
                _antd.Tabs,
                (0, _extends3['default'])({ renderTabBar: this.renderTabBar }, this.props),
                orderTabs
            );
        }
    }]);
    return DraggableTabs;
}(_react2['default'].Component);