import { action, makeObservable } from 'mobx';
import { EventEmitter } from 'events';
export class ScrollAwayScheduleStrategy {
    constructor(state) {
        this.events = new EventEmitter();
        this.state = state;
        makeObservable(this, {
            preventEnter: action.bound,
            preventExit: action.bound,
            onIntersectionRatioChanged: action.bound,
        });
    }
    _scheduleEnter() {
        const { topNotification } = this.state;
        const { nextData } = topNotification;
        this.events.removeAllListeners('enter');
        this.events.once('enter', action(() => {
            if (nextData === topNotification.nextData) {
                topNotification.set(nextData);
            }
        }));
    }
    preventEnter() {
        const { topNotification, intersection, consideredVisibleRatio } = this.state;
        if (intersection.intersectionRatio < consideredVisibleRatio) {
            topNotification.show = false;
            // console.log('enter prevented');
            this._scheduleEnter();
        }
    }
    preventExit() {
        const { topNotification, intersection, consideredVisibleRatio } = this.state;
        if (intersection.intersectionRatio < consideredVisibleRatio) {
            topNotification.show = true;
            // console.log('exit prevented');
            this._scheduleEnter();
        }
    }
    onIntersectionRatioChanged(ratio) {
        const { consideredVisibleRatio, topNotification } = this.state;
        // when scrolling in trigger enter
        if (ratio >= consideredVisibleRatio) {
            this.events.emit('enter');
            topNotification.events.off('beforeExit', this.preventExit);
            topNotification.events.off('enter', this.preventEnter);
        }
        // when scrolling out schedule prevention
        else {
            topNotification.events.off('beforeExit', this.preventExit);
            topNotification.events.off('enter', this.preventEnter);
            if (topNotification.show) {
                topNotification.events.on('beforeExit', this.preventExit);
            }
            else {
                topNotification.events.on('enter', this.preventEnter);
            }
        }
    }
    init() { }
}
//# sourceMappingURL=ScrollAwayScheduleStrategy.js.map