<?php
/**
 * Locations.
 *
 * @package MadeByAura\Prime\Plugins\Elementor\Hooks
 * @author  MadeByAura.com
 */

namespace MadeByAura\Prime\Plugins\Elementor\Hooks;

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

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

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

	/**
	 * Constructor.
	 */
	protected function __construct() {
		// Register theme locations.
		add_action( 'elementor/theme/register_locations', [ __CLASS__, 'register' ] );

		// Do not override theme template for archive location.
		add_filter( 'elementor/theme/need_override_location', [ __CLASS__, 'change_override_theme_template_conditions' ], 10, 3 );
	}

	/**
	 * Register theme locations.
	 *
	 * @param \Elementor\Modules\ThemeBuilder\Classes\Locations_Manager $manager  Locations manager.
	 */
	public static function register( $manager ) {
		$manager->register_all_core_location();

		// Mobile Navigation.
		$manager->register_location(
			'aura_mobile_navigation',
			[
				'label'           => __( 'Mobile Navigation', 'aura-prime' ),
				'multiple'        => false,
				'edit_in_content' => false,
			],
		);

		// Preface.
		$manager->register_location(
			'aura_preface',
			[
				'label'           => __( 'Preface', 'aura-prime' ),
				'multiple'        => false,
				'edit_in_content' => false,
			],
		);

		// Sidebar CTA.
		$manager->register_location(
			'aura_sidebar_cta',
			[
				'label'           => __( 'Sidebar CTA', 'aura-prime' ),
				'multiple'        => false,
				'edit_in_content' => false,
			],
		);

		// Sticky Header.
		if ( Base::get_mod( 'sticky_header', 'enabled' ) ) {
			$manager->register_location(
				'aura_sticky_header',
				[
					'label'           => __( 'Sticky Header', 'aura-prime' ),
					'multiple'        => false,
					'edit_in_content' => false,
				],
			);
		}

		// Sticky Footer.
		if ( Base::get_mod( 'sticky_footer', 'enabled' ) ) {
			$manager->register_location(
				'aura_sticky_footer',
				[
					'label'           => __( 'Sticky Footer', 'aura-prime' ),
					'multiple'        => false,
					'edit_in_content' => false,
				],
			);
		}
	}

	/**
	 * Do not override theme template for archive location.
	 *
	 * @link https://github.com/yangm97/elementor-pro/blob/master/modules/theme-builder/classes/locations-manager.php
	 *
	 * @param bool   $override_theme_template  Whether to override theme location.
	 * @param string $location                 Location name.
	 */
	public static function change_override_theme_template_conditions( $override_theme_template, $location ) {
		if ( 'archive' === $location ) {
			$override_theme_template = false;
		}

		return $override_theme_template;
	}
}
