import BaseComponent from "../BaseComponent";

class Success {
    init() {
        if (!jQuery('.success-ui')[0]) {
            return;
        }
        if (jQuery('.success-ui').data('initializied') !== 'true') {
            new SuccessHandler(jQuery('.success-ui'));
        }
    }
}

class SuccessHandler extends BaseComponent {

    constructor(component_base_ui) {
        super(component_base_ui);

        this.handleRouteChange();

        this.handlePopupClickOutside({
            newHash: '',
        });
    }

    onRouteChange(payload) {
        if (payload?.key === 'success') {
            this._showPopup(payload);
        } else {
            this._closePopup();
        }
    }

    _showPopup(payload) {
        document.title = payload?.data?.page_title ?? '';
        this.checkRecaptcha();
        this.show();
        this.fadeIn();
    }

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

export default Success;