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

// Registers the Elementor equivalent of the gift card form (see
// includes/elementor/*.php) - this hook simply never fires if Elementor
// isn't active, so it's safe to always add; the widget class files (which
// reference \Elementor\Widget_Base / Widget_Nested_Base) are only required
// from inside this callback, never at plugin-boot time.
//
// Gated at registration (not per-control like the other Pro features) -
// Elementor's widget panel has no "disabled with upsell tooltip" state for
// an entire widget the way a <select> can disable one <option>, so a widget
// that's draggable but silently does nothing on drop would be worse UX than
// it simply not appearing. See the admin_notices hook below for the upsell
// path instead.
add_action( 'elementor/widgets/register', function( $widgets_manager ) {
    if ( ! cgc_is_pro_active() ) {
        return;
    }

    require_once CGC_PLUGIN_PATH . 'includes/elementor/class-cgc-elementor-design-picker.php';
    require_once CGC_PLUGIN_PATH . 'includes/elementor/class-cgc-elementor-field.php';
    require_once CGC_PLUGIN_PATH . 'includes/elementor/class-cgc-elementor-submit-button.php';
    require_once CGC_PLUGIN_PATH . 'includes/elementor/class-cgc-elementor-gift-card-form.php';

    $widgets_manager->register( new CGC_Elementor_Design_Picker() );
    $widgets_manager->register( new CGC_Elementor_Field() );
    $widgets_manager->register( new CGC_Elementor_Submit_Button() );
    $widgets_manager->register( new CGC_Elementor_Gift_Card_Form() );
} );

// Elementor's Nested Elements system needs a companion editor-side JS
// registration (elementor.elementsManager.registerElementType(), see
// app/src/elementor-editor/) for the parent widget to actually mount/preview
// its children in the editor canvas at all - the PHP widget registration
// above is not sufficient on its own.
//
// Hook + dependency confirmed against Elementor core's own first-party
// Nested Accordion module (modules/nested-accordion/module.php), the exact
// same situation as ours (a nested element with its own editor JS view):
// it hooks elementor/editor/before_enqueue_scripts and depends on
// 'nested-elements' specifically (Elementor's own nested-elements
// component script - our view.js calls
// $e.components.get('nested-elements'), so that component must actually be
// registered first) - NOT the more generic 'elementor-editor' handle this
// used to (incorrectly) depend on, which doesn't guarantee that ordering.
add_action( 'elementor/editor/before_enqueue_scripts', function() {
    if ( ! cgc_is_pro_active() ) {
        return;
    }

    $asset_file = include( CGC_PLUGIN_PATH . 'build/elementor-editor/index.asset.php' );
    if ( ! is_array( $asset_file ) ) {
        return;
    }

    wp_enqueue_script(
        'cgc-elementor-editor',
        CGC_PLUGIN_URL . 'build/elementor-editor/index.js',
        array_merge( $asset_file['dependencies'], array( 'nested-elements' ) ),
        CGC_PLUGIN_VERSION,
        true
    );
} );

// Elementor often renders this form's widgets via an AJAX partial-render
// (dragging a widget in fresh, or live-editing a control in the panel)
// rather than a full iframe reload - wp_enqueue_style()/_script() calls
// made from inside a widget's render() at that point can never reach the
// preview iframe's already-sent <head>, which is what made the form look
// unstyled in the editor. Enqueuing the same handles unconditionally here,
// early on wp_enqueue_scripts (well before wp_head() prints), guarantees
// they're already queued by the time any render happens, regardless of
// whether it's a full load or an AJAX partial one.
add_action( 'wp_enqueue_scripts', function() {
    if ( ! cgc_is_pro_active() ) {
        return;
    }

    if ( ! did_action( 'elementor/loaded' ) || ! class_exists( '\Elementor\Plugin' ) ) {
        return;
    }

    $is_edit_mode = \Elementor\Plugin::$instance->editor->is_edit_mode();
    $is_preview_mode = \Elementor\Plugin::$instance->preview->is_preview_mode();

    if ( ! $is_edit_mode && ! $is_preview_mode ) {
        return;
    }

    global $cgc_blocks;
    if ( $cgc_blocks ) {
        $cgc_blocks->enqueue_form_frontend_assets();
    }
} );

// Lets an admin browsing Elementor's own admin screens know the Gift Card
// Form widget exists but requires GiftCard Studio Pro, since it otherwise just
// silently doesn't appear with no explanation.
add_action( 'admin_notices', function() {
    if ( cgc_is_pro_active() || ! current_user_can( 'manage_options' ) ) {
        return;
    }

    $screen = get_current_screen();
    if ( ! $screen || false === strpos( (string) $screen->id, 'elementor' ) ) {
        return;
    }

    echo '<div class="notice notice-info is-dismissible"><p>' .
        esc_html__( 'GiftCard Studio: the Gift Card Form widget for Elementor is available on GiftCard Studio Pro.', 'gift-card-studio' ) .
        ' <a href="' . esc_url( CGC_UPGRADE_URL ) . '" target="_blank" rel="noopener noreferrer">' .
        esc_html__( 'Upgrade now', 'gift-card-studio' ) . '</a></p></div>';
} );
