<?php
/**
 * Custom Heading Style Option Class
 *
 * @package errigal
 * @since 1.1.0
 */

namespace Errigal\Customiser;

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Content_Class as Content_Class,
	Errigal_Custom_Heading_Style_Hook as Heading_Style_Hook,
	Errigal_Html_Tag as Html_Tag,
	Errigal_Shared_Class as Shared_Class,
};
use Errigal\Customiser\Errigal_Custom_Heading_Style as Heading_Style;
use Errigal\Shared\{
	Errigal_Utility as Utility,
	Errigal_Validator as Validator,
};

/**
 * This class is in charge of heading style customisation via customiser.
 *
 * @since 1.1.0
 */
class Errigal_Custom_Heading_Style_Option extends Errigal_Custom_Option_Base {
	/**
	 * Gets controls args.
	 *
	 * @since 1.1.0
	 *
	 * @return array
	 */
	public function get_controls_args() {
		return array(
			self::CONTROLS_TO_ADD => array(
				Heading_Style::ID_HEADING_STYLE => array(
					self::CUSTOMISE_ARGS_SETTING => array(
						self::SETTING_DEFAULT   => Heading_Style::DEFAULT_HEADING_STYLE,
						self::SETTING_TRANSPORT => self::SETTING_TRANSPORT_POST_MESSAGE,
					),
					self::CUSTOMISE_ARGS_CONTROL => array(
						self::CONTROL_PRIORITY => self::CONTROL_PRIORITY_APPEARANCE_HEADING_STYLE,
						self::CONTROL_LABEL    => esc_html__( 'Heading Style', 'errigal' ),
						self::CONTROL_CHOICES  => $this->get_heading_styles(),
						self::CONTROL_TYPE     => self::CONTROL_TYPE_SELECT,
					),
					self::CUSTOMISE_ARGS_PARTIAL => array(
						self::PARTIAL_SELECTOR         => $this->get_headings_selector(),
						self::PARTIAL_FALLBACK_REFRESH => false,
					),
				),
			),
		);
	}

	/**
	 * Gets heading styles.
	 *
	 * @since 1.1.0
	 *
	 * @return array
	 */
	private function get_heading_styles() {
		/**
		 * Filters heading styles.
		 *
		 * @since 1.1.0
		 *
		 * @param array $heading_styles Associative array of heading styles.
		 */
		$heading_styles = apply_filters(
			Heading_Style_Hook::HEADING_STYLES,
			array(
				Heading_Style::HEADING_STYLE_STANDARD => esc_html__( 'Standard', 'errigal' ),
				Heading_Style::HEADING_STYLE_SIMPLE   => esc_html__( 'Simple', 'errigal' ),
			),
		);

		return Utility::sanitise_associative_array( $heading_styles );
	}

	/**
	 * Gets headings selector.
	 *
	 * @since 1.1.0
	 *
	 * @return string
	 */
	private function get_headings_selector() {
		$headings = Html_Tag::HEADINGS;

		if ( ! Validator::is_string_list( $headings ) ) {
			return '';
		}

		$selectors = array();

		foreach ( $headings as $heading ) {
			if ( ! Validator::is_non_empty_string( $heading ) ) {
				continue;
			}

			$selectors[] = Utility::get_selector(
				Content_Class::ENTRY_CONTENT,
				$heading . Utility::get_negation_pseudo_class_selector( Shared_Class::SIMPLE_HEADING )
			);
		}

		return Utility::combine_selectors( ...$selectors );
	}
}
