<?php
/**
 * Formats WooCommerce stock data for printable labels and overviews.
 *
 * @package PhoenixWP\PriceLabels
 */

declare(strict_types=1);

namespace PhoenixWP\PriceLabels\Labels;

use WC_Product;

defined( 'ABSPATH' ) || exit;

/**
 * Builds human-readable stock strings via WC CRUD only.
 */
final class Stock_Text_Formatter {

	/**
	 * @param WC_Product $product Product or variation.
	 */
	public static function format( WC_Product $product ): string {
		if ( $product->managing_stock() ) {
			$quantity = $product->get_stock_quantity();

			if ( null === $quantity ) {
				return '';
			}

			$text = (string) (int) $quantity;
		} elseif ( $product->is_in_stock() ) {
			$text = __( 'In stock', 'phoenix-price-labels-for-woocommerce' );
		} else {
			$text = __( 'Out of stock', 'phoenix-price-labels-for-woocommerce' );
		}

		/**
		 * Filters stock text on checkout overview rows.
		 *
		 * @param string     $text    Stock display text.
		 * @param WC_Product $product Source product.
		 */
		return (string) apply_filters( 'phoenix_wp_price_labels_stock_text', $text, $product );
	}
}
