import BaseComponent from "../BaseComponent";
import EventBus, { Event, Events } from "../../Events/EventBus";

class FeError {
    init() {
        if (!jQuery('.fe-error-ui')[0]) {
            return;
        }
        if (jQuery('.fe-error-ui').data('initializied') !== 'true') {
            new FeErrorHandler(jQuery('.fe-error-ui'));
        }
    }
}

class FeErrorHandler extends BaseComponent {

    constructor(component_base_ui) {
        super(component_base_ui);

        this._handleEvents();
    }

    _handleEvents() {
        const _this = this;
        const errorPopupEv = new Event(
            Events.SHOW_FE_ERROR,
            (payload) => {
                this._component_base_ui.find('.fe-error-content').html(payload?.html);
                this._showPopup();
            }
        );
        EventBus.getInstance().registerEvent(errorPopupEv);

        this._component_base_ui.find('.close-btn').on('click', function(event) {
            event.preventDefault();
            _this._closePopup();
        })

        this._component_base_ui.find('.pupup-background-ui').on('click', function(event) {
            event.preventDefault();
            _this._closePopup();
        })
    }

    _showPopup(payload) {
        this.show();
        this.fadeIn();
    }

    _closePopup() {
        this.hide();
        this.fadeOut();
    }
}

export default FeError;