<?php

namespace SugarCalendarBookings\Admin;

/**
 * Admin assets.
 *
 * @since 1.0.0
 */
class Assets {

	/**
	 * Enqueue React dev runtime.
	 *
	 * @since 1.0.0
	 */
	public function enqueue_dev_runtime() {

		$manifest = $this->get_asset_manifest( 'runtime' );

		if ( ! $manifest ) {
			return;
		}

		wp_enqueue_script(
			$manifest['handle'],
			$this->get_src( 'runtime.js' ),
			$manifest['dependencies'],
			$manifest['version'],
			true
		);
	}

	/**
	 * Get the asset manifest.
	 *
	 * @since 1.0.0
	 *
	 * @param string $asset_path The asset path.
	 *
	 * @return array|false
	 */
	public function get_asset_manifest( $asset_path ) {

		$manifest_path = SCBOOKINGS_PLUGIN_PATH . 'assets/dist/' . $asset_path . '.asset.php';

		if ( ! file_exists( $manifest_path ) ) {
			return false;
		}

		return require $manifest_path;
	}

	/**
	 * Get the asset URL.
	 *
	 * @since 1.0.0
	 *
	 * @param string $path The asset path.
	 *
	 * @return string
	 */
	public function get_src( $path ) {

		return SCBOOKINGS_PLUGIN_URL . 'assets/dist/' . $path;
	}

	/**
	 * Check if in dev mode enabled and dev server is running.
	 *
	 * @since 1.0.0
	 *
	 * @return bool
	 */
	public function is_dev() {

		return defined( 'SCBOOKINGS_DEV_MODE' ) &&
					SCBOOKINGS_DEV_MODE &&
					defined( 'SCBOOKINGS_DEV_SERVER_RUNNING' ) &&
					SCBOOKINGS_DEV_SERVER_RUNNING === true;
	}
}
