import * as i0 from '@angular/core';
import { EventEmitter, Directive, Input, Output, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import * as i1 from '@ironsource/fusion-ui/services/window';

class IntersectionDirective {
    set intersectionOptions(value) {
        this.options = value || {};
        this.disconnect();
        this.observe();
    }
    constructor(el, windowService) {
        this.el = el;
        this.windowService = windowService;
        this.fusionIntersection = new EventEmitter();
        this.isAfterViewInit = false;
        this.observerCallback = ([entry]) => {
            this.fusionIntersection.emit(entry);
        };
    }
    ngOnInit() {
        this.isSupported = typeof this.windowService.nativeWindow.IntersectionObserver === 'function';
    }
    ngOnDestroy() {
        this.disconnect();
    }
    ngAfterViewInit() {
        this.isAfterViewInit = true;
        this.observe();
    }
    observe() {
        if (this.isSupported && this.isAfterViewInit) {
            const options = this.createObserverOptions();
            this.observer = new IntersectionObserver(this.observerCallback, options);
            this.observer.observe(this.el.nativeElement);
        }
    }
    disconnect() {
        if (this.isSupported && this.isAfterViewInit && this.observer) {
            this.observer.disconnect();
            this.observer = null;
        }
    }
    createObserverOptions() {
        const options = {
            rootMargin: this.createRootMarginValue()
        };
        const root = this.getRoot();
        if (root) {
            options.root = root;
        }
        if (this.options && this.options.threshold) {
            options.threshold = this.options.threshold;
        }
        return options;
    }
    createRootMarginValue() {
        if (!this.options || !this.options.rootMargin) {
            return '';
        }
        return [this.options.rootMargin.top, this.options.rootMargin.left, this.options.rootMargin.bottom, this.options.rootMargin.right]
            .map(value => {
            if (typeof value === 'number') {
                return value + 'px';
            }
            return value || '0px';
        })
            .join(' ');
    }
    getRoot() {
        if (!this.options) {
            return null;
        }
        return typeof this.options.root === 'string'
            ? this.windowService.nativeWindow.document.querySelector(this.options.root)
            : this.options.root instanceof HTMLElement
                ? this.options.root
                : null;
    }
    static { this.ɵfac = function IntersectionDirective_Factory(t) { return new (t || IntersectionDirective)(i0.ɵɵdirectiveInject(i0.ElementRef), i0.ɵɵdirectiveInject(i1.WindowService)); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: IntersectionDirective, selectors: [["", "fusionIntersection", ""]], inputs: { intersectionOptions: "intersectionOptions" }, outputs: { fusionIntersection: "fusionIntersection" } }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(IntersectionDirective, [{
        type: Directive,
        args: [{
                selector: '[fusionIntersection]'
            }]
    }], () => [{ type: i0.ElementRef }, { type: i1.WindowService }], { intersectionOptions: [{
            type: Input
        }], fusionIntersection: [{
            type: Output
        }] }); })();

class IntersectionModule {
    static { this.ɵfac = function IntersectionModule_Factory(t) { return new (t || IntersectionModule)(); }; }
    static { this.ɵmod = /*@__PURE__*/ i0.ɵɵdefineNgModule({ type: IntersectionModule }); }
    static { this.ɵinj = /*@__PURE__*/ i0.ɵɵdefineInjector({ imports: [CommonModule] }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(IntersectionModule, [{
        type: NgModule,
        args: [{
                declarations: [IntersectionDirective],
                exports: [IntersectionDirective],
                imports: [CommonModule]
            }]
    }], null, null); })();
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(IntersectionModule, { declarations: [IntersectionDirective], imports: [CommonModule], exports: [IntersectionDirective] }); })();

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

export { IntersectionDirective, IntersectionModule };
//# sourceMappingURL=ironsource-fusion-ui-directives-intersection.mjs.map
