<?php
/**
 * DESCRIPTION: Bootstraps the Elementor integration — registers the "Nemtly Booking" widget category
 * DESCRIPTION: and the Event Calendar widget. Self-guarding: its hooks only fire when Elementor is active.
 *
 * @since   PBOOKING_SINCE
 * @package NemtlyBooking\Integrations\Elementor
 */

namespace NemtlyBooking\Integrations\Elementor;

/**
 * Elementor integration bootstrap.
 *
 * @since PBOOKING_SINCE
 */
class Elementor_Integration {

	/**
	 * Register Elementor hooks.
	 *
	 * The `elementor/*` actions only fire when Elementor is loaded, so this method
	 * is safe to call unconditionally — nothing happens if Elementor is inactive.
	 *
	 * @since PBOOKING_SINCE
	 *
	 * @return void
	 */
	public function init() {
		add_action( 'elementor/elements/categories_registered', [ $this, 'register_category' ] );
		add_action( 'elementor/widgets/register', [ $this, 'register_widgets' ] );
		add_action( 'elementor/preview/enqueue_scripts', [ $this, 'enqueue_preview_assets' ] );
	}

	/**
	 * Register the "Nemtly Booking" widget category.
	 *
	 * @since PBOOKING_SINCE
	 *
	 * @param \Elementor\Elements_Manager $elements_manager Elementor elements manager.
	 *
	 * @return void
	 */
	public function register_category( $elements_manager ) {
		$elements_manager->add_category(
			'nemtly-booking',
			[
				'title' => __( 'Nemtly Booking', 'nemtly-booking' ),
				'icon'  => 'eicon-calendar',
			]
		);
	}

	/**
	 * Register the Nemtly Booking widgets.
	 *
	 * @since PBOOKING_SINCE
	 *
	 * @param \Elementor\Widgets_Manager $widgets_manager Elementor widgets manager.
	 *
	 * @return void
	 */
	public function register_widgets( $widgets_manager ) {
		$widgets_manager->register( new Event_Calendar_Widget() );
	}

	/**
	 * Enqueue the editor-only live-preview bundle into the Elementor preview iframe.
	 *
	 * The widget markup is injected via AJAX after page load, so the frontend scanners
	 * never see it. This handler mounts the real Calendar React app on Elementor's
	 * `element_ready` signal. Runs only inside the editor preview.
	 *
	 * @since PBOOKING_SINCE
	 *
	 * @return void
	 */
	public function enqueue_preview_assets() {
		wp_enqueue_style(
			'nemtly-elementor-editor',
			NEMTLY_BOOKING_URL . 'assets/css/elementor-editor.css',
			[],
			NEMTLY_BOOKING_VERSION
		);

		$asset_path = NEMTLY_BOOKING_PATH . 'assets/js/elementor-editor.asset.php';
		$asset      = file_exists( $asset_path ) ? require $asset_path : [
			'dependencies' => [ 'wp-element', 'wp-api-fetch', 'wp-i18n' ],
			'version'      => NEMTLY_BOOKING_VERSION,
		];

		wp_enqueue_script(
			'nemtly-elementor-editor',
			NEMTLY_BOOKING_URL . 'assets/js/elementor-editor.js',
			$asset['dependencies'],
			$asset['version'],
			true
		);

		wp_localize_script( 'nemtly-elementor-editor', 'nemtlyCalendar', Abstract_Widget::calendar_localize_data() );

		wp_add_inline_script(
			'nemtly-elementor-editor',
			sprintf(
				'wp.apiFetch.use( wp.apiFetch.createNonceMiddleware( %s ) );',
				wp_json_encode( wp_create_nonce( 'wp_rest' ) )
			),
			'before'
		);

		wp_set_script_translations( 'nemtly-elementor-editor', 'nemtly-booking' );
	}
}
