<?php

namespace Ilabs\Inpost_International\Frontend;

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


use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface;

class Checkout_Blocks_Map_Button implements IntegrationInterface {

	/**
	 * The name of the integration.
	 *
	 * @return string
	 */
	public function get_name() {
		return 'inpost_intl_block';
	}

	/**
	 * When called invokes any initialization/setup for the integration.
	 */
	public function initialize() {

		$script_url = inpost_intl_core()->get_plugin_assets_url() . '/build/inpost-intl-block-frontend.js';

		$script_asset = array(
			'dependencies' => array( 'wc-settings', 'wp-data', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-primitives' ),
			'version'      => INPOST_INTL_PLUGIN_VERSION,
		);

		wp_register_script(
			'inpost-international-wc-blocks-integration',
			$script_url,
			$script_asset['dependencies'],
			$script_asset['version'],
			true
		);

		$style_url = inpost_intl_core()->get_plugin_assets_url() . '/build/style-inpost-intl-block.css';
		wp_enqueue_style(
			'inpost-international-wc-blocks-integration',
			$style_url,
			array(),
			$script_asset['version'],
		);

		$transaltions_folder = inpost_intl_core()->get_plugin_dir() . 'lang';

		wp_set_script_translations(
			'inpost-international-wc-blocks-integration',
			'inpost-international',
			$transaltions_folder
		);
	}

	/**
	 * Returns an array of script handles to enqueue in the frontend context.
	 *
	 * @return string[]
	 */
	public function get_script_handles() {
		return array( 'inpost-international-wc-blocks-integration' );
	}

	/**
	 * Returns an array of script handles to enqueue in the editor context.
	 *
	 * @return string[]
	 */
	public function get_editor_script_handles() {
		return array( 'inpost-international-wc-blocks-integration' );
	}

	/**
	 * An array of key, value pairs of data made available to the block on the client side.
	 *
	 * @return array
	 */
	public function get_script_data() {
		return array(
			'inpost_wc_block_enabled' => true,
		);
	}

	/**
	 * 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( $file ) {
		if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $file ) ) {
			return filemtime( $file );
		}

		return INPOST_INTL_PLUGIN_VERSION;
	}
}
