<?php

namespace WPML\LIB\WP\App;

use WPML\FP\Fns;
use WPML\FP\Obj;
use WPML\LIB\WP\Nonce;
use WPML\LIB\WP\WordPress;

class Resources {

	public static function enqueue( $app, $pluginBaseUrl, $pluginBasePath, $version, $domain = null, $localize = null ) {
		static::enqueueWithDeps( $app, $pluginBaseUrl, $pluginBasePath, $version, $domain, $localize, [] );
	}

	public static function enqueueWithDeps( $app, $pluginBaseUrl, $pluginBasePath, $version, $domain = null, $localize = null, $dependencies = [] ) {
		$handle = sprintf(
			'wpml-%s-ui',
			str_replace( '/', '_', $app )
		);

		wp_register_script(
			$handle,
			"$pluginBaseUrl/dist/js/$app/app.js",
			$dependencies,
			$version
		);

		if ( $localize ) {
			if ( isset( $localize['data']['endpoint'] ) ) {
				$localize['data']['nonce'] = Nonce::create( $localize['data']['endpoint'] );
			}
			if ( isset( $localize['data']['endpoints'] ) ) {
				$localize = Obj::over(
					Obj::lensPath( [ 'data', 'endpoints' ] ),
					Fns::map( function ( $endpoint ) {
						return [
							'endpoint' => $endpoint,
							'nonce'    => Nonce::create( $endpoint )
						];
					} ),
					$localize
				);
			}
			wp_localize_script( $handle, $localize['name'], $localize['data'] );
		}

		wp_enqueue_script( $handle );

		if ( file_exists( "$pluginBasePath/dist/css/$app/styles.css" ) ) {
			wp_enqueue_style(
				$handle,
				"$pluginBaseUrl/dist/css/$app/styles.css",
				[],
				$version
			);
		}

		if ( $domain && WordPress::versionCompare( '>=', '5.0.0' ) ) {
			$rootPath = $domain === 'wpml-translation-management' && defined( 'WPML_PLUGIN_PATH' )
				? WPML_PLUGIN_PATH
				: $pluginBasePath;

			wp_set_script_translations( $handle, $domain, "$rootPath/locale/jed" );
		}
	}
}
