(function ($) {

	$( document ).ready(
		function () {
			console.log( 'skip-valuation-js' );

			let toggle_text = alsendo_connect_skip_valuation.toggle_text;

			// Collapse all sections on page load.
			$( '#apaczka_panel_sender' ).css( 'min-height', 'unset' );
			$( '#apaczka_panel_sender .options_group' ).hide();
			$( '#apaczka_panel_receiver' ).css( 'min-height', 'unset' );
			$( '#apaczka_panel_receiver .options_group' ).hide();
			$( '#apaczka_panel_properties' ).css( 'min-height', 'unset' );
			$( '#apaczka_panel_properties .options_group' ).hide();

			// Add toggle links after each h4.
			$( '#apaczka_panel_sender h4' ).after( '<a href="#" class="apaczka-toggle-link" data-target="apaczka_panel_sender" style="display: block; margin: 10px 0; color: #2271b1; text-decoration: none;">▼ ' + toggle_text + '</a>' );
			$( '#apaczka_panel_receiver h4' ).after( '<a href="#" class="apaczka-toggle-link" data-target="apaczka_panel_receiver" style="display: block; margin: 10px 0; color: #2271b1; text-decoration: none;">▼ ' + toggle_text + '</a>' );
			$( '#apaczka_panel_properties h4' ).after( '<a href="#" class="apaczka-toggle-link" data-target="apaczka_panel_properties" style="display: block; margin: 10px 0; color: #2271b1; text-decoration: none;">▼ ' + toggle_text + '</a>' );

			// Toggle functionality.
			$( '.apaczka-toggle-link' ).on(
				'click',
				function (e) {
					e.preventDefault();

					var targetId      = $( this ).data( 'target' );
					var $optionsGroup = $( '#' + targetId + ' .options_group' );

					// Toggle the options group.
					$optionsGroup.slideToggle( 300 );

					// Update arrow icon.
					if ($optionsGroup.is( ':visible' )) {
						$( this ).html( '▲ ' + toggle_text + '' );
					} else {
						$( this ).html( '▼ ' + toggle_text + '' );
					}
				}
			);

			let valuate_btn = $( '#apaczka_calculate_price_btn' );
			if (typeof valuate_btn != 'undefined' && valuate_btn !== null) {
				console.log( 'valuation-trigger' );
				setTimeout(
					function () {
						$( '#apaczka_calculate_price_btn' ).click();
					},
					250
				);
			}
		}
	);

})( jQuery );