<?php

declare(strict_types=1);


namespace LidonationDevs\ADAPAYFWC\Admin;

defined('ABSPATH') || exit;


/**
 * LidonationAdaPayments Setup Class
 */
class Setup
{
	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 */
	public function __construct()
	{
		add_action('admin_enqueue_scripts', array($this, 'register_scripts'));
		add_action('admin_menu', array($this, 'register_page'));
	}

	/**
	 * Load all necessary dependencies.
	 *
	 * @since 1.0.0
	 */
	public function register_scripts()
	{
		if (
			! method_exists('Automattic\WooCommerce\Admin\PageController', 'is_admin_or_embed_page') ||
			! \Automattic\WooCommerce\Admin\PageController::is_admin_or_embed_page()
		) {
			return;
		}

		$script_path = '/build/index.js';
		$script_asset_path = dirname(ADAPAYFWC_PLUGIN_FILE) . '/build/index.asset.php';
		$script_asset = file_exists($script_asset_path)
			? require $script_asset_path
			: array(
				'dependencies' => array(),
				'version' => filemtime($script_path),
			);

		$script_url = plugins_url($script_path, ADAPAYFWC_PLUGIN_FILE);

		wp_register_script(
			'adapayments-woocommerce',
			$script_url,
			$script_asset['dependencies'],
			$script_asset['version'],
			true
		);

		wp_enqueue_script('adapayments-woocommerce');
	}

	/**
	 * Register page in wc-admin.
	 *
	 * @since 1.0.0
	 */
	public function register_page()
	{
		if (! function_exists('wc_admin_register_page')) {
			return;
		}

		if (! current_user_can('manage_woocommerce')) {
			wp_die('You do not have sufficient permissions to access this page.');
		} else {
			$user = wp_get_current_user();
		}
	}
}
