import { Alert, AlertInfo } from '../../Components/Alert';
import Main                 from '../../Main';
import TabService           from '../../Services/TabService';
import Compatibility        from '../Compatibility';

declare let mrwpPluginSettings: any;
declare function mrwp_prepare_shipping() : boolean;
declare function mrwpParcelPickerInit() : void;
declare function mrwpShippingCode( shippingIds: string, selectedShipping: string ) : string;
declare function mrwpNeedsParcelPicker( option: boolean ) : boolean;

class MondialRelay extends Compatibility {
    constructor() {
        super( 'MondialRelay' );
    }

    load(): void {
        jQuery( '#cfw-shipping-action' ).hide();

        jQuery( document.body ).on( 'updated_checkout', () => {
            jQuery( '#cfw-shipping-action' ).show();
        } );

        const easyTabsWrap: any = Main.instance.tabService.tabContainer;

        easyTabsWrap.bind( 'easytabs:before', ( event, clicked, target ) => {
            if ( jQuery( target ).attr( 'id' ) === TabService.paymentMethodTabId ) {
                if ( jQuery( '#mrwp_parcel_shop_mandatory' ).val() === 'Yes' ) {
                    if ( jQuery( '#mrwp_parcel_shop_id' ).val() === '' ) {
                        // Prevent removing alert on next update checkout
                        Main.instance.preserveAlerts = true;

                        const alertInfo: AlertInfo = {
                            type: 'error',
                            message: 'Vous n\'avez pas encore choisi de Point Relais.',
                            cssClass: 'cfw-alert-error',
                        };

                        const alert: Alert = new Alert( Main.instance.alertContainer, alertInfo );
                        alert.addAlert( true );

                        event.stopImmediatePropagation();

                        return false;
                    }
                }
            }
        } );
    }
}

export default MondialRelay;
