<?php
defined( 'ABSPATH' ) || exit;

/**
 * Format a price using WooCommerce store currency (Settings → General → Currency).
 *
 * @param float|string $amount Amount.
 * @return string Plain-text formatted price (no HTML).
 */
function formulate_pricing_format_price( $amount ) {
	$amount = (float) $amount;

	if ( function_exists( 'wc_format_localized_price' ) && function_exists( 'get_woocommerce_currency' ) ) {
		$currency = get_woocommerce_currency();
		$price    = wc_format_localized_price( $amount );
		$symbol   = html_entity_decode(
			get_woocommerce_currency_symbol( $currency ),
			ENT_QUOTES | ENT_HTML5,
			'UTF-8'
		);
		$format   = html_entity_decode( get_woocommerce_price_format(), ENT_QUOTES | ENT_HTML5, 'UTF-8' );

		return sprintf( $format, $symbol, $price );
	}

	$decimals = function_exists( 'wc_get_price_decimals' ) ? wc_get_price_decimals() : 2;
	$decimal  = function_exists( 'wc_get_price_decimal_separator' ) ? wc_get_price_decimal_separator() : '.';
	$thousand = function_exists( 'wc_get_price_thousand_separator' ) ? wc_get_price_thousand_separator() : ',';

	return number_format( $amount, $decimals, $decimal, $thousand );
}

/**
 * WooCommerce price format args for frontend JavaScript.
 *
 * @return array|null
 */
function formulate_pricing_price_format_for_js() {
	if ( ! function_exists( 'get_woocommerce_currency' ) ) {
		return null;
	}

	$currency = get_woocommerce_currency();

	return array(
		'currency'  => $currency,
		'symbol'    => html_entity_decode(
			get_woocommerce_currency_symbol( $currency ),
			ENT_QUOTES | ENT_HTML5,
			'UTF-8'
		),
		'decimal'   => wc_get_price_decimal_separator(),
		'thousand'  => wc_get_price_thousand_separator(),
		'precision' => wc_get_price_decimals(),
		'format'    => html_entity_decode( get_woocommerce_price_format(), ENT_QUOTES | ENT_HTML5, 'UTF-8' ),
	);
}
