*/
namespace RankMath\Wizard;
use RankMath\KB;
use RankMath\Admin\Importers\Detector;
defined( 'ABSPATH' ) || exit;
/**
 * Step class.
 */
class Import implements Wizard_Step {
	/**
	 * Render step body.
	 *
	 * @param object $wizard Wizard class instance.
	 *
	 * @return void
	 */
	public function render( $wizard ) {
		?>
		
		cmb->show_form(); ?>
		
		
		
		detect();
		$plugins  = $this->set_priority( $plugins );
		$count = 0;
		foreach ( $plugins as $slug => $plugin ) {
			$checked       = 'checked';
			$multi_checked = 'multicheck-checked';
			$choices       = array_keys( $plugin['choices'] );
			if ( isset( $plugin['checked'] ) && false === $plugin['checked'] ) {
				$checked       = '';
				$multi_checked = '';
				$choices       = [];
			}
			$field_args = [
				'id'           => 'import_from_' . $slug,
				'type'         => 'group',
				'description'  => '',
				'before_group' => 0 === $count ? '' . esc_html__( 'Input Data From:', 'rank-math' ) . '
' : '',
				'repeatable'   => false,
				'options'      => [
					'group_title' => $plugin['name'],
					'sortable'    => false,
					'closed'      => true,
				],
			];
			$group_id  = $wizard->cmb->add_field( $field_args );
			$is_active = is_plugin_active( $plugin['file'] );
			$wizard->cmb->add_group_field(
				$group_id,
				[
					'id'         => $slug . '_meta',
					'type'       => 'multicheck',
					'repeatable' => false,
					'desc'       => $this->get_choice_description( $slug, $plugin, $is_active ),
					'options'    => $plugin['choices'],
					'default'    => $choices,
					'dep'        => [ [ 'import_from', $slug ] ],
					'classes'    => 'nob nopb cmb-multicheck-inline with-description ' . $multi_checked,
					'attributes' => [ 'data-active' => $is_active ],
				]
			);
			$count++;
			// Add checkbox field to Recalculate SEO Scores.
			// But not for Redirections.
			if ( 'redirections' === $slug ) {
				continue;
			}
			$wizard->cmb->add_group_field(
				$group_id,
				[
					'id'         => $slug . '_recalculate',
					'type'       => 'checkbox',
					'repeatable' => false,
					'desc'       => esc_html__( 'Recalculate SEO Scores', 'rank-math' ),
					'value'      => 'recalculate',
					'default'    => 'recalculate',
					'dep'        => [ [ 'import_from', $slug ] ],
					'classes'    => 'nob nopb recalculate-scores',
					'attributes' => [
						'data-active' => $is_active,
						'value'       => 'recalculate',
					],
				]
			);
		}
	}
	/**
	 * Set plugins priority.
	 *
	 * @param array $plugins Array of detected plugins.
	 *
	 * @return array
	 */
	private function set_priority( $plugins ) {
		$checked  = false;
		$priority = array_intersect( [ 'seopress', 'yoast', 'yoast-premium', 'aioseo' ], array_keys( $plugins ) );
		foreach ( $priority as $slug ) {
			if ( ! $checked ) {
				$checked                     = true;
				$plugins[ $slug ]['checked'] = true;
				continue;
			}
			$plugins[ $slug ]['checked'] = false;
		}
		return $plugins;
	}
	/**
	 * Save handler for step.
	 *
	 * @param array  $values Values to save.
	 * @param object $wizard Wizard class instance.
	 *
	 * @return bool
	 */
	public function save( $values, $wizard ) {
		delete_option( 'rank_math_yoast_block_posts' );
		return true;
	}
	/**
	 * Get description for choice field.
	 *
	 * @param string  $slug      Plugin slug.
	 * @param array   $plugin    Plugin info array.
	 * @param boolean $is_active Is plugin active.
	 *
	 * @return string
	 */
	private function get_choice_description( $slug, $plugin, $is_active ) {
		/* translators: 1 is plugin name */
		$desc = 'aio-rich-snippet' === $slug ? esc_html__( 'Import meta data from the %1$s plugin.', 'rank-math' ) : esc_html__( 'Import settings and meta data from the %1$s plugin.', 'rank-math' );
		/* translators: 2 is link to Knowledge Base article */
		$desc .= ' ' . __( 'The process may take a few minutes if you have a large number of posts or pages Learn more about the import process here.', 'rank-math' );
		if ( $is_active ) {
			/* translators: 1 is plugin name */
			$desc .= '
' . __( ' %1$s plugin will be disabled automatically moving forward to avoid conflicts. It is thus recommended to import the data you need now.', 'rank-math' );
		}
		return sprintf( wp_kses_post( $desc ), $plugin['name'], KB::get( 'seo-import', 'SW Import Step' ) );
	}
}