<?php
/**
 * Widget registration - Auto-generated by build process.
 * Registers all widget script modules discovered from widgets/.
 * Do not edit this file manually.
 *
 * @package jpa
 */

/**
 * Register widget script modules.
 *
 * Registers each widget's render and metadata as script modules via
 * wp_register_script_module.
 *
 * Module IDs follow core's route pattern and use the widget directory name:
 *   jetpack-premium-analytics/widgets/{widget-dir-name}/render
 *   jetpack-premium-analytics/widgets/{widget-dir-name}/widget
 */
function jpa_register_widget_modules() {
	$widgets = jpa_get_registered_widget_modules();
	if ( empty( $widgets ) ) {
		return;
	}

	$build_constants = require __DIR__ . '/constants.php';
	$extension       = '.min.js';

	foreach ( $widgets as $widget ) {
		$widget_name = $widget['dir_name'];

		// Register render script module if built.
		if ( ! empty( $widget['render_module'] ) ) {
			$render_asset_path = __DIR__ . "/widgets/{$widget_name}/render.min.asset.php";
			if ( file_exists( $render_asset_path ) ) {
				$render_asset = require $render_asset_path;
				// Deregister first to override any previously registered version
				// (e.g., Core's default modules when running as a plugin).
				wp_deregister_script_module( $widget['render_module'] );
				wp_register_script_module(
					$widget['render_module'],
					$build_constants['build_url'] . 'widgets/' . $widget_name . '/render' . $extension,
					$render_asset['module_dependencies'] ?? array(),
					$render_asset['version'] ?? false
				);
			}
		}

		// Register widget metadata script module if built.
		if ( ! empty( $widget['widget_module'] ) ) {
			$widget_asset_path = __DIR__ . "/widgets/{$widget_name}/widget.min.asset.php";
			if ( file_exists( $widget_asset_path ) ) {
				$widget_asset = require $widget_asset_path;
				// Deregister first to override any previously registered version
				// (e.g., Core's default modules when running as a plugin).
				wp_deregister_script_module( $widget['widget_module'] );
				wp_register_script_module(
					$widget['widget_module'],
					$build_constants['build_url'] . 'widgets/' . $widget_name . '/widget' . $extension,
					$widget_asset['module_dependencies'] ?? array(),
					$widget_asset['version'] ?? false
				);
			}
		}
	}
}

// Register immediately if init already fired, otherwise defer.
if ( did_action( 'init' ) ) {
	jpa_register_widget_modules();
} else {
	add_action( 'init', 'jpa_register_widget_modules' );
}

/**
 * Returns the widgets discovered by the build pipeline, with their
 * script-module handles resolved.
 *
 * Reads `build/widgets/registry.php` once and memoizes the result for the
 * remainder of the request. Each entry is augmented with `render_module`
 * and `widget_module` (the script-module handles), kept null when the
 * corresponding source file was not present at build time. Consumers
 * should treat the array as read-only; authoring of widget types is
 * build-driven via the `widgets/` folder.
 *
 * @return array<int, array<string, mixed>> List of widgets with resolved
 *                                          module handles.
 */
function jpa_get_registered_widget_modules() {
	static $widgets = null;

	if ( null === $widgets ) {
		$registry_file = __DIR__ . '/widgets/registry.php';
		$raw           = file_exists( $registry_file ) ? require $registry_file : array();
		if ( ! is_array( $raw ) ) {
			$raw = array();
		}
		$widgets = array_map(
			static function ( $widget ) {
				$widget['render_module'] = ! empty( $widget['has_render'] )
					? 'jetpack-premium-analytics/widgets/' . $widget['dir_name'] . '/render'
					: null;
				$widget['widget_module'] = ! empty( $widget['has_widget'] )
					? 'jetpack-premium-analytics/widgets/' . $widget['dir_name'] . '/widget'
					: null;
				return $widget;
			},
			$raw
		);
	}

	return $widgets;
}
