<?php
/**
 * Changeloger Block Registration
 *
 * @package Changeloger
 */

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

/**
 * Class Changeloger_Block_Register
 */
class Changeloger_Block_Register {

	/**
	 * Initialize blocks
	 *
	 * @return void
	 */
	public function changeloger_create_block_init(): void {
		register_block_type(
			__DIR__ . '/../build/changeloger',
			[ 'render_callback' => [ $this, 'changeloger_render' ] ]
		);

		register_block_type(
			__DIR__ . '/../build/tabbed-changeloger',
			[ 'render_callback' => [ $this, 'tabbed_changeloger_render' ] ]
		);
	}

	/**
	 * Render callback for Changeloger block
	 *
	 * @param array    $attributes Block attributes.
	 * @param string   $content    Block content.
	 * @param WP_Block $instance   Block instance.
	 * @return string
	 */
	public function changeloger_render( array $attributes, string $content, WP_Block $instance ): string {
		// Register style using constant if available, otherwise fallback to plugins_url.
		$url = defined( 'CHANGELOGER_URL' ) ? CHANGELOGER_URL . 'build/changeloger/style-index.css' : plugins_url( 'build/changeloger/style-index.css', dirname( __DIR__ ) . '/changeloger.php' );
		$ver = defined( 'CHANGELOGER_VERSION' ) ? CHANGELOGER_VERSION : false;

		wp_register_style( 'changeloger', $url, [], $ver );

		if ( cha_is_premium() &&
			(
				! empty( $attributes['enablePagination'] ) ||
				! empty( $attributes['enableFilter'] ) ||
				! empty( $attributes['enableSearch'] )
			)
		) {
			wp_enqueue_script( 'changeloger-filter' );
		}

		if ( ! empty( $attributes['enableSearch'] ) && cha_is_premium() ) {
			wp_enqueue_script( 'mark' );
		}

		if ( ! empty( $attributes['enableSubscription'] ) && cha_is_premium() ) {
			wp_enqueue_script( 'cha-subscription' );
			wp_enqueue_style( 'cha-subscription' );
		}

		// Localize script
		if ( class_exists( 'Changeloger_Block_Assets' ) ) {
			Changeloger_Block_Assets::localize_script( 'changeloger-frontend' );
		}

		return $content;
	}

	/**
	 * Render callback for Tabbed Changeloger block
	 *
	 * @param array    $attributes Block attributes.
	 * @param string   $content    Block content.
	 * @param WP_Block $instance   Block instance.
	 * @return string
	 */
	public function tabbed_changeloger_render( array $attributes, string $content, WP_Block $instance ): string {
		$url = defined( 'CHANGELOGER_URL' ) ? CHANGELOGER_URL . 'build/tabbed-changeloger/style-index.css' : plugins_url( 'build/tabbed-changeloger/style-index.css', dirname( __DIR__ ) . '/changeloger.php' );
		$ver = defined( 'CHANGELOGER_VERSION' ) ? CHANGELOGER_VERSION : false;

		wp_register_style( 'tabbed-changeloger', $url, [], $ver );
		wp_enqueue_style( 'tabbed-changeloger' );

		// Enqueue frontend JavaScript for tab functionality.
		wp_enqueue_script( 'tabbed-changeloger-frontend' );

		// Localize script
		if ( class_exists( 'Changeloger_Block_Assets' ) ) {
			Changeloger_Block_Assets::localize_script( 'tabbed-changeloger-frontend' );
		}

		return $content;
	}
}
