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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { Inject, Injectable } from '@angular/core';
import { EMPTY, fromEvent as observableFromEvent, merge as observableMerge, Subject } from 'rxjs';
import { debounceTime, delay, filter, map, repeat, share, switchMap, takeUntil, takeWhile } from 'rxjs/operators';
import { EL_DOCUMENT } from '../../../theme.options';
export var ElTrigger;
(function (ElTrigger) {
    ElTrigger["NOOP"] = "noop";
    ElTrigger["CLICK"] = "click";
    ElTrigger["HOVER"] = "hover";
    ElTrigger["HINT"] = "hint";
    ElTrigger["FOCUS"] = "focus";
})(ElTrigger || (ElTrigger = {}));
/**
 * TODO maybe we have to use renderer.listen instead of observableFromEvent?
 * Renderer provides capability use it in service worker, ssr and so on.
 * */
var ElTriggerStrategyBase = /** @class */ (function () {
    function ElTriggerStrategyBase(document, host, container) {
        this.document = document;
        this.host = host;
        this.container = container;
        this.destroyed$ = new Subject();
    }
    ElTriggerStrategyBase.prototype.destroy = function () {
        this.destroyed$.next();
    };
    ElTriggerStrategyBase.prototype.isNotOnHostOrContainer = function (event) {
        return !this.isOnHost(event) && !this.isOnContainer(event);
    };
    ElTriggerStrategyBase.prototype.isOnHostOrContainer = function (event) {
        return this.isOnHost(event) || this.isOnContainer(event);
    };
    ElTriggerStrategyBase.prototype.isOnHost = function (_a) {
        var target = _a.target;
        return this.host.contains(target);
    };
    ElTriggerStrategyBase.prototype.isOnContainer = function (_a) {
        var target = _a.target;
        return this.container() && this.container().location.nativeElement.contains(target);
    };
    return ElTriggerStrategyBase;
}());
export { ElTriggerStrategyBase };
/**
 * Creates show and hide event streams.
 * Fires toggle event when the click was performed on the host element.
 * Fires close event when the click was performed on the document but
 * not on the host or container.
 * */
var ElClickTriggerStrategy = /** @class */ (function (_super) {
    __extends(ElClickTriggerStrategy, _super);
    function ElClickTriggerStrategy() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        // since we should track click for both SHOW and HIDE event we firstly need to track the click and the state
        // of the container and then later on decide should we hide it or show
        // if we track the click & state separately this will case a behavior when the container is getting shown
        // and then hidden right away
        _this.click$ = observableFromEvent(_this.document, 'click')
            .pipe(map(function (event) { return [!_this.container() && _this.isOnHost(event), event]; }), share(), takeUntil(_this.destroyed$));
        _this.show$ = _this.click$
            .pipe(filter(function (_a) {
            var shouldShow = _a[0];
            return shouldShow;
        }), map(function (_a) {
            var event = _a[1];
            return event;
        }), takeUntil(_this.destroyed$));
        _this.hide$ = _this.click$
            .pipe(filter(function (_a) {
            var shouldShow = _a[0], event = _a[1];
            return !shouldShow && !_this.isOnContainer(event);
        }), map(function (_a) {
            var event = _a[1];
            return event;
        }), takeUntil(_this.destroyed$));
        return _this;
    }
    return ElClickTriggerStrategy;
}(ElTriggerStrategyBase));
export { ElClickTriggerStrategy };
/**
 * Creates show and hide event streams.
 * Fires open event when a mouse hovers over the host element and stay over at least 100 milliseconds.
 * Fires close event when the mouse leaves the host element and stops out of the host and popover container.
 * */
var ElHoverTriggerStrategy = /** @class */ (function (_super) {
    __extends(ElHoverTriggerStrategy, _super);
    function ElHoverTriggerStrategy() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.show$ = observableFromEvent(_this.host, 'mouseenter')
            .pipe(filter(function () { return !_this.container(); }), delay(100), takeUntil(observableMerge(observableFromEvent(_this.host, 'mouseleave'), _this.destroyed$)), repeat());
        _this.hide$ = observableFromEvent(_this.host, 'mouseleave')
            .pipe(switchMap(function () { return observableFromEvent(_this.document, 'mousemove')
            .pipe(debounceTime(100), takeWhile(function () { return !!_this.container(); }), filter(function (event) { return _this.isNotOnHostOrContainer(event); })); }), takeUntil(_this.destroyed$));
        return _this;
    }
    return ElHoverTriggerStrategy;
}(ElTriggerStrategyBase));
export { ElHoverTriggerStrategy };
/**
 * Creates show and hide event streams.
 * Fires open event when a mouse hovers over the host element and stay over at least 100 milliseconds.
 * Fires close event when the mouse leaves the host element.
 * */
var ElHintTriggerStrategy = /** @class */ (function (_super) {
    __extends(ElHintTriggerStrategy, _super);
    function ElHintTriggerStrategy() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.show$ = observableFromEvent(_this.host, 'mouseenter')
            .pipe(delay(100), takeUntil(observableMerge(observableFromEvent(_this.host, 'mouseleave'), _this.destroyed$)), 
        // this `delay & takeUntil & repeat` operators combination is a synonym for `conditional debounce`
        // meaning that if one event occurs in some time after the initial one we won't react to it
        repeat());
        _this.hide$ = observableFromEvent(_this.host, 'mouseleave')
            .pipe(takeUntil(_this.destroyed$));
        return _this;
    }
    return ElHintTriggerStrategy;
}(ElTriggerStrategyBase));
export { ElHintTriggerStrategy };
/**
 * Creates show and hide event streams.
 * Fires open event when a focus is on the host element and stay over at least 100 milliseconds.
 * Fires close event when the focus leaves the host element.
 * */
var ElFocusTriggerStrategy = /** @class */ (function (_super) {
    __extends(ElFocusTriggerStrategy, _super);
    function ElFocusTriggerStrategy() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.focusOut$ = observableFromEvent(_this.host, 'focusout')
            .pipe(switchMap(function () { return observableFromEvent(_this.document, 'focusin')
            .pipe(takeWhile(function () { return !!_this.container(); }), filter(function (event) { return _this.isNotOnHostOrContainer(event); })); }), takeUntil(_this.destroyed$));
        _this.clickIn$ = observableFromEvent(_this.host, 'click')
            .pipe(filter(function () { return !_this.container(); }), takeUntil(_this.destroyed$));
        _this.clickOut$ = observableFromEvent(_this.document, 'click')
            .pipe(filter(function () { return !!_this.container(); }), filter(function (event) { return _this.isNotOnHostOrContainer(event); }), takeUntil(_this.destroyed$));
        _this.tabKeyPress$ = observableFromEvent(_this.document, 'keydown')
            .pipe(filter(function (event) { return event.keyCode === 9; }), filter(function () { return !!_this.container(); }), takeUntil(_this.destroyed$));
        _this.show$ = observableMerge(observableFromEvent(_this.host, 'focusin'), _this.clickIn$)
            .pipe(filter(function () { return !_this.container(); }), debounceTime(100), takeUntil(observableMerge(observableFromEvent(_this.host, 'focusout'), _this.destroyed$)), repeat());
        _this.hide$ = observableMerge(_this.focusOut$, _this.tabKeyPress$, _this.clickOut$)
            .pipe(takeUntil(_this.destroyed$));
        return _this;
    }
    return ElFocusTriggerStrategy;
}(ElTriggerStrategyBase));
export { ElFocusTriggerStrategy };
/**
 * Creates empty show and hide event streams.
 * */
var ElNoopTriggerStrategy = /** @class */ (function (_super) {
    __extends(ElNoopTriggerStrategy, _super);
    function ElNoopTriggerStrategy() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.show$ = EMPTY;
        _this.hide$ = EMPTY;
        return _this;
    }
    return ElNoopTriggerStrategy;
}(ElTriggerStrategyBase));
export { ElNoopTriggerStrategy };
var ElTriggerStrategyBuilderService = /** @class */ (function () {
    function ElTriggerStrategyBuilderService(_document) {
        this._document = _document;
    }
    ElTriggerStrategyBuilderService.prototype.trigger = function (trigger) {
        this._trigger = trigger;
        return this;
    };
    ElTriggerStrategyBuilderService.prototype.host = function (host) {
        this._host = host;
        return this;
    };
    ElTriggerStrategyBuilderService.prototype.container = function (container) {
        this._container = container;
        return this;
    };
    ElTriggerStrategyBuilderService.prototype.build = function () {
        switch (this._trigger) {
            case ElTrigger.CLICK:
                return new ElClickTriggerStrategy(this._document, this._host, this._container);
            case ElTrigger.HINT:
                return new ElHintTriggerStrategy(this._document, this._host, this._container);
            case ElTrigger.HOVER:
                return new ElHoverTriggerStrategy(this._document, this._host, this._container);
            case ElTrigger.FOCUS:
                return new ElFocusTriggerStrategy(this._document, this._host, this._container);
            case ElTrigger.NOOP:
                return new ElNoopTriggerStrategy(this._document, this._host, this._container);
            default:
                throw new Error('Trigger have to be provided');
        }
    };
    ElTriggerStrategyBuilderService = __decorate([
        Injectable(),
        __param(0, Inject(EL_DOCUMENT)),
        __metadata("design:paramtypes", [Object])
    ], ElTriggerStrategyBuilderService);
    return ElTriggerStrategyBuilderService;
}());
export { ElTriggerStrategyBuilderService };
//# sourceMappingURL=overlay-trigger.js.map