<?php

/**
 * Control for currency settings
 *
 * @package Kubelio\Customizer
 * @since 1.0.0
 */

namespace Kubelio\Customizer;

if (! defined('ABSPATH')) {
	exit;
}

class Currency {

	use Helper;

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action('after_setup_theme', array($this, 'register_fields'));
	}

	/**
	 * Register Currency related customizer fields
	 */
	public function register_fields() {
		$this->set_section();
		$this->add_currency_fields();
	}

	/**
	 * Set sections and panels
	 */
	public function set_section() {
		$this->add_section(
			'general_currency',
			array(
				'title'    => esc_html__('Currency Switcher', 'kubelio'),
				'description' => sprintf(
					/* translators: %s is the URL to the FOX Currency Switcher plugin. */
					wp_kses_post(__('This feature is a frontend interface and requires a third-party plugin to handle backend price conversion. Optimized for <a href="%s" target="_blank">FOX Currency Switcher</a> plugin.', 'kubelio')),
					'https://wordpress.org/plugins/woocommerce-currency-switcher/'
				),
				'panel'    => 'general',
				'priority' => 30,
			)
		);
	}

	/**
	 * Add currency fields
	 */
	public function add_currency_fields() {
		$section = 'general_currency';

		// Enable Switcher
		$this->add_field(
			array(
				'type'     => 'toggle',
				'settings' => 'currency_switcher',
				'label'    => esc_html__('Enable Switcher', 'kubelio'),
				'default'  => false,
				'section'  => $section,
			)
		);

		// Flag Toggle
		$this->add_field(
			array(
				'type'     => 'toggle',
				'settings' => 'currency_flag',
				'label'    => esc_html__('Show Flag', 'kubelio'),
				'default'  => true,
				'section'  => $section,
				'active_callback' => array(
					array(
						'setting'  => 'currency_switcher',
						'operator' => '==',
						'value'    => true,
					),
				),
			)
		);

		// Display Format
		$this->add_field(
			array(
				'type'     => 'text',
				'settings' => 'currency_format',
				'label'    => esc_html__('Display Format', 'kubelio'),
				'description' => esc_html__('Placeholders: {{country}}, {{currency}}, {{symbol}}', 'kubelio'),
				'default'  => '{{currency}} {{symbol}}',
				'section'  => $section,
				'active_callback' => array(
					array(
						'setting'  => 'currency_switcher',
						'operator' => '==',
						'value'    => true,
					),
				),
			)
		);
	}
}
