<?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\Measurements\Adapters\JsonSerializers;

use WC_Price_Calculator_Measurement;

defined( 'ABSPATH' ) or exit;

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

	/**
	 * Converts a measurement to an array.
	 *
	 * @since 3.24.0
	 *
	 * @param WC_Price_Calculator_Measurement $measurement
	 * @return array<string, mixed>
	 */
	public static function convert(WC_Price_Calculator_Measurement $measurement): array
	{
		return [
			'name'     => $measurement->get_name(),
			'unit'     => $measurement->get_unit(),
			'label'    => $measurement->get_label(),
			'editable' => $measurement->is_editable(),
			'options'  => $measurement->get_options(),
		];
	}

	/**
	 * Gets the JSON schema for a measurement.
	 *
	 * @since 3.24.0
	 *
	 * @return array<string, mixed>
	 */
	public static function getJsonSchema() : array
	{
		return [
			'type'       => 'object',
			'properties' => [
				'name'     => [
					'type'        => 'string',
					'description' => __( 'The measurement name (e.g. "length", "width", "area", "volume", "weight").', 'woocommerce-measurement-price-calculator' ),
				],
				'unit'     => [
					'type'        => 'string',
					'description' => __( 'The unit abbreviation for the measurement (e.g. "ft", "sq m", "kg").', 'woocommerce-measurement-price-calculator' ),
				],
				'label'    => [
					'type'        => 'string',
					'description' => __( 'Display label shown to the customer.', 'woocommerce-measurement-price-calculator' ),
				],
				'editable' => [
					'type'        => 'boolean',
					'description' => __( 'Whether the customer can edit this measurement in the calculator.', 'woocommerce-measurement-price-calculator' ),
				],
				'options'  => [
					'type'                 => 'object',
					'description'          => __( 'Preset options keyed by value with display label as the value.', 'woocommerce-measurement-price-calculator' ),
					'additionalProperties' => [ 'type' => 'string' ],
				],
			],
		];
	}
}
