/**
 * Voice Logica AI for WooCommerce – Admin settings page.
 *
 * Toggles visibility of product code type and order emails rows based on checkboxes.
 *
 * @package VoiceLogica_AI_WooCommerce
 */

(function () {
	'use strict';

	var createOrderCheckbox      = document.getElementById( 'voicelogica-create-order-checkbox' );
	var getProductByCodeCheckbox = document.getElementById( 'voicelogica-get-product-by-code-checkbox' );
	var codeTypeSelect           = document.getElementById( 'voicelogica-code-type' );
	var orderEmailsRow           = document.getElementById( 'voicelogica-order-emails-row' );
	var codeTypeRow              = document.getElementById( 'voicelogica-code-type-row' );

	function toggleOrderEmailsRow() {
		if (createOrderCheckbox && orderEmailsRow) {
			orderEmailsRow.style.display = createOrderCheckbox.checked ? 'table-row' : 'none';
		}
	}

	function toggleCodeTypeRow() {
		if ( ! getProductByCodeCheckbox || ! codeTypeRow) {
			return;
		}
		var isChecked             = getProductByCodeCheckbox.checked;
		codeTypeRow.style.display = isChecked ? 'table-row' : 'none';
		if (codeTypeSelect) {
			codeTypeSelect.required = isChecked;
			if (isChecked && ! codeTypeSelect.value) {
				codeTypeSelect.value = 'sku';
			}
		}
	}

	if (createOrderCheckbox) {
		createOrderCheckbox.addEventListener( 'change', toggleOrderEmailsRow );
		toggleOrderEmailsRow(); // Initial state.
	}

	if (getProductByCodeCheckbox) {
		getProductByCodeCheckbox.addEventListener( 'change', toggleCodeTypeRow );
		toggleCodeTypeRow(); // Initial state.
	}
})();
