<?php

namespace WPVOID\StockBeacon\Admin;

if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Shared React + Tailwind admin bundle (dashboard and WooCommerce product editor).
 */
class AdminBundle {
    /**
     * Enqueue built admin JS/CSS and attach localized config.
     *
     * @param array<string,mixed> $localize              Passed to wp_localize_script.
     * @param string              $object_name           Global JS variable name.
     * @param bool                $include_frontend_css  Whether to load storefront CSS (dashboard preview only).
     */
    public static function enqueue_react( array $localize, string $object_name = 'stockbeaconAdmin', bool $include_frontend_css = true ) : void {
        $bundle = 'index';
        $asset_path = STOCKBEACON_PATH . 'build/' . $bundle . '.asset.php';
        $asset = ( file_exists( $asset_path ) ? require $asset_path : array(
            'dependencies' => array(
                'react',
                'react-dom',
                'react-jsx-runtime',
                'wp-i18n'
            ),
            'version'      => STOCKBEACON_VERSION,
        ) );
        $dependencies = array_values( array_unique( array_merge( $asset['dependencies'], array('wp-i18n') ) ) );
        wp_enqueue_script(
            'stockbeacon-admin',
            STOCKBEACON_URL . 'build/' . $bundle . '.js',
            $dependencies,
            $asset['version'],
            true
        );
        wp_set_script_translations( 'stockbeacon-admin', 'stockbeacon-product-waitlists-for-woocommerce', STOCKBEACON_PATH . 'languages' );
        if ( $include_frontend_css ) {
            wp_enqueue_style(
                'stockbeacon-frontend-preview',
                STOCKBEACON_URL . 'assets/css/frontend.css',
                array(),
                STOCKBEACON_VERSION
            );
        }
        if ( file_exists( STOCKBEACON_PATH . 'build/' . $bundle . '.css' ) ) {
            wp_enqueue_style(
                'stockbeacon-admin',
                STOCKBEACON_URL . 'build/' . $bundle . '.css',
                array(),
                $asset['version']
            );
        }
        wp_localize_script( 'stockbeacon-admin', $object_name, $localize );
    }

}
