import * as i0 from '@angular/core';
import { Directive, Input } from '@angular/core';
import { TooltipPosition } from '@ironsource/fusion-ui/components/tooltip/common/base';
import { isNullOrUndefined } from '@ironsource/fusion-ui/utils';

/**
 * Used @floating-ui/dom (flip()) For flip position of dropped element like drop-menu
 */
class RepositionDirective {
    constructor(elRef) {
        this.elRef = elRef;
        this.fusionRepositionPlacement = TooltipPosition.Bottom;
        this.hostElement = this.elRef.nativeElement;
        this.clientHeight = document.body.clientHeight;
    }
    ngOnInit() {
        this.referenceElement = document.querySelector(this.referenceElementSelector);
    }
    ngAfterViewInit() {
        this.calcForReposition();
    }
    calcForReposition() {
        if (!!this.referenceElement) {
            let shiftPosition;
            const rectReference = this.referenceElement.getBoundingClientRect();
            const rectHost = this.hostElement.getBoundingClientRect();
            shiftPosition = this.calcPosition(this.fusionRepositionPlacement, rectHost, rectReference);
            shiftPosition = this.applyOffset(shiftPosition);
            shiftPosition = this.checkForFlip(this.fusionRepositionPlacement, shiftPosition, rectHost, rectReference);
            Object.assign(this.hostElement.style, {
                left: `${shiftPosition.left}px`,
                top: `${shiftPosition.top}px`
            });
        }
    }
    calcPosition(pos, hostRect, refRect) {
        const commonX = refRect.x + refRect.width / 2 - hostRect.width / 2;
        const commonY = refRect.y + refRect.height / 2 - hostRect.height / 2;
        let position = { top: refRect.y, left: refRect.x };
        switch (pos) {
            case TooltipPosition.Top:
                position = { left: commonX, top: refRect.y - hostRect.height };
                break;
            case TooltipPosition.TopRight:
                position = { left: refRect.x - hostRect.width + refRect.width, top: refRect.y - hostRect.height };
                break;
            case TooltipPosition.TopLeft:
                position = { left: refRect.x, top: refRect.y - hostRect.height };
                break;
            case TooltipPosition.Bottom:
                position = { left: commonX, top: refRect.y + refRect.height };
                break;
            case TooltipPosition.BottomRight:
                position = { left: refRect.x - hostRect.width + refRect.width, top: refRect.y + refRect.height };
                break;
            case TooltipPosition.BottomLeft:
                position = { left: refRect.x, top: refRect.y + refRect.height - hostRect.height };
                break;
            case TooltipPosition.Right:
                position = { left: refRect.x + refRect.width, top: commonY };
                break;
            case TooltipPosition.Left:
                position = { left: refRect.x - hostRect.width, top: commonY };
                break;
        }
        return position;
    }
    applyOffset(position, afterFlip = false) {
        if (!isNullOrUndefined(this.fusionRepositionOffset?.y)) {
            if (afterFlip) {
                position.top = position.top + this.fusionRepositionOffset.y * -1;
            }
            else {
                position.top = position.top + this.fusionRepositionOffset.y;
            }
        }
        if (!isNullOrUndefined(this.fusionRepositionOffset?.x)) {
            if (afterFlip) {
                position.left = position.left + (this.fusionRepositionOffset.x - 1);
            }
            else {
                position.left = position.left + this.fusionRepositionOffset.x;
            }
        }
        return position;
    }
    checkForFlip(pos, position, hostRect, rectReference) {
        switch (pos) {
            case TooltipPosition.Bottom:
            case TooltipPosition.BottomLeft:
            case TooltipPosition.BottomRight:
                if (this.clientHeight - (position.top + hostRect.height) < 0) {
                    pos =
                        pos === TooltipPosition.Bottom
                            ? TooltipPosition.Top
                            : pos === TooltipPosition.BottomLeft
                                ? TooltipPosition.TopLeft
                                : TooltipPosition.TopRight;
                    position = this.applyOffset(this.calcPosition(pos, hostRect, rectReference), true);
                }
                break;
        }
        return position;
    }
    static { this.ɵfac = function RepositionDirective_Factory(t) { return new (t || RepositionDirective)(i0.ɵɵdirectiveInject(i0.ElementRef)); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: RepositionDirective, selectors: [["", "fusionReposition", ""]], inputs: { referenceElementSelector: [i0.ɵɵInputFlags.None, "fusionReposition", "referenceElementSelector"], fusionRepositionOffset: "fusionRepositionOffset", fusionRepositionPlacement: "fusionRepositionPlacement" }, standalone: true }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(RepositionDirective, [{
        type: Directive,
        args: [{
                selector: '[fusionReposition]',
                standalone: true
            }]
    }], () => [{ type: i0.ElementRef }], { referenceElementSelector: [{
            type: Input,
            args: ['fusionReposition']
        }], fusionRepositionOffset: [{
            type: Input
        }], fusionRepositionPlacement: [{
            type: Input
        }] }); })();

/**
 * Generated bundle index. Do not edit.
 */

export { RepositionDirective };
//# sourceMappingURL=ironsource-fusion-ui-directives-reposition.mjs.map
