<?php
/**
 * Display the progress of a batch.
 *
 * @package EaLab\WP_Deployer
 */

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

/**
 * Display the progress of a batch.
 */
function deploy_sync_content__batch_progress(): void {
	$batch_id = sanitize_text_field( wp_unslash( $_GET['batch_id'] ?? '' ) );

	if ( ! is_string( $batch_id ) || 0 === strlen( $batch_id ) || 128 < strlen( $batch_id ) ) {
		// No batch to link to.
		return;
	}

	?>
	<deploy-sync-content--batch-progress data-url="<?php echo esc_attr( rest_url( 'deploy-sync-content/v1/batch/' . $batch_id . '/step/process' ) ) ?>">
		<div class="status-text in-progress"><?php echo esc_html__( 'Loading the queue status…', 'deploy-sync-content' ) ?></div>
	</deploy-sync-content--batch-progress>
	<?php

	/**
	 * Enqueue the assets for the tracking queue progress component.
	 */
	wp_enqueue_style(
		'deploy-sync-content--batch-progress',
		plugins_url( '/admin/assets/css/batch-progress.css', DEPLOY_SYNC_CONTENT__PLUGIN_FILE ),
		array(),
		DEPLOY_SYNC_CONTENT__VERSION
	);

	wp_enqueue_script(
		'deploy-sync-content--batch-progress',
		plugins_url( '/admin/assets/js/batch-progress.js', DEPLOY_SYNC_CONTENT__PLUGIN_FILE ),
		array(),
		DEPLOY_SYNC_CONTENT__VERSION,
		true
	);

	// Attach the nonce to the script.
	wp_localize_script(
		'deploy-sync-content--batch-progress',
		'deploy_sync_content__batchProgress_settings',
		array(
			'rest' => array(
				'nonce' => wp_create_nonce( 'wp_rest' ),
			),
			'text' => array(
				'loadingQueueStatus' => esc_html__( 'Loading the queue status…', 'deploy-sync-content' ),
				'batchStepsOutput'   => esc_html__( 'Batch steps output:', 'deploy-sync-content' ),
				'networkError'       => esc_html__( 'A network error occurred.', 'deploy-sync-content' ),
				'remainingSteps'     => esc_html__( 'Remaining steps: ', 'deploy-sync-content' ),
				'queueProcessed'     => esc_html__( 'The queue has been processed.', 'deploy-sync-content' ),
			),
		)
	);
}
