<?php

namespace Dobare\Admin;


use Dobare\Utils\Dobare_Utils;
use Dobare_Integration;
use Exception;

/**
 * Dobare Setup Class
 */
class Dobare_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'));
		\add_action('admin_menu', array($this, 'merge_old_options'));
	}

	public function merge_old_options()
	{
		if (\get_option(\DOBARE_OLD_API_KEY_OPTION_KEY, array()) && ! Dobare_Utils::get_dob_api_key()) {
			$option_array = \get_option(\DOBARE_OLD_API_KEY_OPTION_KEY, array());
			if (! empty($option_array)) {
				$old_api_key_value = $option_array['jwt_key'] ?? '';
				if (! empty($old_api_key_value)) {
					try {
						Dobare_Utils::set_dob_api_key($old_api_key_value);
					} catch (Exception $e) {
					}
				}
			}
		}
	}

	/**
	 * 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(\DOBARE_MAIN_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, \DOBARE_MAIN_PLUGIN_FILE);

		\wp_register_script(
			'dobare',
			$script_url,
			$script_asset['dependencies'],
			$script_asset['version'],
			true
		);

		// Pass dynamic settings to admin app
		\wp_localize_script(
			'dobare',
			'DobareSettings',
			array(
				'currency' => Dobare_Utils::get_currency_word(),
			)
		);

		\wp_register_style(
			'dobare',
			\plugins_url('/build/index.css', \DOBARE_MAIN_PLUGIN_FILE),
			// Add any dependencies styles may have, such as wp-components.
			array(),
			filemtime(dirname(\DOBARE_MAIN_PLUGIN_FILE) . '/build/index.css')
		);

		\wp_enqueue_script('dobare');
		\wp_enqueue_style('dobare');
	}

	/**
	 * Register page in wc-admin.
	 *
	 * @since 1.0.0
	 */
	public function register_page()
	{

		if (! function_exists('wc_admin_register_page')) {
			return;
		}

		wc_admin_register_page(
			array(
				'id'    => 'dobare',
				'title' => __('دوباره', 'dobare'),
				'path'  => '/dobare',
				'icon'  => 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTE1Ljg3MTkgMS45MTk5MlYxNC44NjUxQzE1Ljg3MjIgMTUuNjU4OSAxNS42NTU0IDE2LjQzNzcgMTUuMjQ0OSAxNy4xMTcyQzE0LjgzNDMgMTcuNzk2NyAxNC4yNDU4IDE4LjM1MDkgMTMuNTQyOSAxOC43MTk5QzEyLjg0MDEgMTkuMDg4OSAxMi4wNDk3IDE5LjI1ODcgMTEuMjU3MyAxOS4yMTA4QzEwLjQ2NDkgMTkuMTYyOCA5LjcwMDY1IDE4Ljg5OTEgOS4wNDczNiAxOC40NDgxQzguMzk0MDggMTcuOTk3MSA3Ljg3NjYgMTcuMzc2IDcuNTUwOTMgMTYuNjUyQzcuMjI1MjcgMTUuOTI4IDcuMTAzODMgMTUuMTI4OCA3LjE5OTc2IDE0LjM0MDdDNy4yOTU2OSAxMy41NTI3IDcuNjA1MzQgMTIuODA1OSA4LjA5NTE5IDEyLjE4MTJDOC41ODUwMyAxMS41NTY1IDkuMjM2NDEgMTEuMDc3NyA5Ljk3ODgyIDEwLjc5NjZWMTEuNzk1MkM5Ljk3OSAxMS44NDMgOS45OTM0MiAxMS44ODk3IDEwLjAyMDIgMTEuOTI5MkMxMC4wNDcgMTEuOTY4OCAxMC4wODUgMTEuOTk5NCAxMC4xMjk0IDEyLjAxNzNDMTAuMTczNyAxMi4w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