<?php
/**
 * WooCommerce Measurement Price Calculator
 *
 * This source file is subject to the GNU General Public License v3.0
 * that is bundled with this package in the file license.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-3.0.html
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@skyverge.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade WooCommerce Measurement Price Calculator to newer
 * versions in the future. If you wish to customize WooCommerce Measurement Price Calculator for your
 * needs please refer to http://docs.woocommerce.com/document/measurement-price-calculator/ for more information.
 *
 * @author    SkyVerge
 * @copyright Copyright (c) 2012-2026, SkyVerge, Inc. (info@skyverge.com)
 * @license   http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
 */

namespace SkyVerge\WooCommerce\Measurement_Price_Calculator\Products\Adapters\JsonSerializers;

use SkyVerge\WooCommerce\Measurement_Price_Calculator\Measurements\Adapters\JsonSerializers\MeasurementSerializer;
use WC_Price_Calculator_Measurement;
use WC_Price_Calculator_Settings;

defined( 'ABSPATH' ) or exit;

/**
 * Serializes a {@see WC_Price_Calculator_Settings} to an array for ability output.
 *
 * @since 3.24.0
 */
class ProductCalculatorSettingsSerializer {

	/**
	 * Converts calculator settings to an array.
	 *
	 * @since 3.24.0
	 *
	 * @param WC_Price_Calculator_Settings $settings
	 * @return array<string, mixed>
	 */
	public static function convert(WC_Price_Calculator_Settings $settings): array
	{
		$product = $settings->get_product();

		return [
			'product_id'                 => $product ? $product->get_id() : 0,
			'calculator_enabled'         => $settings->is_calculator_enabled(),
			'calculator_type'            => $settings->get_calculator_type(),
			'pricing_enabled'            => $settings->is_pricing_enabled(),
			'pricing_calculator_enabled' => $settings->is_pricing_calculator_enabled(),
			'pricing_inventory_enabled'  => $settings->is_pricing_inventory_enabled(),
			'pricing_unit'               => $settings->get_pricing_unit(),
			'pricing_label'              => $settings->get_pricing_label(),
			'measurements'               => array_map(
				function (WC_Price_Calculator_Measurement $measurement): array {
					return $measurement->jsonSerialize();
				},
				$settings->get_calculator_measurements()
			),
			'pricing_rules'              => array_map(
				[PricingRuleSerializer::class, 'convert'],
				$settings->get_pricing_rules()
			),
		];
	}

	/**
	 * Gets the JSON schema for product calculator settings.
	 *
	 * @since 3.24.0
	 *
	 * @return array<string, mixed>
	 */
	public static function getJsonSchema() : array
	{
		return [
			'type'       => 'object',
			'properties' => [
				'product_id'                 => [
					'type'        => 'integer',
					'description' => __( 'The product ID.', 'woocommerce-measurement-price-calculator' ),
				],
				'calculator_enabled'         => [
					'type'        => 'boolean',
					'description' => __( 'Whether the measurement calculator is enabled for this product.', 'woocommerce-measurement-price-calculator' ),
				],
				'calculator_type'            => [
					'type'        => 'string',
					'description' => __( 'The calculator type (e.g. "dimension", "area", "volume", "weight"). Empty string if no calculator is configured.', 'woocommerce-measurement-price-calculator' ),
				],
				'pricing_enabled'            => [
					'type'        => 'boolean',
					'description' => __( 'Whether "show price per unit" is enabled.', 'woocommerce-measurement-price-calculator' ),
				],
				'pricing_calculator_enabled' => [
					'type'        => 'boolean',
					'description' => __( 'Whether the product price is defined per unit (e.g. $/sq ft) and the customer purchases a custom amount.', 'woocommerce-measurement-price-calculator' ),
				],
				'pricing_inventory_enabled'  => [
					'type'        => 'boolean',
					'description' => __( 'Whether inventory is tracked in measurement units rather than per item.', 'woocommerce-measurement-price-calculator' ),
				],
				'pricing_unit'               => [
					'type'        => 'string',
					'description' => __( 'The unit used for pricing (e.g. "ft", "sq m"). Empty string if pricing is not enabled.', 'woocommerce-measurement-price-calculator' ),
				],
				'pricing_label'              => [
					'type'        => 'string',
					'description' => __( 'Display label for the pricing unit (e.g. "ft", "square metre").', 'woocommerce-measurement-price-calculator' ),
				],
				'measurements'               => [
					'type'        => 'array',
					'description' => __( 'The measurement inputs defined for the calculator.', 'woocommerce-measurement-price-calculator' ),
					'items'       => MeasurementSerializer::getJsonSchema(),
				],
				'pricing_rules'              => [
					'type'        => 'array',
					'description' => __( 'Tiered pricing rules, in terms of the pricing unit.', 'woocommerce-measurement-price-calculator' ),
					'items'       => PricingRuleSerializer::getJsonSchema(),
				],
			],
		];
	}
}
