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);
};
import { Injectable, SimpleChange } from '@angular/core';
import { ElTrigger, ElTriggerStrategyBuilderService } from '../overlay-trigger';
import { ElAdjustment, ElPosition, ElPositionBuilderService, } from '../overlay-position';
import { ElDynamicOverlay } from './dynamic-overlay';
var ElDynamicOverlayChange = /** @class */ (function (_super) {
    __extends(ElDynamicOverlayChange, _super);
    function ElDynamicOverlayChange(previousValue, currentValue, firstChange) {
        if (firstChange === void 0) { firstChange = false; }
        return _super.call(this, previousValue, currentValue, firstChange) || this;
    }
    ElDynamicOverlayChange.prototype.isChanged = function () {
        return this.currentValue !== this.previousValue;
    };
    return ElDynamicOverlayChange;
}(SimpleChange));
export { ElDynamicOverlayChange };
var ElDynamicOverlayHandler = /** @class */ (function () {
    function ElDynamicOverlayHandler(positionBuilder, triggerStrategyBuilder, dynamicOverlayService) {
        this.positionBuilder = positionBuilder;
        this.triggerStrategyBuilder = triggerStrategyBuilder;
        this.dynamicOverlayService = dynamicOverlayService;
        this._context = {};
        this._trigger = ElTrigger.NOOP;
        this._position = ElPosition.TOP;
        this._adjustment = ElAdjustment.NOOP;
        this._offset = 15;
        this._overlayConfig = {};
        this.changes = {};
    }
    ElDynamicOverlayHandler.prototype.host = function (host) {
        this.changes.host = new ElDynamicOverlayChange(this._host, host);
        this._host = host;
        return this;
    };
    ElDynamicOverlayHandler.prototype.trigger = function (trigger) {
        this.changes.trigger = new ElDynamicOverlayChange(this._trigger, trigger);
        this._trigger = trigger;
        return this;
    };
    ElDynamicOverlayHandler.prototype.position = function (position) {
        this.changes.position = new ElDynamicOverlayChange(this._position, position);
        this._position = position;
        return this;
    };
    ElDynamicOverlayHandler.prototype.adjustment = function (adjustment) {
        this.changes.adjustment = new ElDynamicOverlayChange(this._adjustment, adjustment);
        this._adjustment = adjustment;
        return this;
    };
    ElDynamicOverlayHandler.prototype.componentType = function (componentType) {
        this.changes.componentType = new ElDynamicOverlayChange(this._componentType, componentType);
        this._componentType = componentType;
        return this;
    };
    ElDynamicOverlayHandler.prototype.content = function (content) {
        this.changes.content = new ElDynamicOverlayChange(this._content, content);
        this._content = content;
        return this;
    };
    ElDynamicOverlayHandler.prototype.context = function (context) {
        this.changes.context = new ElDynamicOverlayChange(this._context, context);
        this._context = context;
        return this;
    };
    ElDynamicOverlayHandler.prototype.offset = function (offset) {
        this.changes.offset = new ElDynamicOverlayChange(this._offset, offset);
        this._offset = offset;
        return this;
    };
    ElDynamicOverlayHandler.prototype.overlayConfig = function (overlayConfig) {
        this.changes.overlayConfig = new ElDynamicOverlayChange(this._overlayConfig, overlayConfig);
        this._overlayConfig = overlayConfig;
        return this;
    };
    ElDynamicOverlayHandler.prototype.build = function () {
        if (!this._componentType || !this._host) {
            throw Error("ElDynamicOverlayHandler: at least 'componentType' and 'host' should be\n      passed before building a dynamic overlay.");
        }
        this.dynamicOverlay = this.dynamicOverlayService.create(this._componentType, this._content, this._context, this.createPositionStrategy(), this._overlayConfig);
        this.connect();
        this.clearChanges();
        return this.dynamicOverlay;
    };
    ElDynamicOverlayHandler.prototype.rebuild = function () {
        /**
         * we should not throw here
         * as we use rebuilt in lifecycle hooks
         * which it could be called before the build
         * so we just ignore this call
         */
        if (!this.dynamicOverlay) {
            return;
        }
        if (this.isPositionStrategyUpdateRequired()) {
            this.dynamicOverlay.setPositionStrategy(this.createPositionStrategy());
        }
        if (this.isTriggerStrategyUpdateRequired()) {
            this.connect();
        }
        if (this.isContainerRerenderRequired()) {
            this.dynamicOverlay.setContentAndContext(this._content, this._context);
        }
        if (this.isComponentTypeUpdateRequired()) {
            this.dynamicOverlay.setComponent(this._componentType);
        }
        if (this.isOverlayConfigUpdateRequired()) {
            this.dynamicOverlay.setOverlayConfig(this._overlayConfig);
        }
        this.clearChanges();
        return this.dynamicOverlay;
    };
    ElDynamicOverlayHandler.prototype.connect = function () {
        if (!this.dynamicOverlay) {
            throw new Error("ElDynamicOverlayHandler: cannot connect to DynamicOverlay\n      as it is not created yet. Call build() first");
        }
        this.disconnect();
        this.subscribeOnTriggers(this.dynamicOverlay);
    };
    ElDynamicOverlayHandler.prototype.disconnect = function () {
        if (this.triggerStrategy) {
            this.triggerStrategy.destroy();
        }
    };
    ElDynamicOverlayHandler.prototype.destroy = function () {
        this.disconnect();
        this.clearChanges();
        if (this.dynamicOverlay) {
            this.dynamicOverlay.dispose();
        }
    };
    ElDynamicOverlayHandler.prototype.createPositionStrategy = function () {
        return this.positionBuilder
            .connectedTo(this._host)
            .position(this._position)
            .adjustment(this._adjustment)
            .offset(this._offset);
    };
    ElDynamicOverlayHandler.prototype.subscribeOnTriggers = function (dynamicOverlay) {
        this.triggerStrategy = this.triggerStrategyBuilder
            .trigger(this._trigger)
            .host(this._host.nativeElement)
            .container(function () { return dynamicOverlay.getContainer(); })
            .build();
        this.triggerStrategy.show$.subscribe(function () { return dynamicOverlay.show(); });
        this.triggerStrategy.hide$.subscribe(function () { return dynamicOverlay.hide(); });
    };
    ElDynamicOverlayHandler.prototype.isContainerRerenderRequired = function () {
        return this.isContentUpdated()
            || this.isContextUpdated()
            || this.isPositionStrategyUpdateRequired();
    };
    ElDynamicOverlayHandler.prototype.isPositionStrategyUpdateRequired = function () {
        return this.isAdjustmentUpdated() || this.isPositionUpdated() || this.isOffsetUpdated() || this.isHostUpdated();
    };
    ElDynamicOverlayHandler.prototype.isTriggerStrategyUpdateRequired = function () {
        return this.isTriggerUpdated() || this.isHostUpdated();
    };
    ElDynamicOverlayHandler.prototype.isComponentTypeUpdateRequired = function () {
        return this.isComponentTypeUpdated();
    };
    ElDynamicOverlayHandler.prototype.isOverlayConfigUpdateRequired = function () {
        return this.isOverlayConfigUpdated();
    };
    ElDynamicOverlayHandler.prototype.isComponentTypeUpdated = function () {
        return this.changes.componentType && this.changes.componentType.isChanged();
    };
    ElDynamicOverlayHandler.prototype.isContentUpdated = function () {
        return this.changes.content && this.changes.content.isChanged();
    };
    ElDynamicOverlayHandler.prototype.isContextUpdated = function () {
        return this.changes.context && this.changes.context.isChanged();
    };
    ElDynamicOverlayHandler.prototype.isAdjustmentUpdated = function () {
        return this.changes.adjustment && this.changes.adjustment.isChanged();
    };
    ElDynamicOverlayHandler.prototype.isPositionUpdated = function () {
        return this.changes.position && this.changes.position.isChanged();
    };
    ElDynamicOverlayHandler.prototype.isHostUpdated = function () {
        return this.changes.host && this.changes.host.isChanged();
    };
    ElDynamicOverlayHandler.prototype.isTriggerUpdated = function () {
        return this.changes.trigger && this.changes.trigger.isChanged();
    };
    ElDynamicOverlayHandler.prototype.isOffsetUpdated = function () {
        return this.changes.offset && this.changes.offset.isChanged();
    };
    ElDynamicOverlayHandler.prototype.isOverlayConfigUpdated = function () {
        return this.changes.overlayConfig && this.changes.overlayConfig.isChanged();
    };
    ElDynamicOverlayHandler.prototype.clearChanges = function () {
        this.changes = {};
    };
    ElDynamicOverlayHandler = __decorate([
        Injectable(),
        __metadata("design:paramtypes", [ElPositionBuilderService,
            ElTriggerStrategyBuilderService,
            ElDynamicOverlay])
    ], ElDynamicOverlayHandler);
    return ElDynamicOverlayHandler;
}());
export { ElDynamicOverlayHandler };
//# sourceMappingURL=dynamic-overlay-handler.js.map