<?php
/**
 * Custom JS
 *
 * @package captain-widgets-kit
 * @since 1.0.0
 */

use Elementor\Controls_Manager;

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

/**
 * Class Captwiki_Custom_Js
 *
 * @package captain-widgets-kit
 */
if ( ! class_exists( 'Captwiki_Custom_Js' ) ) {

	/**
	 * Custom JS
	 *
	 * @package captain-widgets-kit
	 */
	class Captwiki_Custom_Js {

		/**
		 * A reference to an instance of this class.
		 *
		 * @since 1.0.0
		 * @var   object
		 */
		private static $instance = null;

		/**
		 * Returns a singleton instance of the class.
		 *
		 * This method ensures that only one instance of the class is created (singleton pattern).
		 * If an instance doesn't exist, it creates one using the provided shortcodes.
		 *
		 * @since 1.0.0
		 *
		 * @param array $shortcodes Optional. An array of shortcodes to initialize the instance with.
		 * @return self The single instance of the class.
		 */
		public static function get_instance( $shortcodes = array() ) {
			if ( null === self::$instance ) {
				self::$instance = new self( $shortcodes );
			}

			return self::$instance;
		}

		/**
		 * Constructor
		 *
		 * @since 1.0.0
		 */
		public function __construct() {
			add_action( 'elementor/documents/register_controls', array( $this, 'captwiki_section_custom_js' ), 10, 2 );

			if ( ! is_admin() && ! wp_doing_ajax() ) {
				add_action( 'wp_footer', array( $this, 'captwiki_print_custom_js' ), 100 );
			}
		}

		/**
		 * Section Custom JS
		 *
		 * @since 1.0.0
		 * @param object $controls The controls object.
		 * @return void
		 */
		public function captwiki_section_custom_js( $controls ) {

			$controls->start_controls_section(
				'captwiki_ext_section_custom_js',
				array(
					'label' => esc_html__( 'Custom JS', 'captain-widgets-kit' ),
					'tab'   => Controls_Manager::TAB_ADVANCED,
				)
			);

			$controls->add_control(
				'captwiki_custom_js_label',
				array(
					'type' => Controls_Manager::RAW_HTML,
					'raw'  => esc_html__( 'Add your own custom JS here', 'captain-widgets-kit' ),
				)
			);

			$controls->add_control(
				'captwiki_custom_js',
				array(
					'type'       => Controls_Manager::CODE,
					'show_label' => false,
					'language'   => 'javascript',
				)
			);

			if ( ! current_user_can( 'manage_options' ) ) {
				$controls->add_control(
					'captwiki_custom_js_global_warning_text',
					array(
						'type'            => Controls_Manager::RAW_HTML,
						'raw'             => esc_html__( '<strong>Note:</strong> Only users who can manage site options can add or edit JavaScript code from here.', 'captain-widgets-kit' ),
						'content_classes' => 'captwiki-warning',
					)
				);
			}

			$controls->add_control(
				'captwiki_custom_js_usage',
				array(
					'type'            => Controls_Manager::RAW_HTML,
					'raw'             => esc_html__( 'You may use both jQuery selector e.g. $(‘.selector’) or Vanilla JS selector e.g. document.queryselector(‘.selector’)', 'captain-widgets-kit' ),
					'content_classes' => 'captwiki-descriptor',
				)
			);

			$controls->end_controls_section();
		}

		/**
		 * Print Custom JS from Elementor Page Settings in the frontend.
		 *
		 * @since 1.0.0
		 */
		public function captwiki_print_custom_js() {
			if ( is_admin() ) {
				return;
			}

			if ( ! function_exists( 'get_post_meta' ) ) {
				return;
			}

			global $post;
			if ( empty( $post ) || empty( $post->ID ) ) {
				return;
			}

			// Get the custom JS from post meta (Elementor page settings).
			$custom_js = get_post_meta( $post->ID, '_captwiki_custom_js', true );

			// Fallback: also check Elementor's default key if needed.
			if ( empty( $custom_js ) ) {
				$settings = get_post_meta( $post->ID, '_elementor_page_settings', true );
				if ( ! empty( $settings['captwiki_custom_js'] ) ) {
					$custom_js = $settings['captwiki_custom_js'];
				}
			}

			$custom_js = trim( $custom_js );

			if ( ! empty( $custom_js ) ) {
				wp_register_script( 'captwiki-custom-js', false, array(), CAPTWIKI_VERSION, true );
				wp_enqueue_script( 'captwiki-custom-js' );
				wp_add_inline_script( 'captwiki-custom-js', $custom_js );
			}
		}
	}
}

Captwiki_Custom_Js::get_instance();
