<?php
/** Shared closed and bounded public output primitives for form providers. */

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

define( 'WPAGENT_FORMS_PUBLIC_MAX_FORMS', 100 );
define( 'WPAGENT_FORMS_PUBLIC_MAX_FIELDS', 200 );
define( 'WPAGENT_FORMS_PUBLIC_MAX_OPTIONS', 100 );
define( 'WPAGENT_FORMS_PUBLIC_MAX_TEXT', 10000 );
define( 'WPAGENT_FORMS_PUBLIC_MAX_SHORT_TEXT', 1000 );

/** @return WP_Error */
function wpagent_forms_public_output_error( string $provider, string $field ): WP_Error {
	return new WP_Error(
		'wpagent_' . sanitize_key( $provider ) . '_public_output_invalid',
		__( 'Der Formularanbieter lieferte eine öffentliche Konfiguration außerhalb der unterstützten Größen- oder Typgrenzen.', 'wp-agent-companion' ),
		array( 'status' => 500, 'provider' => $provider, 'field' => $field )
	);
}

/** @return string|WP_Error */
function wpagent_forms_public_string( $value, int $max_length, string $provider, string $field, bool $allow_empty = true ) {
	if ( ! is_string( $value ) || strlen( $value ) > $max_length || wp_check_invalid_utf8( $value, true ) !== $value || ( ! $allow_empty && '' === $value ) ) {
		return wpagent_forms_public_output_error( $provider, $field );
	}
	return $value;
}

/** @return array|WP_Error */
function wpagent_forms_public_list( $value, int $max_items, string $provider, string $field ) {
	if ( ! is_array( $value ) || ! array_is_list( $value ) || count( $value ) > $max_items ) {
		return wpagent_forms_public_output_error( $provider, $field );
	}
	return $value;
}

/** @return array|WP_Error */
function wpagent_forms_public_object( $value, int $max_properties, string $provider, string $field ) {
	// PHP treats [] as a list, but providers commonly use it for an empty map.
	if ( ! is_array( $value ) || ( array_is_list( $value ) && array() !== $value ) || count( $value ) > $max_properties ) {
		return wpagent_forms_public_output_error( $provider, $field );
	}
	return $value;
}

/** @return array<string,mixed> */
function wpagent_forms_public_string_schema( int $max_length, bool $required = false ): array {
	$schema = array( 'type' => 'string', 'maxLength' => $max_length );
	if ( $required ) {
		$schema['minLength'] = 1;
	}
	return $schema;
}

/** @param array<string,mixed> $properties @param string[] $required @return array<string,mixed> */
function wpagent_forms_public_object_schema( array $properties, array $required ): array {
	return array(
		'type'                 => 'object',
		'additionalProperties' => false,
		'properties'           => $properties,
		'required'             => $required,
	);
}

/** @param array<string,mixed> $items @return array<string,mixed> */
function wpagent_forms_public_list_schema( array $items, int $max_items ): array {
	return array( 'type' => 'array', 'maxItems' => $max_items, 'items' => $items );
}

/** @return array<string,mixed> */
function wpagent_forms_public_hash_schema(): array {
	return array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$', 'minLength' => 64, 'maxLength' => 64 );
}
