<?php
/**
 * Global helper functions.
 *
 * This file intentionally declares NO namespace so the helpers live in the global
 * namespace and can be called unqualified from any of the plugin's namespaced classes
 * (PHP resolves unqualified function calls against the global namespace as a fallback).
 *
 * @package FunnelKit_Square
 */

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

if ( ! function_exists( 'fkwcsq_idempotency_key' ) ) {
	/**
	 * Generate a Square-compatible idempotency key.
	 *
	 * Square caps the `idempotency_key` field at 45 characters on every endpoint. We clamp
	 * to 40 to keep a safety margin under that limit while preserving the high-entropy front
	 * of the random value. Even after truncation the surviving random tail retains ~100 bits
	 * of entropy, so keys remain unique.
	 *
	 * @param string $prefix Optional short prefix for log readability.
	 * @return string Idempotency key, at most 40 characters.
	 */
	function fkwcsq_idempotency_key( $prefix = 'fkwcsq_' ) {
		return substr( $prefix . bin2hex( random_bytes( 16 ) ), 0, 40 );
	}
}
