<?php
/**
 * Generated browser asset metadata.
 *
 * @package VoidPulse
 */

namespace VoidPulse\Shared;

defined( 'ABSPATH' ) || exit;

final class Asset {
	/**
	 * Read one @wordpress/scripts dependency file.
	 *
	 * @param string $entry Closed build entry name.
	 * @return array{dependencies:array<int,string>,version:string}
	 */
	public static function metadata( $entry ) {
		$fallback = array(
			'dependencies' => array(),
			'version'      => VOIDPULSE_VERSION,
		);
		if ( ! is_string( $entry ) || ! preg_match( '/^[a-z0-9_-]+$/', $entry ) ) {
			return $fallback;
		}
		$file = VOIDPULSE_PATH . 'public/' . $entry . '.asset.php';
		if ( ! is_readable( $file ) ) {
			return $fallback;
		}
		$metadata = require $file;
		if ( ! is_array( $metadata ) ) {
			return $fallback;
		}
		$dependencies = isset( $metadata['dependencies'] ) && is_array( $metadata['dependencies'] )
			? array_values( array_filter( array_map( 'sanitize_key', $metadata['dependencies'] ) ) )
			: array();
		$version = isset( $metadata['version'] ) && is_scalar( $metadata['version'] )
			? sanitize_text_field( (string) $metadata['version'] )
			: VOIDPULSE_VERSION;
		return array(
			'dependencies' => $dependencies,
			'version'      => $version ?: VOIDPULSE_VERSION,
		);
	}
}
