<?php
/**
 * Button for refreshing the configs.
 *
 * @package EaLab\WP_Deployer
 */

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

if ( ! function_exists( 'deploy_sync_content__refresh_config_button' ) ) {
	/**
	 * Button for refreshing the configs.
	 *
	 * @return string The HTML.
	 */
	function deploy_sync_content__refresh_config_button(): void {
		?>
		<deploy-sync-content--refresh-config-button
			data-url="<?php echo esc_attr( rest_url( 'deploy-sync-content/v1/deployment/config/refresh' ) ) ?>">
			<button class="button"><?php echo esc_html__( 'Refresh the deployment config', 'deploy-sync-content' ) ?></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__( 'This will update the loaded deployment configuration.', 'deploy-sync-content' ) ?>">
				<i class="bi bi-question-circle-fill"></i></a>
			<p class="description status-text"></p>
		</deploy-sync-content--refresh-config-button>
		<?php

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

		wp_enqueue_script(
			'deploy-sync-content--refresh-config-button',
			plugins_url( '/admin/assets/js/refresh-config-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--refresh-config-button',
			'deploy_sync_content__refreshConfigButton_settings',
			array(
				'rest' => array(
					'nonce' => wp_create_nonce( 'wp_rest' ),
				),
				'text' => array(
					'confirm'           => esc_html__( 'Are you sure you want to refresh the deployment configuration? This will remove any customized deployment configuration on this install.', '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' ),
					'refreshError'      => esc_html__( 'The configuration could not be refreshed.', 'deploy-sync-content' ),
					'refreshSuccessful' => esc_html__( 'The configuration has been refreshed successfully.', 'deploy-sync-content' ),
					'refreshing'        => esc_html__( 'Refreshing the configuration…', 'deploy-sync-content' ),
				),
			)
		);
	}
}

deploy_sync_content__refresh_config_button();
