/**
 * Field Persistence Service
 */
import DataService    from './DataService';
import LoggingService from './LoggingService';

const debounce = require( 'debounce' );

class FieldPersistenceService {
    private readonly _debouncedLoaded;

    constructor( form: any ) {
        this._debouncedLoaded = debounce( FieldPersistenceService.loaded, 200 );

        form.garlic( {
            events: [ 'textInput', 'input', 'change', 'click', 'keypress', 'paste', 'focus', 'cfw_store' ],
            destroy: false,
            excluded: DataService.getSetting( 'field_persistence_excludes' ).join( ', ' ),
            onRetrieve: this.onRetrieve.bind( this ),
        } );

        this.setListeners();
    }

    setListeners() {
        jQuery( document.body ).on( 'cfw-order-complete-before-redirect', ( event, form ) => {
            this.destroyForm( form );
        } );

        // After Parsley Service resets field
        jQuery( document.body ).on( 'cfw-after-field-country-to-state-changed', ( e ) => {
            jQuery( e.target ).garlic();
        } );
    }

    destroyForm( form: any ) {
        jQuery( form ).find( ':input' ).each( ( index, element ) => {
            jQuery( element ).garlic( 'destroy' );
        } );
    }

    onRetrieve( element, retrievedValue ) {
        jQuery( document.body ).trigger( 'cfw-field-persistence-after-retrieve-value', [ element, retrievedValue ] );
        LoggingService.logEvent( 'Fired cfw-field-persistence-after-retrieve-value event.' );

        ( this._debouncedLoaded )();
    }

    static loaded() {
        jQuery( document.body ).trigger( 'cfw-field-persistence-loaded' );
        LoggingService.logEvent( 'Fired cfw-field-persistence-loaded event.' );
    }
}

export default FieldPersistenceService;
