var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

import { DragGesture } from '../../gestures/drag-gesture';
import { closest } from '../../util/dom';
var DRAG_THRESHOLD = 20;
var MAX_ATTACK_ANGLE = 20;
export var ItemSlidingGesture = function (_DragGesture) {
    _inherits(ItemSlidingGesture, _DragGesture);

    function ItemSlidingGesture(list, listEle) {
        _classCallCheck(this, ItemSlidingGesture);

        var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(ItemSlidingGesture).call(this, listEle, {
            direction: 'x',
            threshold: DRAG_THRESHOLD
        }));

        _this.list = list;
        _this.listEle = listEle;
        _this.selectedContainer = null;
        _this.openContainer = null;
        _this.listen();
        return _this;
    }

    _createClass(ItemSlidingGesture, [{
        key: 'onTapCallback',
        value: function onTapCallback(ev) {
            if (isFromOptionButtons(ev.target)) {
                return;
            }
            var didClose = this.closeOpened();
            if (didClose) {
                console.debug('tap close sliding item, preventDefault');
                ev.preventDefault();
            }
        }
    }, {
        key: 'onDragStart',
        value: function onDragStart(ev) {
            var angle = Math.abs(ev.angle);
            if (angle > MAX_ATTACK_ANGLE && Math.abs(angle - 180) > MAX_ATTACK_ANGLE) {
                this.closeOpened();
                return false;
            }
            if (this.selectedContainer) {
                console.debug('onDragStart, another container is already selected');
                return false;
            }
            var container = getContainer(ev);
            if (!container) {
                console.debug('onDragStart, no itemContainerEle');
                return false;
            }
            // Close open container if it is not the selected one.
            if (container !== this.openContainer) {
                this.closeOpened();
            }
            // Close all item sliding containers but the selected one
            this.selectedContainer = container;
            this.openContainer = container;
            container.startSliding(ev.center.x);
            return true;
        }
    }, {
        key: 'onDrag',
        value: function onDrag(ev) {
            if (this.selectedContainer) {
                this.selectedContainer.moveSliding(ev.center.x);
                ev.preventDefault();
            }
            return;
        }
    }, {
        key: 'onDragEnd',
        value: function onDragEnd(ev) {
            var _this2 = this;

            if (this.selectedContainer) {
                var openAmount = this.selectedContainer.endSliding(ev.velocityX);
                this.selectedContainer = null;
                // TODO: I am not sure listening for a tap event is the best idea
                // we should try mousedown/touchstart
                if (openAmount === 0) {
                    this.openContainer = null;
                    this.off('tap', this.onTap);
                    this.onTap = null;
                } else if (!this.onTap) {
                    this.onTap = function (event) {
                        return _this2.onTapCallback(event);
                    };
                    this.on('tap', this.onTap);
                }
            }
        }
    }, {
        key: 'closeOpened',
        value: function closeOpened() {
            if (this.openContainer) {
                this.openContainer.close();
                this.openContainer = null;
                this.selectedContainer = null;
                this.off('tap', this.onTap);
                this.onTap = null;
                return true;
            }
            return false;
        }
    }, {
        key: 'unlisten',
        value: function unlisten() {
            _get(Object.getPrototypeOf(ItemSlidingGesture.prototype), 'unlisten', this).call(this);
            this.listEle = null;
        }
    }]);

    return ItemSlidingGesture;
}(DragGesture);
function getContainer(ev) {
    var ele = closest(ev.target, 'ion-item-sliding', true);
    if (ele) {
        return ele['$ionComponent'];
    }
    return null;
}
function isFromOptionButtons(ele) {
    return !!closest(ele, 'ion-item-options', true);
}