<?php
/**
 * Shared field-rendering helpers for CPT Builder admin views.
 *
 * @package CptBuilder
 * @since 1.0.0
 */

namespace CptBuilder;

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

if ( ! function_exists( __NAMESPACE__ . '\\cptb_arg' ) ) {
	/**
	 * Read an argument value from a definition with default fallback.
	 *
	 * @param array<string, mixed> $definition Definition.
	 * @param string               $key        Argument key.
	 * @return mixed
	 */
	function cptb_arg( array $definition, string $key ): mixed {
		$args     = isset( $definition['args'] ) && is_array( $definition['args'] ) ? $definition['args'] : [];
		$defaults = Config::defaultArgs();

		return $args[ $key ] ?? ( $defaults[ $key ] ?? '' );
	}
}

if ( ! function_exists( __NAMESPACE__ . '\\cptb_label' ) ) {
	/**
	 * Read a label value from a definition.
	 *
	 * @param array<string, mixed> $definition Definition.
	 * @param string               $key        Label key.
	 */
	function cptb_label( array $definition, string $key ): string {
		$labels = isset( $definition['labels'] ) && is_array( $definition['labels'] ) ? $definition['labels'] : [];

		return isset( $labels[ $key ] ) ? (string) $labels[ $key ] : '';
	}
}

if ( ! function_exists( __NAMESPACE__ . '\\cptb_text_row' ) ) {
	/**
	 * Render a labelled text input row.
	 *
	 * @param string $name        Input name attribute.
	 * @param string $label       Field label.
	 * @param string $value       Current value.
	 * @param string $help        Help description.
	 * @param string $placeholder Placeholder text.
	 * @param string $extra_attrs Extra raw attributes (already escaped).
	 */
	function cptb_text_row( string $name, string $label, string $value, string $help = '', string $placeholder = '', string $extra_attrs = '' ): void {
		$id = 'cptb-' . preg_replace( '/[^a-z0-9]+/', '-', strtolower( $name ) );
		?>
		<tr>
			<th scope="row"><label for="<?php echo esc_attr( $id ); ?>"><?php echo esc_html( $label ); ?></label></th>
			<td>
				<input type="text" id="<?php echo esc_attr( $id ); ?>" name="<?php echo esc_attr( $name ); ?>" value="<?php echo esc_attr( $value ); ?>" class="regular-text" placeholder="<?php echo esc_attr( $placeholder ); ?>" <?php echo $extra_attrs; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- caller-supplied static attrs. ?>>
				<?php if ( $help !== '' ) : ?>
					<p class="description"><?php echo esc_html( $help ); ?></p>
				<?php endif; ?>
			</td>
		</tr>
		<?php
	}
}

if ( ! function_exists( __NAMESPACE__ . '\\cptb_checkbox_row' ) ) {
	/**
	 * Render a labelled checkbox row.
	 *
	 * @param string $name    Input name attribute.
	 * @param string $label   Field label.
	 * @param bool   $checked Whether checked.
	 * @param string $help    Help description.
	 */
	function cptb_checkbox_row( string $name, string $label, bool $checked, string $help = '' ): void {
		$id = 'cptb-' . preg_replace( '/[^a-z0-9]+/', '-', strtolower( $name ) );
		?>
		<tr>
			<th scope="row"><?php echo esc_html( $label ); ?></th>
			<td>
				<label class="cptb-toggle" for="<?php echo esc_attr( $id ); ?>">
					<input type="checkbox" id="<?php echo esc_attr( $id ); ?>" name="<?php echo esc_attr( $name ); ?>" value="1" <?php checked( $checked ); ?>>
					<span><?php echo esc_html( $help ); ?></span>
				</label>
			</td>
		</tr>
		<?php
	}
}

if ( ! function_exists( __NAMESPACE__ . '\\cptb_arg_checkbox' ) ) {
	/**
	 * Render a checkbox row bound to a definition argument.
	 *
	 * @param array<string, mixed> $definition Definition.
	 * @param string               $arg_key    Argument key.
	 * @param string               $label      Field label.
	 * @param string               $help       Help description.
	 */
	function cptb_arg_checkbox( array $definition, string $arg_key, string $label, string $help = '' ): void {
		cptb_checkbox_row( 'args[' . $arg_key . ']', $label, (bool) cptb_arg( $definition, $arg_key ), $help );
	}
}

if ( ! function_exists( __NAMESPACE__ . '\\cptb_arg_text' ) ) {
	/**
	 * Render a text row bound to a definition argument.
	 *
	 * @param array<string, mixed> $definition  Definition.
	 * @param string               $arg_key     Argument key.
	 * @param string               $label       Field label.
	 * @param string               $help        Help description.
	 * @param string               $placeholder Placeholder.
	 */
	function cptb_arg_text( array $definition, string $arg_key, string $label, string $help = '', string $placeholder = '', string $extra_attrs = '' ): void {
		cptb_text_row( 'args[' . $arg_key . ']', $label, (string) cptb_arg( $definition, $arg_key ), $help, $placeholder, $extra_attrs );
	}
}

if ( ! function_exists( __NAMESPACE__ . '\\cptb_tax_arg' ) ) {
	/**
	 * Read a taxonomy argument value from a definition with default fallback.
	 *
	 * @param array<string, mixed> $definition Definition.
	 * @param string               $key        Argument key.
	 * @return mixed
	 */
	function cptb_tax_arg( array $definition, string $key ): mixed {
		$args     = isset( $definition['args'] ) && is_array( $definition['args'] ) ? $definition['args'] : [];
		$defaults = Config::defaultTaxonomyArgs();

		return $args[ $key ] ?? ( $defaults[ $key ] ?? '' );
	}
}

if ( ! function_exists( __NAMESPACE__ . '\\cptb_tax_arg_checkbox' ) ) {
	/**
	 * Render a checkbox row bound to a taxonomy definition argument.
	 *
	 * @param array<string, mixed> $definition Definition.
	 * @param string               $arg_key    Argument key.
	 * @param string               $label      Field label.
	 * @param string               $help       Help description.
	 */
	function cptb_tax_arg_checkbox( array $definition, string $arg_key, string $label, string $help = '' ): void {
		cptb_checkbox_row( 'args[' . $arg_key . ']', $label, (bool) cptb_tax_arg( $definition, $arg_key ), $help );
	}
}

if ( ! function_exists( __NAMESPACE__ . '\\cptb_tax_arg_text' ) ) {
	/**
	 * Render a text row bound to a taxonomy definition argument.
	 *
	 * @param array<string, mixed> $definition  Definition.
	 * @param string               $arg_key     Argument key.
	 * @param string               $label       Field label.
	 * @param string               $help        Help description.
	 * @param string               $placeholder Placeholder.
	 */
	function cptb_tax_arg_text( array $definition, string $arg_key, string $label, string $help = '', string $placeholder = '', string $extra_attrs = '' ): void {
		cptb_text_row( 'args[' . $arg_key . ']', $label, (string) cptb_tax_arg( $definition, $arg_key ), $help, $placeholder, $extra_attrs );
	}
}
