import * as i0 from '@angular/core';
import { Injectable } from '@angular/core';
import { Subject, fromEvent, merge } from 'rxjs';
import { mergeAll, takeUntil, repeatWhen, publishReplay, filter, pluck, map, finalize, shareReplay } from 'rxjs/operators';
import { isUndefined } from '@ironsource/fusion-ui/utils';

class EventsHandlerService {
    constructor() {
        this.observables$ = new Subject();
        this.takeUntilAll$ = new Subject();
        this.repeatWhen$ = new Subject();
        this.stream$ = this.observables$.asObservable().pipe(mergeAll(), takeUntil(this.takeUntilAll$), repeatWhen(() => this.repeatWhen$), publishReplay(1));
        this.registeredEvents = {};
        this.stream$.connect();
    }
    ngOnDestroy() {
        this.takeUntilAll$.next();
        this.takeUntilAll$.complete();
    }
    registerStream({ eventName, stream$ }) {
        if (this.registeredEvents[eventName]) {
            console.warn(`Stream ${eventName} already registerd`);
            return;
        }
        const wrapStream$ = this.wrapStream({ stream$, eventName });
        this.observables$.next(wrapStream$);
        this.registeredEvents[eventName] = true;
    }
    getStream({ eventName }) {
        return this.stream$.pipe(filter(({ eventName: event }) => event === eventName), pluck('value'));
    }
    clearAllStreams() {
        this.takeUntilAll$.next();
        this.registeredEvents = {};
        this.repeatWhen$.next();
    }
    wrapStream({ eventName, stream$ }) {
        return stream$.pipe(map(value => ({ value, eventName })), finalize(() => delete this.registeredEvents[eventName]));
    }
    static { this.ɵfac = function EventsHandlerService_Factory(t) { return new (t || EventsHandlerService)(); }; }
    static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: EventsHandlerService, factory: EventsHandlerService.ɵfac }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(EventsHandlerService, [{
        type: Injectable
    }], () => [], null); })();

class SharedEventsService {
    constructor() {
        this.onDestroy$ = new Subject();
        this.mouseOver$ = merge(fromEvent(document.body, 'mouseover'), fromEvent(document.body, 'click')).pipe(shareReplay(1), takeUntil(this.onDestroy$));
    }
    ngOnDestroy() {
        this.onDestroy$.next();
        this.onDestroy$.complete();
    }
    getMouseoverObservable({ element, padding }) {
        return this.mouseOver$.pipe(filter((event) => {
            // if no event coordinate like event.clientX - this testim.io
            return isUndefined(event.clientX) || this.isInRegion({ event, element, padding });
        }));
    }
    isInRegion({ event, element, padding }) {
        const { left, right, top, bottom } = element.getBoundingClientRect();
        const isInRect = event.clientX >= left - padding &&
            event.clientX <= right + padding &&
            event.clientY >= top - padding &&
            event.clientY <= bottom + padding;
        return isInRect;
    }
    static { this.ɵfac = function SharedEventsService_Factory(t) { return new (t || SharedEventsService)(); }; }
    static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: SharedEventsService, factory: SharedEventsService.ɵfac, providedIn: 'root' }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(SharedEventsService, [{
        type: Injectable,
        args: [{
                providedIn: 'root'
            }]
    }], null, null); })();

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

export { EventsHandlerService, SharedEventsService };
//# sourceMappingURL=ironsource-fusion-ui-services-events-handler.mjs.map
