<?php

// =====================================================
// Admin & editor asset enqueues for REEID Translate.
// Bulletproof asset loading from plugin ROOT, not /includes/
// =====================================================

defined('ABSPATH') || exit;


/* =======================================================
   Shared frontend/editor localization asset
   ======================================================= */

if ( ! function_exists( 'reeid_register_localize_asset' ) ) {
    function reeid_register_localize_asset() {
        global $reeid_base_path, $reeid_base_url;

        if ( empty( $reeid_base_path ) || empty( $reeid_base_url ) ) {
            return;
        }

        $handle = 'reeid-translate-localize';
        $src    = $reeid_base_url . '/assets/js/reeid-localize.js';
        $path   = $reeid_base_path . '/assets/js/reeid-localize.js';

        $ver = null;
        if ( file_exists( $path ) ) {
            $ver = (string) filemtime( $path );
        } elseif ( defined( 'REEID_PLUGIN_VERSION' ) ) {
            $ver = REEID_PLUGIN_VERSION;
        }

        if ( ! wp_script_is( $handle, 'registered' ) ) {
            wp_register_script( $handle, $src, array( 'jquery' ), $ver, true );
        }

        wp_enqueue_script( $handle );

        $localized = array(
            'nonce'    => wp_create_nonce( 'reeid_translate_nonce_action' ),
            'ajax_url' => admin_url( 'admin-ajax.php' ),
            'tones'    => function_exists( 'reeid_get_available_tones' )
                ? reeid_get_available_tones()
                : array(),
        );

        wp_localize_script( $handle, 'REEID_TRANSLATE', $localized );

        wp_add_inline_script(
            $handle,
            'if(!window.REEID_TRANSLATE){window.REEID_TRANSLATE=' .
            wp_json_encode( $localized, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) .
            ';}',
            'after'
        );
    }

    add_action( 'wp_enqueue_scripts', 'reeid_register_localize_asset', 20 );
}


// Point directly to MAIN PLUGIN FILE → never breaks
$reeid_main_file  = dirname( __DIR__ ) . '/reeid-translate.php';
$reeid_base_path  = dirname( __DIR__ ); // /wp-content/plugins/reeid-translate
$reeid_base_url   = plugins_url( '', $reeid_main_file ); // correct root URL


add_action( 'admin_enqueue_scripts', 'reeid_enqueue_admin_and_metabox_assets' );
function reeid_enqueue_admin_and_metabox_assets( $hook ) {
    global $reeid_base_path, $reeid_base_url;

  /* =====================================================
   A) Settings Page Assets
   ===================================================== */
if (
    'settings_page_reeid-translate-settings' === $hook
    || 'toplevel_page_reeid-translate' === $hook
    || 'toplevel_page_reeid-translate-settings' === $hook
    || false !== strpos( (string) $hook, 'reeid-translate' )
) {
        wp_enqueue_style(
            'reeid-admin-styles',
            $reeid_base_url . '/assets/css/admin-styles.css',
            array(),
            '1.0'
        );

        $admin_js = $reeid_base_path . '/assets/js/admin-settings.js';
        if ( file_exists( $admin_js ) ) {

            wp_enqueue_script(
                'reeid-admin-settings',
                $reeid_base_url . '/assets/js/admin-settings.js',
                array( 'jquery' ),
                (string) filemtime( $admin_js ),
                true
            );

            wp_localize_script(
                'reeid-admin-settings',
                'REEID_TRANSLATE',
                array(
                    'ajaxurl'        => esc_url_raw( admin_url( 'admin-ajax.php' ) ),
                    'nonce'          => wp_create_nonce( 'reeid_translate_nonce_action' ),
                   
                )
            );
        }
    }

    /* =====================================================
       B) Post Edit Screens (Metabox)
       ===================================================== */
    if ( in_array( $hook, array( 'post.php', 'post-new.php' ), true ) ) {

        /* --- CSS --- */
        $mb_css = $reeid_base_path . '/assets/css/meta-box.css';
        if ( file_exists( $mb_css ) ) {
            wp_enqueue_style(
                'reeid-meta-box-styles',
                $reeid_base_url . '/assets/css/meta-box.css',
                array(),
                (string) filemtime( $mb_css )
            );
        }

        /* --- JS --- */
        $mb_js = $reeid_base_path . '/assets/js/translation-meta-box.js';
        if ( file_exists( $mb_js ) ) {

            wp_enqueue_script(
                'reeid-translation-meta-box',
                $reeid_base_url . '/assets/js/translation-meta-box.js',
                array( 'jquery' ),
                (string) filemtime( $mb_js ),
                true
            );

            /* --- Localize meta-box data --- */
                $lang_names = function_exists( 'reeid_get_allowed_languages' )
                ? (array) reeid_get_allowed_languages()
                : array();


            wp_localize_script(
                'reeid-translation-meta-box',
                'reeidData',
                array(
                    'ajaxurl'   => esc_url_raw( admin_url( 'admin-ajax.php' ) ),
                    'nonce'     => wp_create_nonce( 'reeid_translate_nonce_action' ),
                     'langNames' => $lang_names,
                )
            );

            /* =====================================================
               WooCommerce product editor enhancements
               ===================================================== */
            $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;

            if ( $screen && ! empty( $screen->post_type ) && $screen->post_type === 'product' ) {

                wp_enqueue_style( 'woocommerce_admin_styles' );
                wp_enqueue_script( 'wc-enhanced-select' );
                                wp_enqueue_style(
                    'reeid-admin-styles',
                    $reeid_base_url . '/assets/css/admin-styles.css',
                    array(),
                    file_exists( $reeid_base_path . '/assets/css/admin-styles.css' )
                        ? (string) filemtime( $reeid_base_path . '/assets/css/admin-styles.css' )
                        : '1.0'
                );

                $reeid_admin_settings_js = $reeid_base_path . '/assets/js/admin-settings.js';

                if ( file_exists( $reeid_admin_settings_js ) ) {
                    wp_enqueue_script(
                        'reeid-admin-settings',
                        $reeid_base_url . '/assets/js/admin-settings.js',
                        array( 'jquery' ),
                        (string) filemtime( $reeid_admin_settings_js ),
                        true
                    );
                }



                $reeid_wc_tr_css = '
                    .reeid-wc-tr-toolbar{display:flex;gap:8px;align-items:center;margin:6px 0 10px;}
                    .reeid-wc-tr-select{min-width:260px;max-width:420px;}
                    .reeid-wc-tr-toolbar .button{height:32px}
                ';
                wp_add_inline_style( 'woocommerce_admin_styles', $reeid_wc_tr_css );
            }
        }
    }
}


