<?php
// IMPORTANT: This plugin is dynamically updated - MODIFICATIONS WILL BE OVERWRITTEN

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

$script_src = sprintf( '%s/nutsheller-esm.js', untrailingslashit( $nutshell_domain ) );
?>
<?php // phpcs:disable WordPress.WP.EnqueuedResources ?>
<?php // - We specifically *WANT* to inline these scripts this way vs enqueueing ?>
<?php // - We do not want WP or any plugins to be modifying these scripts ?>

<!-- --------- START PLUGIN NUTSHELL-ANALYTICS - HEADER SCRIPTS --------- -->

<!-- Nutshell - Primary Tracking Script -->
<script type="text/javascript" data-registered="nutshell-plugin">
	(function(n,u,t){n[u]=n[u]||function(){(n[u].q=n[u].q||[]).push(arguments)}}(window,'Nutsheller'));
	Nutsheller( 'boot', {
		instance: '<?php echo esc_js( $nutshell_instance_id ); ?>',
		target: 'nutshell-boot-<?php echo esc_js( $nutshell_instance_id ); ?>',
		authToken: '<?php echo esc_js( $nutshell_auth_token ); ?>'
	});
</script>
<script type="module" data-registered="nutshell-plugin" src="<?php echo esc_url( $script_src ); ?>"></script>

<!-- Helper Script -->
<script type="text/javascript" data-registered="nutshell-plugin">
<?php
	/**
	 * This method will send data as a form lead
	 *
	 * @param data - array of objects with field data, eg:
	 *  [
	 *    { name: 'name', value: 'Bill' },
	 *    { name: 'email', value: 'bill@example.com' }
	 *  ]
	 *
	 * @param formId - id for form to submit
	 *
	 */
?>
	/* global mcfx */
	window.mcfxCaptureCustomFormData = function( data, formId = 'form-from-mcfxCaptureCustomFormData' ) {
		if( 'undefined' === typeof mcfx ) {
			console.warn( '"mcfx" is not defined. Please ensure that the MCFX pixel loads before usage.' );
			return;
		}
		
		const formEl = document.createElement('form');
		formEl.id = formId;
		for ( const field of data ) {
			const fieldEl = document.createElement('input');
			fieldEl.type = 'hidden';
			for ( const key in field ) {
				fieldEl[key] = field[key];
			}
			formEl.appendChild(fieldEl);
		}
		mcfx( 'capture', formEl );
	}
</script>

<!-- --------- END PLUGIN NUTSHELL-ANALYTICS - HEADER SCRIPTS --------- -->

<?php // phpcs:enable WordPress.WP.EnqueuedResources ?>

<?php // IMPORTANT: This plugin is dynamically updated - MODIFICATIONS WILL BE OVERWRITTEN ?>
