<?php
/**
 * Subform template for the transmutation advanced settings.
 *
 * @package EaLab\WP_Deployer
 */

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

require_once DEPLOY_SYNC_CONTENT__PLUGIN_DIR . '/admin/partials/component/dynamic-tag-template.php';

if ( ! function_exists( 'deploy_sync_content__processor_subform__transmutation' ) ) {
	/**
	 * Subform template for the transmutation advanced settings.
	 *
	 * This will be used as the base of subforms by JavaScript.
	 *
	 * @param array|null $context The context to build this component:
	 *     - "as_template" (bool): Set to true to build a template instead of the real markup.
	 *     - "line_index" (int): Index of the line where this subform is located.
	 *     - "processor_item" (array): The processor item values.
	 */
	function deploy_sync_content__advanced_settings__processor_subform__transmutation( array $context = null ): void {
		$as_template = $context['as_template'] ?? false;
		?>
		<div
			class="deploy-sync-content--settings--processor-subform--transmutation"
			<?php echo esc_attr( $as_template ? 'data-dtt-template' : '' ); ?>>
			<label>
				<?php echo esc_html__( 'Data walker pattern', 'deploy-sync-content' ); ?>
				<?php if ( $as_template ) : ?>
					<dtt-dynamic-tag-template
						data-dtt-html-tag="input"
						data-dtt-format-attrs="name"
						data-dtt-format-attr-name="line_index"
						name="processors[{line_index}][processor_subform][data_walker_pattern]"
						type="text"
						value="">
					</dtt-dynamic-tag-template>
				<?php else : ?>
					<input
						name="processors[<?php echo esc_attr( $context['line_index'] ?? '' ); ?>][processor_subform][data_walker_pattern]"
						type="text"
						value="<?php echo esc_attr( $context['processor_item']['data_walker_pattern'] ?? '' ); ?>">
				<?php endif; ?>
			</label>
			<label>
				<?php echo esc_html__( 'Transmutation handler', 'deploy-sync-content' ); ?>
				<?php if ( $as_template ) : ?>
					<dtt-dynamic-tag-template
						data-dtt-html-tag="input"
						data-dtt-format-attrs="name"
						data-dtt-format-attr-name="line_index"
						name="processors[{line_index}][processor_subform][transmutation_handler]"
						type="text"
						value="">
					</dtt-dynamic-tag-template>
				<?php else : ?>
					<input
						name="processors[<?php echo esc_attr( $context['line_index'] ?? '' ); ?>][processor_subform][transmutation_handler]"
						type="text"
						value="<?php echo esc_attr( $context['processor_item']['transmutation_handler'] ?? '' ); ?>">
				<?php endif; ?>
			</label>
			<?php
			/*
			 * TODO: The transmutation_handler_options key is not yet needed
			 * by the current transmutation implementation. When needed,
			 * a subform should take place here to allow its configuration.
			*/
			?>
		</div>
		<?php
	}
}
