<?php
/**
 * Analytics.
 *
 * @package WCPOS\WooCommercePOS
 */

namespace WCPOS\WooCommercePOS\Admin;

use const WCPOS\WooCommercePOS\PLUGIN_NAME;
use const WCPOS\WooCommercePOS\PLUGIN_URL;
use const WCPOS\WooCommercePOS\VERSION;

/**
 * Analytics class.
 */
class Analytics {

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
		add_action( 'admin_head', array( $this, 'analytics_css' ) );
	}

	/**
	 * Add CSS to the admin head
	 */
	public function analytics_css() {
		echo '<style>.woocommerce-pos-upgrade-notice { margin: 0 0 24px 0; }</style>';
	}

	/**
	 * Enqueue analytics assets.
	 */
	public function enqueue_assets() {
		$is_development = isset( $_ENV['DEVELOPMENT'] ) && sanitize_text_field( $_ENV['DEVELOPMENT'] );
		$dir = $is_development ? 'build' : 'assets';

		// Inject translation version for i18next.
		wp_add_inline_script(
			'wp-hooks',
			'window.wcpos = window.wcpos || {}; window.wcpos.translationVersion = "' . esc_js( VERSION ) . '";',
			'before'
		);

		wp_enqueue_script(
			PLUGIN_NAME . '-analytics',
			PLUGIN_URL . $dir . '/js/analytics.js',
			array(
				'wp-hooks',
				'wc-components',
			),
			VERSION,
			true
		);
	}
}
