*/
namespace RankMathPro\Schema;
use RankMath\Helper;
use RankMath\Admin\Admin_Helper;
use RankMath\Schema\DB;
use RankMath\Traits\Hooker;
use RankMath\Rest\Sanitize;
use MyThemeShop\Helpers\Str;
use MyThemeShop\Helpers\Param;
use WP_Screen;
defined( 'ABSPATH' ) || exit;
/**
 * Admin class.
 */
class Admin {
	use Hooker;
	/**
	 * The Constructor.
	 */
	public function __construct() {
		$this->action( 'admin_enqueue_scripts', 'overwrite_wplink', 100 );
		$this->action( 'rank_math/admin/before_editor_scripts', 'admin_scripts' );
		$this->action( 'rank_math/admin/editor_scripts', 'deregister_scripts', 99 );
		$this->action( 'save_post', 'save', 10, 2 );
		$this->action( 'edit_form_after_title', 'render_div' );
		$this->filter( 'rank_math/filter_metadata', 'filter_metadata', 10, 2 );
		$this->filter( 'rank_math/settings/snippet/types', 'add_pro_schema_types' );
		$this->filter( 'rank_math/schema/filter_data', 'update_schema_data' );
		new Taxonomy();
	}
	/**
	 * Update schema data.
	 *
	 * @param array $schemas Schema data.
	 */
	public function update_schema_data( $schemas ) {
		if ( empty( $schemas ) ) {
			return $schemas;
		}
		foreach ( $schemas as $schema_key => $schema ) {
			if ( empty( $schema['review'] ) ) {
				continue;
			}
			if (
				! empty( $schema['review']['positiveNotes'] ) &&
				! empty( $schema['review']['positiveNotes']['itemListElement'] ) &&
				is_string( $schema['review']['positiveNotes']['itemListElement'] )
			) {
				$notes = explode( PHP_EOL, $schema['review']['positiveNotes']['itemListElement'] );
				$schemas[ $schema_key ]['review']['positiveNotes']['itemListElement'] = [];
				foreach ( $notes as $key => $note ) {
					$schemas[ $schema_key ]['review']['positiveNotes']['itemListElement'][] = [
						'@type'    => 'ListItem',
						'position' => $key + 1,
						'name'     => $note,
					];
				}
			}
			if (
				! empty( $schema['review']['negativeNotes'] ) &&
				! empty( $schema['review']['negativeNotes']['itemListElement'] ) &&
				is_string( $schema['review']['negativeNotes']['itemListElement'] )
			) {
				$notes = explode( PHP_EOL, $schema['review']['negativeNotes']['itemListElement'] );
				$schemas[ $schema_key ]['review']['negativeNotes']['itemListElement'] = [];
				foreach ( $notes as $key => $note ) {
					$schemas[ $schema_key ]['review']['negativeNotes']['itemListElement'][] = [
						'@type'    => 'ListItem',
						'position' => $key + 1,
						'name'     => $note,
					];
				}
			}
		}
		return $schemas;
	}
	/**
	 * Add Pro schema types in Schema settings choices array.
	 *
	 * @param array $types Schema types.
	 */
	public function add_pro_schema_types( $types ) {
		$types = array_merge(
			$types,
			[
				'dataset'   => esc_html__( 'DataSet', 'rank-math-pro' ),
				'FactCheck' => esc_html__( 'Fact Check', 'rank-math-pro' ),
				'movie'     => esc_html__( 'Movie', 'rank-math-pro' ),
			]
		);
		unset( $types['off'] );
		ksort( $types, SORT_NATURAL | SORT_FLAG_CASE );
		return [ 'off' => esc_html__( 'None (Click here to set one)', 'rank-math-pro' ) ] + $types;
	}
	/**
	 * Overwrite wplink script file.
	 * Rank Math adds new options in the link popup when editing a post.
	 */
	public function overwrite_wplink() {
		if ( ! Admin_Helper::is_post_edit() || Admin_Helper::is_posts_page() ) {
			return;
		}
		wp_deregister_script( 'rank-math-formats' );
		wp_register_script(
			'rank-math-formats',
			RANK_MATH_PRO_URL . 'assets/admin/js/gutenberg-formats.js',
			[],
			rank_math_pro()->version,
			true
		);
		wp_deregister_script( 'wplink' );
		wp_register_script( 'wplink', RANK_MATH_PRO_URL . 'assets/admin/js/wplink.js', [ 'jquery', 'wpdialogs' ], rank_math_pro()->version, true );
		wp_localize_script(
			'wplink',
			'wpLinkL10n',
			[
				'title'             => esc_html__( 'Insert/edit link', 'rank-math-pro' ),
				'update'            => esc_html__( 'Update', 'rank-math-pro' ),
				'save'              => esc_html__( 'Add Link', 'rank-math-pro' ),
				'noTitle'           => esc_html__( '(no title)', 'rank-math-pro' ),
				'noMatchesFound'    => esc_html__( 'No matches found.', 'rank-math-pro' ),
				'linkSelected'      => esc_html__( 'Link selected.', 'rank-math-pro' ),
				'linkInserted'      => esc_html__( 'Link inserted.', 'rank-math-pro' ),
				'relCheckbox'       => 'rel="nofollow"',
				'sponsoredCheckbox' => 'rel="sponsored"',
				'aboutCheckbox'     => 'about',
				'mentionsCheckbox'  => 'mentions',
				'schemaMarkupLabel' => esc_html__( 'Use in Schema Markup', 'rank-math-pro' ),
				'linkTitle'         => esc_html__( 'Link Title', 'rank-math-pro' ),
			]
		);
	}
	/**
	 * Enqueue Styles and Scripts required for the schema functionality on Gutenberg & Classic editor.
	 *
	 * @return void
	 */
	public function admin_scripts() {
		if ( ! $this->can_enqueue_scripts() ) {
			return;
		}
		$this->localize_data();
		wp_enqueue_style( 'rank-math-schema-pro', RANK_MATH_PRO_URL . 'includes/modules/schema/assets/css/schema.css', [ 'rank-math-schema' ], rank_math_pro()->version );
		wp_enqueue_script(
			'rank-math-pro-schema-filters',
			RANK_MATH_PRO_URL . 'includes/modules/schema/assets/js/schemaFilters.js',
			[
				'wp-plugins',
				'wp-components',
				'wp-hooks',
				'wp-api-fetch',
				'lodash',
			],
			rank_math_pro()->version,
			true
		);
		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false;
		if ( $screen instanceof WP_Screen && 'rank_math_schema' === $screen->post_type ) {
			Helper::add_json( 'isTemplateScreen', true );
			wp_enqueue_script(
				'rank-math-pro-schema',
				rank_math()->plugin_url() . 'includes/modules/schema/assets/js/schema-template.js',
				[
					'clipboard',
					'wp-autop',
					'wp-components',
					'wp-editor',
					'wp-edit-post',
					'wp-element',
					'wp-i18n',
					'wp-plugins',
					'rank-math-analyzer',
				],
				rank_math_pro()->version,
				true
			);
			return;
		}
		wp_enqueue_script( 'rank-math-schema-pro', RANK_MATH_PRO_URL . 'includes/modules/schema/assets/js/schema.js', [ 'rank-math-schema' ], rank_math_pro()->version, true );
	}
	/**
	 * Deregister some scripts.
	 */
	public function deregister_scripts() {
		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false;
		if ( ! $screen instanceof WP_Screen || 'post' !== $screen->base || Helper::is_elementor_editor() || 'rank_math_schema' !== $screen->post_type ) {
			return;
		}
		wp_deregister_script( 'rank-math-editor' );
		wp_deregister_script( 'rank-math-schema' );
		if ( wp_script_is( 'rank-math-pro-editor', 'registered' ) ) {
			wp_deregister_script( 'rank-math-pro-editor' );
		}
	}
	/**
	 * Render app div
	 */
	public function render_div() {
		$screen = get_current_screen();
		if ( 'rank_math_schema' !== $screen->post_type ) {
			return;
		}
		Helper::add_json( 'postType', 'rank_math_schema' );
		wp_nonce_field( 'rank_math_schema_template', 'security' );
		?>