<?php

/**
 * JSON Schema normalization for LLM function parameters.
 *
 * @package LolaCore\Actions
 * @license GPL-2.0-or-later
 */

declare(strict_types=1);

namespace LolaCore\Actions;

if (! defined('ABSPATH')) {
	exit;
}

/**
 * Ensures tool parameter schemas serialize as JSON objects, not empty arrays.
 *
 * OpenAI/DeepSeek reject `parameters: []` and `properties: []`; both must be objects.
 */
final class ToolParameterSchema {

	/**
	 * @param array<string, mixed> $params Tool parameter JSON Schema.
	 * @return array<string, mixed>
	 */
	public static function normalize( array $params ): array {
		if ($params === array() || ! isset($params['type'])) {
			return array(
				'type'       => 'object',
				'properties' => new \stdClass(),
			);
		}

		if (isset($params['properties']) && is_array($params['properties']) && $params['properties'] === array()) {
			$params['properties'] = new \stdClass();
		}

		return $params;
	}
}
