/**
 * JS for Apple Pay Settings.
 *
 * @package flo-checkout
 */

/* global jQuery, floApplePay */
let NMC_Apple_Pay_Settings_Instance;
jQuery( document ).ready(
	function () {
		NMC_Apple_Pay_Settings_Instance = new NMC_Apple_Pay_Settings();
	}
);

class NMC_Apple_Pay_Settings {
	constructor() {
		// Bindings.
		this.toggle_api_settings = this.toggle_api_settings.bind( this );

		this.payment_method = nm_apple_pay_settings.payment_method_id;
		this.modes          = nm_apple_pay_settings.modes;

		this.$mode_select          = jQuery( '#woocommerce_' + this.payment_method + '_mode' );
		this.$prod_merchant_id     = jQuery( '#woocommerce_' + this.payment_method + '_merchant_id' );
		this.$prod_sub_merchant_id = jQuery( '#woocommerce_' + this.payment_method + '_sub_merchant_id' );
		this.$prod_secret          = jQuery( '#woocommerce_' + this.payment_method + '_secret' );
		this.$int_merchant_id      = jQuery( '#woocommerce_' + this.payment_method + '_integration_merchant_id' );
		this.$int_sub_merchant_id  = jQuery( '#woocommerce_' + this.payment_method + '_integration_sub_merchant_id' );
		this.$int_secret           = jQuery( '#woocommerce_' + this.payment_method + '_integration_secret' );

		this.$mode_select.on( 'change', this.toggle_api_settings );

		this.toggle_api_settings();
	}

	toggle_api_settings() {
		this.hide_settings_row( this.$prod_merchant_id );
		this.hide_settings_row( this.$prod_sub_merchant_id );
		this.hide_settings_row( this.$prod_secret );
		this.hide_settings_row( this.$int_merchant_id );
		this.hide_settings_row( this.$int_sub_merchant_id );
		this.hide_settings_row( this.$int_secret );

		switch ( this.$mode_select.val() ) {
			case this.modes.prod:
				this.show_settings_row( this.$prod_merchant_id );
				this.show_settings_row( this.$prod_sub_merchant_id );
				this.show_settings_row( this.$prod_secret );
				break;
			case this.modes.int:
				this.show_settings_row( this.$int_merchant_id );
				this.show_settings_row( this.$int_sub_merchant_id );
				this.show_settings_row( this.$int_secret );
				break;
		}
	}

	hide_settings_row( $settings_element ) {
		$settings_element.closest( 'tr' ).hide();
	}

	show_settings_row( $settings_element ) {
		$settings_element.closest( 'tr' ).show();
	}
}
