<?php
/** Read Elementor's global colors and typography through the active native kit. */

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

/** @return array<string,mixed> */
function wpagent_elementor_design_system_output_schema(): array {
	$color = array(
		'type' => 'object',
		'properties' => array(
			'id' => array( 'type' => 'string' ),
			'title' => array( 'type' => 'string' ),
			'value' => array( 'type' => 'string' ),
		),
	);
	$typography = array(
		'type' => 'object',
		'properties' => array(
			'id' => array( 'type' => 'string' ),
			'title' => array( 'type' => 'string' ),
			'styles' => array( 'type' => 'object', 'additionalProperties' => true ),
		),
	);
	return array(
		'type' => 'object',
		'properties' => array(
			'kit_id' => array( 'type' => 'integer' ),
			'elementor_version' => array( 'type' => 'string' ),
			'system_colors' => array( 'type' => 'array', 'items' => $color ),
			'custom_colors' => array( 'type' => 'array', 'items' => $color ),
			'system_typography' => array( 'type' => 'array', 'items' => $typography ),
			'custom_typography' => array( 'type' => 'array', 'items' => $typography ),
			'fallback_font' => array( 'type' => 'string' ),
			'hash' => array( 'type' => 'string' ),
		),
	);
}

function wpagent_ability_get_elementor_design_system(): array {
	return array(
		'label' => __( 'Elementor-Designsystem lesen', 'wp-agent-companion' ),
		'description' => __( 'Liest globale System- und eigene Farben, Typografie, responsive Typografiewerte und Fallback-Schrift aus Elementors aktivem Kit mit Konflikt-Hash.', 'wp-agent-companion' ),
		'category' => 'wp-agent',
		'input_schema' => array( 'type' => 'object', 'properties' => array(), 'additionalProperties' => false ),
		'output_schema' => wpagent_elementor_design_system_output_schema(),
		'execute_callback' => 'wpagent_exec_get_elementor_design_system',
		'permission_callback' => 'wpagent_perm_manage_options',
		'meta' => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ) ),
	);
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_get_elementor_design_system( $input ) {
	$kit = wpagent_elementor_design_active_kit();
	return is_wp_error( $kit ) ? $kit : wpagent_elementor_design_system_state( $kit );
}
