// phpcs:ignoreFile
/**
 * PaddleKonnect Admin JS
 * Enhances the WooCommerce product edit screen.
 */
jQuery(
	function ($) {
		// Only run if we are in the product data panel
		if ($( '.paddlekonnect-product-fields' ).length === 0) {
			return;
		}

		var $priceIdInput       = $( '#_paddlekonnect_price_id' );
		var $vaultCheckboxField = $( '.show_if_paddlekonnect_vault' ).closest( '.form-field' ); // In case we want to wrap it later, currently using direct targeting
		var $vaultCheckbox      = $( '#_paddlekonnect_grants_vault' ).closest( '.form-field' );

		function toggleVaultVisibility() {
			if ($priceIdInput.val().trim() !== '') {
				$vaultCheckbox.slideDown();
			} else {
				$vaultCheckbox.slideUp();
				$( '#_paddlekonnect_grants_vault' ).prop( 'checked', false ); // uncheck if hidden
			}
		}

		// Initial check
		toggleVaultVisibility();

		// Bind to input changes
		$priceIdInput.on(
			'input change',
			function () {
				toggleVaultVisibility();
			}
		);
	}
);
