import { action, makeObservable } from 'mobx';
import { EventEmitter } from 'events';
export class ReturnFocus {
    constructor() {
        this.prevFocusedElement = null;
        this.modalElement = null;
        this.events = new EventEmitter();
        makeObservable(this, {
            onClose: action,
            onOpen: action,
        });
    }
    onOpen() {
        this.prevFocusedElement = document.activeElement;
        this.modalElement?.focus({
            preventScroll: true,
        });
    }
    onClose() {
        const toFocus = this.prevFocusedElement;
        if (toFocus instanceof HTMLElement) {
            try {
                toFocus.focus();
            }
            catch (e) {
                console.error(e);
            }
            this.prevFocusedElement = null;
        }
    }
    init() {
        return () => {
            this.events.emit('unmount');
        };
    }
}
//# sourceMappingURL=ReturnFocus.js.map