<?php
/**
 * Button.
 *
 * @package MadeByAura\Prime\Hooks
 * @author  MadeByAura.com
 */

namespace MadeByAura\Prime\Hooks;

use MadeByAura\Prime\Base;
use MadeByAura\Prime\Helpers\SingletonTrait;

defined( 'ABSPATH' ) || die();

/**
 * Button.
 */
class Button {
	use SingletonTrait;

	/**
	 * Constructor.
	 */
	protected function __construct() {
		// Set defaults from customizer.
		add_filter( 'aura_theme|view|args|views/button', [ __CLASS__, 'set_defaults_from_customizer' ] );
	}

	/**
	 * Set defaults from customizer.
	 *
	 * @param array $args  Arguments passed to template part.
	 */
	public static function set_defaults_from_customizer( $args ) {
		$type = isset( $args['type'] ) ? $args['type'] : '1';

		// Get location from customizer IF icon_enabled OR icon_before_text is not provided.
		if ( ! isset( $args['icon_enabled'] ) || ! isset( $args['icon_before_text'] ) ) {
			$location = Base::get_mod( "button_{$type}", 'icon_location' );
		}

		// Use value of icon_enabled from customizer IF its not provided.
		if ( ! isset( $args['icon_enabled'] ) ) {
			$args['icon_enabled'] = in_array( $location, [ 'before_text', 'after_text' ], true );
		}

		// Use value of icon_before_text from customizer IF its not provided.
		if ( ! isset( $args['icon_before_text'] ) ) {
			$args['icon_before_text'] = 'before_text' === $location;
		}

		return $args;
	}
}
