<?php
/**
 * Wemeet_Gutenberg Class
 *
 * This class manages the frontend functionality for the Meeting Hub plugin.
 *
 * @package SOVLIX\WEMEET
 */

namespace SOVLIX\WEMEET;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'Wemeet_Gutenberg' ) ) {
	/**
	 * Wemeet_Gutenberg Class
	 *
	 * Handles frontend-related tasks and functionality for the Meeting Hub plugin.
	 *
	 * @since 1.0.0
	 */
	class Wemeet_Gutenberg {

		/**
		 * Wemeet_Gutenberg constructor.
		 *
		 * Initializes the frontend class.
		 */
		public function __construct() {
			add_action( 'enqueue_block_editor_assets', array( $this, 'register_block_assets' ) );
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_assets' ) );
		}

		/**
		 * Register block assets.
		 *
		 * Enqueues block editor script and styles.
		 *
		 * @since 1.0.0
		 */
		public function register_block_assets() {
			// Enqueue block editor script.
			wp_enqueue_script(
				'wemeet-gutenberg-editor',
				WEMEET_ASSETS . '/dist/block.build.js',
				array( 'wp-blocks', 'wp-components', 'wp-editor' ),
				filemtime( WEMEET_PATH . '/assets/dist/block.build.js' ),
				true
			);

			$wemeet_jitsi_settings = get_option( 'wemeet_jitsi_settings' );

			// Localize script with zoom_users data.
			wp_localize_script(
				'wemeet-gutenberg-editor',
				'wemeetGutenbergData',
				array(
					'ajax_url' => admin_url( 'admin-ajax.php' ),
					'nonce'    => wp_create_nonce( 'wemeet_admin_nonce' ),
				)
			);

			wp_enqueue_style(
				'wemeet-gutenberg-editor-style',
				WEMEET_ASSETS . '/dist/block.build.css',
				array( 'wp-edit-blocks' ),
				filemtime( WEMEET_PATH . '/assets/dist/block.build.css' ),
				null
			);
		}

		/**
		 * Enqueue frontend assets.
		 *
		 * Enqueues frontend styles.
		 *
		 * @since 1.0.0
		 */
		public function enqueue_frontend_assets() {
			wp_enqueue_style(
				'wemeet-frontend-style',
				WEMEET_ASSETS . '/dist/block.build.css',
				array(),
				filemtime( WEMEET_PATH . '/assets/dist/block.build.css' )
			);
		}
	}
}
