<?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();
		}

		// Prepend the theme's own color palette to the color picker's
		// swatches, ahead of Kirki's built-in defaults (kept, not replaced),
		// unless a field already defines its own swatches/palettes.
		if ('color' === $type && empty($args['choices']['swatches']) && empty($args['choices']['palettes'])) {
			$theme_colors = kubelio_get_theme('color');
			$swatches     = array();

			foreach (array_keys(kubelio_get_palette_settings()) as $slug) {
				if (! empty($theme_colors[$slug])) {
					$swatches[] = $theme_colors[$slug];
				}
			}

			if (! empty($swatches)) {
				$kirki_defaults = apply_filters(
					'kirki_default_color_swatches',
					array('#000000', '#ffffff', '#dd3333', '#dd9933', '#eeee22', '#81d742', '#1e73be', '#8224e3')
				);

				$args['choices']             = isset($args['choices']) ? $args['choices'] : array();
				$args['choices']['swatches'] = array_merge($swatches, $kirki_defaults);
			}
		}

		$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);
	}
}
