<?php
/**
 * Button for reindexing the items.
 *
 * @package EaLab\WP_Deployer
 */

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

if ( ! function_exists( 'deploy_sync_content__reindex_items_button' ) ) {
	/**
	 * Button for refreshing the configs.
	 *
	 * @return string The HTML.
	 */
	function deploy_sync_content__reindex_items_button(): void {
		$endpoint_url = rest_url( 'deploy-sync-content/v1/deployment/items/reindex' );

		$button_label = __( 'Reindex items', 'deploy-sync-content' );
		$tooltip      = __( 'This will index items missing from the tracking system.', 'deploy-sync-content' );

		$tracking_progress_url = admin_url( 'admin.php?page=deploy-sync-content&tab=tracking-queue-progress' );

		?>
		<deploy-sync-content--reindex-items-button data-url="<?php echo esc_attr( $endpoint_url ); ?>">
			<button class="button"><?php echo esc_html( $button_label ); ?></button>&nbsp;&nbsp;<a href="javascript:;"
				class="tooltip-icon"
				data-bs-placement="top" data-bs-custom-class="custom-tooltip" data-bs-toggle="tooltip"
				data-bs-title="<?php echo esc_attr( $tooltip ); ?>"><i class="bi bi-question-circle-fill"></i></a>
			<p>
				<?php
				echo wp_kses(
					sprintf(
						// translators: %s is a URL.
						__( '<a href="%s">Click here</a> to view the indexation progress.', 'deploy-sync-content' ),
						esc_attr( $tracking_progress_url )
					),
					array(
						'a' => array( 'href' => array() ),
					)
				);
				?>
			</p>
			<p class="description status-text"></p>
		</deploy-sync-content--reindex-items-button>
		<?php

		/**
		 * Enqueue the assets.
		 */
		wp_enqueue_style(
			'deploy-sync-content--reindex-items-button',
			plugins_url( '/admin/assets/css/reindex-items-button.css', DEPLOY_SYNC_CONTENT__PLUGIN_FILE ),
			array(),
			DEPLOY_SYNC_CONTENT__VERSION
		);

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

		// Attach the nonce to the script.
		wp_localize_script(
			'deploy-sync-content--reindex-items-button',
			'deploy_sync_content__reindexItemsButton_settings',
			array(
				'rest' => array(
					'nonce' => wp_create_nonce( 'wp_rest' ),
				),
				'text' => array(
					'confirm'           => esc_html__( 'Please confirm the items re-indexation. The re-indexation will run in the background.', 'deploy-sync-content' ),
					'error'             => esc_html__( 'An error occurred. Please check your network status or the basic Deploy & Sync Content settings, or try again later.', 'deploy-sync-content' ),
					'requestError'      => esc_html__( 'The items could not be re-indexed.', 'deploy-sync-content' ),
					'requestInProgress' => esc_html__( 'Queueing items for re-indexation…', 'deploy-sync-content' ),
					'requestSuccessful' => esc_html__( 'The items have been queued for re-indexation.', 'deploy-sync-content' ),
				),
			)
		);
	}
}

deploy_sync_content__reindex_items_button();
