<?php
/**
 * Publicize_Assets.
 *
 * @package automattic/jetpack-publicize
 */

namespace Automattic\Jetpack\Publicize;

use Automattic\Jetpack\Assets;
use Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills;

/**
 * Publicize_Assets class.
 */
class Publicize_Assets {

	/**
	 * Initialize the class.
	 */
	public static function configure() {
		Publicize_Script_Data::configure();

		add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_block_editor_scripts' ), 15 );
	}

	/**
	 * Whether to enqueue the block editor scripts.
	 *
	 * @return boolean True if the criteria are met.
	 */
	public static function should_enqueue_block_editor_scripts() {

		$post_type = get_post_type();

		if ( empty( $post_type ) || ! post_type_supports( $post_type, 'publicize' ) ) {
			return false;
		}

		/** This filter is documented in projects/packages/publicize/src/class-publicize-base.php */
		$capability = apply_filters( 'jetpack_publicize_capability', 'publish_posts' );

		return current_user_can( $capability );
	}

	/**
	 * Enqueue block editor scripts and styles.
	 */
	public static function enqueue_block_editor_scripts() {
		if ( ! self::should_enqueue_block_editor_scripts() ) {
			return;
		}

		// We don't want to render the Social UI in Jetpack sidebar
		// if Jetpack is old, which has it bundled.
		if ( defined( 'JETPACK__VERSION' ) && ( version_compare( (string) JETPACK__VERSION, '14.5-a.1', '<' ) ) ) {
			return;
		}

		$script_to_load = class_exists( 'Jetpack' ) ? 'block-editor-jetpack' : 'block-editor-social';

		self::register_wp_build_polyfills();

		// Dequeue the old Social assets.
		wp_dequeue_script( 'jetpack-social-editor' );
		wp_dequeue_style( 'jetpack-social-editor' );

		Assets::register_script(
			'jetpack-social-editor',
			sprintf( '../build/%s.js', $script_to_load ),
			__FILE__,
			array(
				'in_footer'  => true,
				'textdomain' => 'jetpack-publicize-pkg',
				'enqueue'    => true,
			)
		);
	}

	/**
	 * Register polyfills for the wp-theme / wp-private-apis / wp-rich-text handles the
	 * Social bundles depend on but WP < 7.0 does not ship (or ships with an incomplete
	 * allowlist).
	 *
	 * `wp-rich-text` is needed because the editor bundles reach `@wordpress/dataviews`
	 * (via `@wordpress/ui`), whose dataform controls unlock rich-text's `privateApis` at
	 * module scope. WP 6.9 exports none, so without the polyfill the bundle throws
	 * "Cannot unlock an undefined object" before the sidebar renders.
	 *
	 * Only the handles Social actually uses are requested, to keep the polyfill's
	 * force-replacement off any handle we don't need — `wp-notices` in particular, which
	 * Core registers adequately for these bundles.
	 */
	public static function register_wp_build_polyfills() {
		if ( ! class_exists( WP_Build_Polyfills::class ) ) {
			return;
		}

		WP_Build_Polyfills::register(
			'jetpack-social',
			array( 'wp-theme', 'wp-private-apis', 'wp-rich-text' )
		);
	}
}
