/**
 * Support form — open WhatsApp with pre-filled technical issue message.
 */
( function () {
	var cfg = window.shc4wcSupport || {};
	var number = cfg.whatsappNumber || '';

	function val( id ) {
		var el = document.getElementById( id );
		return el ? String( el.value || '' ).trim() : '';
	}

	function issueLabel() {
		var select = document.getElementById( 'shc4wc_support_issue' );
		if ( ! select || ! select.options.length ) {
			return '';
		}
		return select.options[ select.selectedIndex ].text;
	}

	function buildWhatsAppText() {
		var name = val( 'shc4wc_support_name' );
		var email = val( 'shc4wc_support_email' );
		var subject = val( 'shc4wc_support_subject' ) || issueLabel();
		var message = val( 'shc4wc_support_message' );

		var lines = [
			'Payments Square Connect — support request',
			'',
			'Name: ' + name,
			'Email: ' + email,
			'Issue type: ' + issueLabel(),
			'Subject: ' + subject,
			'',
			message,
		];

		return lines.join( '\n' );
	}

	document.addEventListener( 'DOMContentLoaded', function () {
		var btn = document.getElementById( 'shc4wc_support_whatsapp' );
		if ( ! btn || ! number ) {
			return;
		}

		btn.addEventListener( 'click', function () {
			if ( ! val( 'shc4wc_support_name' ) || ! val( 'shc4wc_support_email' ) || ! val( 'shc4wc_support_message' ) ) {
				window.alert( cfg.strings && cfg.strings.fillRequired ? cfg.strings.fillRequired : 'Please fill required fields.' );
				return;
			}

			var url = 'https://wa.me/' + encodeURIComponent( number ) + '?text=' + encodeURIComponent( buildWhatsAppText() );
			window.open( url, '_blank', 'noopener,noreferrer' );
		} );
	} );
}() );
