<?php

/**
 * Trait to help with Kirki calls using the Object-Oriented Standard (Kirki 4+).
 *
 * @package Kubelio\Customizer
 * @since 1.0.0
 */

namespace Kubelio\Customizer;

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

trait Helper {

	/**
	 * Main configuration ID for Kirki.
	 */
	protected $config_id = 'kubelio_customizer';

	/**
	 * Map of friendly field types to their Kirki OO field classes.
	 */
	protected static $field_map = array(
		'background'      => '\Kirki\Field\Background',
		'color'           => '\Kirki\Field\Color',
		'color_palette'   => '\Kirki\Field\Color_Palette',
		'dimension'       => '\Kirki\Field\Dimension',
		'dimensions'      => '\Kirki\Field\Dimensions',
		'image'           => '\Kirki\Field\Image',
		'multicheck'      => '\Kirki\Field\Multicheck',
		'multicolor'      => '\Kirki\Field\Multicolor',
		'number'          => '\Kirki\Field\Number',
		'radio-buttonset' => '\Kirki\Field\Radio_Buttonset',
		'radio-image'     => '\Kirki\Field\Radio_Image',
		'repeater'        => '\Kirki\Field\Repeater',
		'select'          => '\Kirki\Field\Select',
		'slider'          => '\Kirki\Field\Slider',
		'sortable'        => '\Kirki\Field\Sortable',
		'toggle'          => '\Kirki\Field\Checkbox_Toggle',
		'typography'      => '\Kirki\Field\Typography',
		'upload'          => '\Kirki\Field\Upload',
		'url'             => '\Kirki\Field\URL',
		'link'            => '\Kirki\Field\URL',
		'text'            => '\Kirki\Field\Text',
		'textarea'        => '\Kirki\Field\Textarea',
		'generic'         => '\Kirki\Field\Generic',
		'editor'          => '\Kirki\Field\Editor',
		'code'            => '\Kirki\Field\Code',
		'headline'        => '\Kirki\Field\Headline',
		'divider'         => '\Kirki\Field\Divider',
		'responsive'      => '\Kirki\Field\Responsive',
	);

	/**
	 * Wrapper for Kirki\Panel
	 */
	public function add_panel($name, $args) {
		new \Kirki\Panel($name, $args);
	}

	/**
	 * Wrapper for Kirki\Section
	 */
	public function add_section($name, $args) {
		new \Kirki\Section($name, $args);
	}

	/**
	 * Add field control with direct OO instantiation
	 */
	public function add_field($args) {
		$type = isset($args['type']) ? $args['type'] : 'text';

		$args['config']        = $this->config_id;
		$args['option_config'] = $this->config_id;

		// Kirki's webfonts module only enqueues Google Fonts for fields whose
		// type is the canonical 'kirki-typography'. The 'typography' alias is
		// skipped, so normalize it here.
		if ('typography' === $type) {
			$args['type'] = 'kirki-typography';
		}

		// Headline/divider are display-only; give them a unique setting id.
		if (! isset($args['settings']) && in_array($type, array('headline', 'divider'), true)) {
			$args['settings'] = "control_{$type}_" . uniqid();
		}

		$class_name = isset(self::$field_map[$type]) ? self::$field_map[$type] : '\Kirki\Field\Text';

		if (class_exists($class_name)) {
			new $class_name($args);
			return;
		}

		\Kirki::add_field($this->config_id, $args);
	}
}
