<?php
/**
 * Comment Hooks Class
 *
 * @package errigal
 * @since 1.1.0
 */

namespace Errigal\Hook;

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Component_Class as Component_Class,
	Errigal_WP_Hook as WP_Hook,
};
use Errigal\Shared\{
	Errigal_Utility as Utility,
	Errigal_Validator as Validator,
};

/**
 * This class is in charge of adding comment hooks.
 *
 * @since 1.1.0
 */
class Errigal_Comment_Hooks extends Errigal_Hooks_Base {
	/**
	 * Comment form field: comment_field.
	 *
	 * @since 1.2.0
	 *
	 * @link https://developer.wordpress.org/reference/hooks/comment_form_defaults/
	 * @link https://developer.wordpress.org/reference/functions/comment_form/
	 *
	 * @var int
	 */
	private const COMMENT_FIELD = 'comment_field';

	/**
	 * Class: comment-form-cookies-consent.
	 *
	 * @since 1.2.0
	 *
	 * @link https://developer.wordpress.org/reference/hooks/comment_form_defaults/
	 * @link https://developer.wordpress.org/reference/functions/comment_form/
	 *
	 * @var int
	 */
	private const COMMENT_FORM_COOKIES_CONSENT_CLASS = 'comment-form-cookies-consent';

	/**
	 * Class: comment-reply-link.
	 *
	 * @since 1.2.0
	 *
	 * @link https://developer.wordpress.org/reference/hooks/comment_reply_link/
	 * @link https://developer.wordpress.org/reference/functions/get_comment_reply_link/
	 *
	 * @var int
	 */
	private const COMMENT_REPLY_LINK_CLASS = 'comment-reply-link';

	/**
	 * Adds class to comment form default fields.
	 *
	 * @since 1.0.0
	 *
	 * @link https://developer.wordpress.org/reference/hooks/comment_form_default_fields/
	 *
	 * @param array $fields Associative array of Comment fields.
	 * @return array
	 */
	public function add_class_to_comment_form_default_fields( $fields ) {
		if ( Validator::is_associative_array( $fields ) ) {
			foreach ( $fields as &$field ) {
				$field = Utility::add_class(
					$field,
					Validator::string_contains( $field, self::COMMENT_FORM_COOKIES_CONSENT_CLASS )
						? Component_Class::CHECKBOX
						: Component_Class::INPUT_FIELD
				);
			}
		}

		return $fields;
	}

	/**
	 * Adds class to comment form textarea.
	 *
	 * @since 1.0.0
	 *
	 * @link https://developer.wordpress.org/reference/hooks/comment_form_defaults/
	 *
	 * @param array $defaults Associative array of arguments for comment form.
	 * @return array
	 */
	public function add_class_to_comment_form_textarea( $defaults ) {
		if ( Validator::has_array_key( $defaults, self::COMMENT_FIELD ) ) {
			$defaults[ self::COMMENT_FIELD ] = Utility::add_class(
				$defaults[ self::COMMENT_FIELD ],
				Component_Class::INPUT_FIELD,
			);
		}

		return $defaults;
	}

	/**
	 * Adds class to comment reply link.
	 *
	 * @since 1.0.0
	 *
	 * @link https://developer.wordpress.org/reference/hooks/comment_reply_link/
	 *
	 * @param string $comment_reply_link HTML markup for comment reply link.
	 * @return string
	 */
	public function add_class_to_comment_reply_link( $comment_reply_link ) {
		if ( Validator::is_non_empty_string( $comment_reply_link ) ) {
			$comment_reply_link = Utility::add_class(
				$comment_reply_link,
				array(
					Component_Class::BUTTON,
					Component_Class::OUTLINED,
				),
				self::COMMENT_REPLY_LINK_CLASS
			);
		}

		return $comment_reply_link;
	}

	/**
	 * Adds hooks.
	 *
	 * @since 1.1.0
	 *
	 * @return void
	 */
	protected function add_hooks() {
		add_filter( WP_Hook::COMMENT_FORM_DEFAULT_FIELDS, array( $this, 'add_class_to_comment_form_default_fields' ) );
		add_filter( WP_Hook::COMMENT_FORM_DEFAULTS, array( $this, 'add_class_to_comment_form_textarea' ) );
		add_filter( WP_Hook::COMMENT_REPLY_LINK, array( $this, 'add_class_to_comment_reply_link' ) );
	}
}
