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); }
};
var _a;
import { Inject, Injectable } from '@angular/core';
import { map } from 'rxjs/operators';
import { EL_DOCUMENT } from '../../../theme.options';
import { ElFlexibleConnectedPositionStrategy, ElOverlayPositionBuilder, ElPlatform, } from './mapping';
import { ElOverlayContainerAdapter } from '../adapter/overlay-container-adapter';
import { ElViewportRulerAdapter } from '../adapter/viewport-ruler-adapter';
import { ElGlobalLogicalPosition } from './position-helper';
import { GlobalPositionStrategy } from '@angular/cdk/overlay';
export var ElAdjustment;
(function (ElAdjustment) {
    ElAdjustment["NOOP"] = "noop";
    ElAdjustment["CLOCKWISE"] = "clockwise";
    ElAdjustment["COUNTERCLOCKWISE"] = "counterclockwise";
    ElAdjustment["VERTICAL"] = "vertical";
    ElAdjustment["HORIZONTAL"] = "horizontal";
})(ElAdjustment || (ElAdjustment = {}));
export var ElPosition;
(function (ElPosition) {
    ElPosition["TOP"] = "top";
    ElPosition["BOTTOM"] = "bottom";
    ElPosition["LEFT"] = "left";
    ElPosition["RIGHT"] = "right";
    ElPosition["START"] = "start";
    ElPosition["END"] = "end";
    ElPosition["TOP_END"] = "top-end";
    ElPosition["TOP_START"] = "top-start";
    ElPosition["BOTTOM_END"] = "bottom-end";
    ElPosition["BOTTOM_START"] = "bottom-start";
    ElPosition["END_TOP"] = "end-top";
    ElPosition["END_BOTTOM"] = "end-bottom";
    ElPosition["START_TOP"] = "start-top";
    ElPosition["START_BOTTOM"] = "start-bottom";
})(ElPosition || (ElPosition = {}));
var POSITIONS = (_a = {},
    _a[ElPosition.RIGHT] = function (offset) {
        return { originX: 'end', originY: 'center', overlayX: 'start', overlayY: 'center', offsetX: offset };
    },
    _a[ElPosition.BOTTOM] = function (offset) {
        return { originX: 'center', originY: 'bottom', overlayX: 'center', overlayY: 'top', offsetY: offset };
    },
    _a[ElPosition.LEFT] = function (offset) {
        return { originX: 'start', originY: 'center', overlayX: 'end', overlayY: 'center', offsetX: -offset };
    },
    _a[ElPosition.TOP] = function (offset) {
        return { originX: 'center', originY: 'top', overlayX: 'center', overlayY: 'bottom', offsetY: -offset };
    },
    _a[ElPosition.START] = function (offset) {
        return this[ElPosition.LEFT](offset);
    },
    _a[ElPosition.END] = function (offset) {
        return this[ElPosition.RIGHT](offset);
    },
    _a[ElPosition.END_TOP] = function (offset) {
        return { originX: 'end', originY: 'bottom', overlayX: 'start', overlayY: 'bottom', offsetX: offset };
    },
    _a[ElPosition.END_BOTTOM] = function (offset) {
        return { originX: 'end', originY: 'top', overlayX: 'start', overlayY: 'top', offsetX: offset };
    },
    _a[ElPosition.BOTTOM_START] = function (offset) {
        return { originX: 'end', originY: 'bottom', overlayX: 'end', overlayY: 'top', offsetY: offset };
    },
    _a[ElPosition.BOTTOM_END] = function (offset) {
        return { originX: 'start', originY: 'bottom', overlayX: 'start', overlayY: 'top', offsetY: offset };
    },
    _a[ElPosition.START_TOP] = function (offset) {
        return { originX: 'start', originY: 'bottom', overlayX: 'end', overlayY: 'bottom', offsetX: -offset };
    },
    _a[ElPosition.START_BOTTOM] = function (offset) {
        return { originX: 'start', originY: 'top', overlayX: 'end', overlayY: 'top', offsetX: -offset };
    },
    _a[ElPosition.TOP_START] = function (offset) {
        return { originX: 'end', originY: 'top', overlayX: 'end', overlayY: 'bottom', offsetY: -offset };
    },
    _a[ElPosition.TOP_END] = function (offset) {
        return { originX: 'start', originY: 'top', overlayX: 'start', overlayY: 'bottom', offsetY: -offset };
    },
    _a);
var COUNTER_CLOCKWISE_POSITIONS = [
    ElPosition.TOP,
    ElPosition.TOP_END,
    ElPosition.TOP_START,
    ElPosition.START,
    ElPosition.START_TOP,
    ElPosition.START_BOTTOM,
    ElPosition.BOTTOM,
    ElPosition.BOTTOM_START,
    ElPosition.BOTTOM_END,
    ElPosition.END,
    ElPosition.END_BOTTOM,
    ElPosition.END_TOP,
];
var CLOCKWISE_POSITIONS = [
    ElPosition.TOP,
    ElPosition.TOP_START,
    ElPosition.TOP_END,
    ElPosition.END,
    ElPosition.END_TOP,
    ElPosition.END_BOTTOM,
    ElPosition.BOTTOM,
    ElPosition.BOTTOM_END,
    ElPosition.BOTTOM_START,
    ElPosition.START,
    ElPosition.START_BOTTOM,
    ElPosition.START_TOP,
];
var VERTICAL_POSITIONS = [ElPosition.BOTTOM, ElPosition.TOP];
var HORIZONTAL_POSITIONS = [ElPosition.START, ElPosition.END];
function comparePositions(p1, p2) {
    return p1.originX === p2.originX
        && p1.originY === p2.originY
        && p1.overlayX === p2.overlayX
        && p1.overlayY === p2.overlayY;
}
/**
 * The main idea of the adjustable connected strategy is to provide predefined set of positions for your overlay.
 * You have to provide adjustment and appropriate strategy will be chosen in runtime.
 * */
var ElAdjustableConnectedPositionStrategy = /** @class */ (function (_super) {
    __extends(ElAdjustableConnectedPositionStrategy, _super);
    function ElAdjustableConnectedPositionStrategy() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this._offset = 15;
        _this.positionChange = _this.positionChanges.pipe(map(function (positionChange) { return positionChange.connectionPair; }), map(function (connectionPair) {
            return _this.appliedPositions.find(function (_a) {
                var connectedPosition = _a.connectedPosition;
                return comparePositions(connectedPosition, connectionPair);
            }).key;
        }));
        return _this;
    }
    ElAdjustableConnectedPositionStrategy.prototype.attach = function (overlayRef) {
        /**
         * We have to apply positions before attach because super.attach() validates positions and crashes app
         * if no positions provided.
         * */
        this.applyPositions();
        _super.prototype.attach.call(this, overlayRef);
    };
    ElAdjustableConnectedPositionStrategy.prototype.apply = function () {
        this.applyPositions();
        _super.prototype.apply.call(this);
    };
    ElAdjustableConnectedPositionStrategy.prototype.position = function (position) {
        this._position = position;
        return this;
    };
    ElAdjustableConnectedPositionStrategy.prototype.adjustment = function (adjustment) {
        this._adjustment = adjustment;
        return this;
    };
    ElAdjustableConnectedPositionStrategy.prototype.offset = function (offset) {
        this._offset = offset;
        return this;
    };
    ElAdjustableConnectedPositionStrategy.prototype.applyPositions = function () {
        var positions = this.createPositions();
        this.persistChosenPositions(positions);
        this.withPositions(this.appliedPositions.map(function (_a) {
            var connectedPosition = _a.connectedPosition;
            return connectedPosition;
        }));
    };
    ElAdjustableConnectedPositionStrategy.prototype.createPositions = function () {
        switch (this._adjustment) {
            case ElAdjustment.NOOP:
                return [this._position];
            case ElAdjustment.CLOCKWISE:
                return this.reorderPreferredPositions(CLOCKWISE_POSITIONS);
            case ElAdjustment.COUNTERCLOCKWISE:
                return this.reorderPreferredPositions(COUNTER_CLOCKWISE_POSITIONS);
            case ElAdjustment.HORIZONTAL:
                return this.reorderPreferredPositions(HORIZONTAL_POSITIONS);
            case ElAdjustment.VERTICAL:
                return this.reorderPreferredPositions(VERTICAL_POSITIONS);
        }
    };
    ElAdjustableConnectedPositionStrategy.prototype.persistChosenPositions = function (positions) {
        var _this = this;
        this.appliedPositions = positions.map(function (position) { return ({
            key: position,
            connectedPosition: POSITIONS[position](_this._offset),
        }); });
    };
    ElAdjustableConnectedPositionStrategy.prototype.reorderPreferredPositions = function (positions) {
        var startPositionIndex = positions.indexOf(this._position);
        var firstPart = positions.slice(startPositionIndex);
        var secondPart = positions.slice(0, startPositionIndex);
        return firstPart.concat(secondPart);
    };
    return ElAdjustableConnectedPositionStrategy;
}(ElFlexibleConnectedPositionStrategy));
export { ElAdjustableConnectedPositionStrategy };
var ElGlobalPositionStrategy = /** @class */ (function (_super) {
    __extends(ElGlobalPositionStrategy, _super);
    function ElGlobalPositionStrategy() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    ElGlobalPositionStrategy.prototype.position = function (position) {
        switch (position) {
            case ElGlobalLogicalPosition.TOP_START:
                return this.top().left();
            case ElGlobalLogicalPosition.TOP_END:
                return this.top().right();
            case ElGlobalLogicalPosition.BOTTOM_START:
                return this.bottom().left();
            case ElGlobalLogicalPosition.BOTTOM_END:
                return this.bottom().right();
        }
    };
    return ElGlobalPositionStrategy;
}(GlobalPositionStrategy));
export { ElGlobalPositionStrategy };
var ElPositionBuilderService = /** @class */ (function () {
    function ElPositionBuilderService(document, viewportRuler, platform, positionBuilder, overlayContainer) {
        this.document = document;
        this.viewportRuler = viewportRuler;
        this.platform = platform;
        this.positionBuilder = positionBuilder;
        this.overlayContainer = overlayContainer;
    }
    ElPositionBuilderService.prototype.global = function () {
        return new ElGlobalPositionStrategy();
    };
    ElPositionBuilderService.prototype.connectedTo = function (elementRef) {
        return new ElAdjustableConnectedPositionStrategy(elementRef, this.viewportRuler, this.document, this.platform, this.overlayContainer)
            .withFlexibleDimensions(false)
            .withPush(false);
    };
    ElPositionBuilderService = __decorate([
        Injectable(),
        __param(0, Inject(EL_DOCUMENT)),
        __metadata("design:paramtypes", [Object, ElViewportRulerAdapter,
            ElPlatform,
            ElOverlayPositionBuilder,
            ElOverlayContainerAdapter])
    ], ElPositionBuilderService);
    return ElPositionBuilderService;
}());
export { ElPositionBuilderService };
//# sourceMappingURL=overlay-position.js.map