<?php

namespace PostenBringCheckout\checkout\block;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface;

/**
 * Cart block integration interface
 *
 * Class responsible for initializing the checkout block integration interface
 *
 * @since 0.0.1
 */
class CartBlock implements IntegrationInterface {

	/**
	 * Integration interface name
	 *
	 * @return string Integration interface name
	 * @since 0.0.1
	 */
	public function get_name(): string {
		return 'posten-bring-checkout-cart';
	}

	/**
	 * Initialize integration
	 *
	 * This function will initialize all necessary block stuff and enqueue scripts
	 *
	 * @since 0.0.1
	 */
	public function initialize(): void {
		$this->register_frontend_scripts();
		$this->register_editor_scripts();
	}

	/**
	 * Get scripts to enqueue
	 *
	 * This function will return the assets to enqueue on the frontend
	 *
	 * @return string[] Array of script handles
	 * @since 0.0.1
	 */
	public function get_script_handles(): array {
		return array( 'posten-bring-checkout-cart-block-frontend' );
	}

	/**
	 * Get editor scripts to enqueue
	 *
	 * Returns an array of script handles to enqueue in the editor context.
	 *
	 * @return string[] Array of script handles
	 * @since 0.0.1
	 */
	public function get_editor_script_handles(): array {
		return array( 'posten-bring-checkout-cart-block-editor' );
	}

	/**
	 * Get script data
	 *
	 * Returns an array of key, value pairs of data made available to the block on the client side.
	 *
	 * @return array Array of script handles
	 */
	public function get_script_data(): array {
		$data = array(
			'active' => true,
		);

		return $data;
	}

	/**
	 * Register block frontend scripts
	 *
	 * This function will initialize all necessary block stuff and enqueue scripts
	 *
	 * @since 0.0.1
	 */
	private function register_frontend_scripts(): void {
		$script_url        = plugin_dir_url( dirname( __DIR__ ) ) . 'build/posten-bring-checkout-cart-block-frontend.js';
		$script_asset_path = plugin_dir_path( dirname( __DIR__ ) ) . 'build/posten-bring-checkout-cart-block-frontend.asset.php';
		$script_asset      = file_exists( $script_asset_path )
			? require $script_asset_path
			: array(
				'dependencies' => array(),
				'version'      => $this->get_file_version( $script_asset_path ),
			);

		wp_register_script(
			'posten-bring-checkout-cart-block-frontend',
			$script_url,
			$script_asset['dependencies'],
			$script_asset['version'],
			true
		);

		wp_set_script_translations(
			'posten-bring-checkout-cart-block-frontend',
			'posten-bring-checkout',
			plugin_dir_path( dirname( __DIR__ ) ) . 'languages'
		);
	}

	/**
	 * Register block editor scripts
	 *
	 * This function will initialize all necessary block stuff and enqueue scripts
	 *
	 * @since 0.0.1
	 */
	private function register_editor_scripts(): void {
		$script_url        = plugin_dir_url( dirname( __DIR__ ) ) . 'build/posten-bring-checkout-cart-block.js';
		$script_asset_path = plugin_dir_path( dirname( __DIR__ ) ) . 'build/posten-bring-checkout-cart-block.asset.php';
		$script_asset      = file_exists( $script_asset_path )
			? require $script_asset_path
			: array(
				'dependencies' => array(),
				'version'      => $this->get_file_version( $script_asset_path ),
			);

		wp_register_script(
			'posten-bring-checkout-cart-block-editor',
			$script_url,
			$script_asset['dependencies'],
			$script_asset['version'],
			true
		);

		wp_set_script_translations(
			'posten-bring-checkout-cart-block-editor',
			'posten-bring-checkout',
			plugin_dir_path( dirname( __DIR__ ) ) . 'languages'
		);
	}

	/**
	 * Get the file modified time as a cache buster if we're in dev mode.
	 *
	 * @param string $file Local path to the file.
	 * @return string The cache buster value to use for the given file.
	 */
	protected function get_file_version( string $file ): string {
		if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $file ) ) {
			return filemtime( $file );
		}
		return POSTEN_BRING_CHECKOUT_VERSION;
	}
}
