<?php
/**
 * NMI hooks.
 *
 * @package PeachPay
 */

if ( ! defined( 'PEACHPAY_ABSPATH' ) ) {
	exit;
}

// Required PeachPay standard hooks.
add_filter( 'peachpay_register_feature', 'peachpay_nmi_register_feature' );
add_action( 'peachpay_settings_admin_action', 'peachpay_nmi_handle_admin_actions', 10, 1 );

/**
 * Add nonce for AJAX security.
 */
add_action( 'wp_enqueue_scripts', 'peachpay_nmi_add_nonce', 20 );

/**
 * Add AJAX nonce configuration for NMI scripts.
 */
function peachpay_nmi_add_nonce() {
	// Check if the checkout script is enqueued.
	if ( wp_script_is( 'peachpay-nmi-checkout', 'enqueued' ) ) {
		$ajax_config = array(
			'ajax_url'               => admin_url( 'admin-ajax.php' ),
			'create_transaction_url' => WC_AJAX::get_endpoint( 'pp-create-transaction' ),
			'nonce'                  => wp_create_nonce( 'peachpay_nmi_nonce' ),
		);

		wp_localize_script( 'peachpay-nmi-checkout', 'peachpayNMIAjax', $ajax_config );
	}

	// Also add AJAX config globally for any script that might need it.
	wp_localize_script(
		'jquery',
		'peachpayNMIAjax',
		array(
			'ajax_url'               => admin_url( 'admin-ajax.php' ),
			'create_transaction_url' => WC_AJAX::get_endpoint( 'pp-create-transaction' ),
			'nonce'                  => wp_create_nonce( 'peachpay_nmi_nonce' ),
		)
	);
}
