var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
import { Activator } from './activator';
import { CSS, hasPointerMoved, nativeRaf, pointerCoord, rafFrames } from '../../util/dom';
export var RippleActivator = (function (_super) {
    __extends(RippleActivator, _super);
    function RippleActivator(app, config) {
        _super.call(this, app, config);
    }
    RippleActivator.prototype.downAction = function (ev, activatableEle, startCoord) {
        var self = this;
        if (self.disableActivated(ev)) {
            return;
        }
        self._queue.push(activatableEle);
        nativeRaf(function () {
            for (var i = 0; i < self._queue.length; i++) {
                var queuedEle = self._queue[i];
                if (queuedEle && queuedEle.parentNode) {
                    self._active.push(queuedEle);
                    queuedEle.classList.add(self._css);
                    var j = queuedEle.childElementCount;
                    while (j--) {
                        var rippleEle = queuedEle.children[j];
                        if (rippleEle.classList.contains('button-effect')) {
                            rippleEle.style.left = '-9999px';
                            rippleEle.style.opacity = '';
                            rippleEle.style[CSS.transform] = 'scale(0.001) translateZ(0px)';
                            rippleEle.style[CSS.transition] = '';
                            var clientRect = activatableEle.getBoundingClientRect();
                            rippleEle.$top = clientRect.top;
                            rippleEle.$left = clientRect.left;
                            rippleEle.$width = clientRect.width;
                            rippleEle.$height = clientRect.height;
                            break;
                        }
                    }
                }
            }
            self._queue = [];
        });
    };
    RippleActivator.prototype.upAction = function (ev, activatableEle, startCoord) {
        if (!hasPointerMoved(6, startCoord, pointerCoord(ev))) {
            var i = activatableEle.childElementCount;
            while (i--) {
                var rippleEle = activatableEle.children[i];
                if (rippleEle.classList.contains('button-effect')) {
                    var clientPointerX = (startCoord.x - rippleEle.$left);
                    var clientPointerY = (startCoord.y - rippleEle.$top);
                    var x = Math.max(Math.abs(rippleEle.$width - clientPointerX), clientPointerX) * 2;
                    var y = Math.max(Math.abs(rippleEle.$height - clientPointerY), clientPointerY) * 2;
                    var diameter = Math.min(Math.max(Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)), 64), 240);
                    if (activatableEle.hasAttribute('ion-item')) {
                        diameter = Math.min(diameter, 140);
                    }
                    var radius = Math.sqrt(rippleEle.$width + rippleEle.$height);
                    var scaleTransitionDuration = Math.max(1600 * Math.sqrt(radius / TOUCH_DOWN_ACCEL) + 0.5, 260);
                    var opacityTransitionDuration = scaleTransitionDuration * 0.7;
                    var opacityTransitionDelay = scaleTransitionDuration - opacityTransitionDuration;
                    rippleEle.style.width = rippleEle.style.height = diameter + 'px';
                    rippleEle.style.marginTop = rippleEle.style.marginLeft = -(diameter / 2) + 'px';
                    rippleEle.style.left = clientPointerX + 'px';
                    rippleEle.style.top = clientPointerY + 'px';
                    rippleEle.style.opacity = '0';
                    rippleEle.style[CSS.transform] = 'scale(1) translateZ(0px)';
                    rippleEle.style[CSS.transition] = 'transform ' +
                        scaleTransitionDuration +
                        'ms,opacity ' +
                        opacityTransitionDuration +
                        'ms ' +
                        opacityTransitionDelay + 'ms';
                }
            }
        }
        _super.prototype.upAction.call(this, ev, activatableEle, startCoord);
    };
    RippleActivator.prototype.deactivate = function () {
        var self = this;
        self._queue = [];
        rafFrames(2, function () {
            for (var i = 0; i < self._active.length; i++) {
                self._active[i].classList.remove(self._css);
            }
            self._active = [];
        });
    };
    return RippleActivator;
}(Activator));
var TOUCH_DOWN_ACCEL = 300;
