<?php

use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface;

class Dobare_Integration implements IntegrationInterface
{

	/**
	 * @inheritDoc
	 */
	public function get_name(): string
	{
		return 'dobare';
	}

	/**
	 * @inheritDoc
	 */
	public function initialize()
	{
		$script_path = '/build/index.js';
		$style_path  = '/build/index.css';

		$script_url = plugins_url($script_path, DOBARE_MAIN_PLUGIN_FILE);
		$style_url  = plugins_url($style_path, DOBARE_MAIN_PLUGIN_FILE);

		$script_asset_path = dirname(DOBARE_MAIN_PLUGIN_FILE) . '/build/index.asset.php';
		$script_asset      = file_exists($script_asset_path)
			? require $script_asset_path
			: array(
				'dependencies' => array(),
				'version'      => $this->get_file_version($script_path),
			);

		wp_enqueue_style(
			'dobare_wc_blocks_integration',
			$style_url,
			[],
			$this->get_file_version($style_path)
		);

			wp_register_script(
			'dobare_wc_blocks_integration',
			$script_url,
			$script_asset['dependencies'],
			$script_asset['version'],
			true
		);
		wp_set_script_translations(
			'dobare_wc_blocks_integration',
			'woocommerce-example-plugin',
			dirname(DOBARE_MAIN_PLUGIN_FILE) . '/languages'
		);
	}

	/**
	 * @inheritDoc
	 */
	public function get_script_handles(): array
	{
		return array('dobare_wc_blocks_integration');
	}

	/**
	 * @inheritDoc
	 */
	public function get_editor_script_handles(): array
	{
		return array('dobare_wc_blocks_integration');
	}

	/**
	 * @inheritDoc
	 */
	public function get_script_data(): array
	{
		//		$woocommerce_example_plugin_data = some_expensive_serverside_function();

		return [
			'expensive_data_calculation' => 'data'
		];
	}

	/**
	 * @inheritDoc
	 */
	protected function get_file_version($file)
	{
		if (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG && file_exists($file)) {
			return filemtime($file);
		}

		return DOBARE_VERSION;
	}
}
