"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Observable_1 = require("rxjs/Observable");
require("rxjs/add/observable/fromEvent");
require("rxjs/add/operator/filter");
require("rxjs/add/operator/map");
require("rxjs/add/operator/startWith");
require("rxjs/add/operator/takeUntil");
function touchMatcher(touch) {
    return ev => Array.from(ev.changedTouches).find(t => t.identifier === touch.identifier);
}
/**
 * Captures a MouseEvent or TouchEvent (from touchstart or mousedown) and
 * returns an observable that emits when the pointer or finger is dragged.
 */
function captureDrag(ev) {
    ev.preventDefault();
    if (ev instanceof MouseEvent) {
        return Observable_1.Observable.fromEvent(window, 'mousemove')
            .startWith(ev)
            .takeUntil(dragCancelled(ev));
    }
    const match = touchMatcher(ev.touches[0]);
    return Observable_1.Observable.fromEvent(window, 'touchmove')
        .startWith(ev)
        .map(match)
        .filter(Boolean)
        .takeUntil(dragCancelled(ev));
}
exports.captureDrag = captureDrag;
/**
 * Returns an observable that fires once the drag stops.
 */
function dragCancelled(ev) {
    return ev instanceof MouseEvent
        ? Observable_1.Observable.fromEvent(window, 'mouseup')
        : Observable_1.Observable.fromEvent(window, 'touchend')
            .map(touchMatcher(ev.touches[0]))
            .filter(Boolean);
}
exports.dragCancelled = dragCancelled;
