<?php

namespace VertexMediaLLC\ZyreElementorAddons\Modules\ThemeBuilder\Compatibility;

use VertexMediaLLC\ZyreElementorAddons\Modules\ThemeBuilder\Module as Theme_Builder;
use VertexMediaLLC\ZyreElementorAddons\Modules\ThemeBuilder\Conditions_Manager;

defined( 'ABSPATH' ) || exit;

/**
 * Astra theme compatibility.
 */
class Astra {

	/**
	 * Instance of Elementor Frontend class.
	 *
	 * @var \Elementor\Frontend()
	 */
	private $elementor;

	private $header;
	private $footer;

	/**
	 * Run all the Actions / Filters.
	 */
	public function __construct() {

		$headers = Conditions_Manager::instance()->get_documents_for_location( 'header' );
		$footers = Conditions_Manager::instance()->get_documents_for_location( 'footer' );

		if ( ! empty( $headers ) ) {
			$this->header = $headers[0];
		}

		if ( ! empty( $footers ) ) {
			$this->footer = $footers[0];
		}

		if ( defined( 'ELEMENTOR_VERSION' ) && is_callable( 'Elementor\Plugin::instance' ) ) {
			$this->elementor = \Elementor\Plugin::instance();
		}

		if ( null !== $this->header ) {
			add_action( 'template_redirect', [ $this, 'remove_theme_header_markup' ], 10 );
			add_action( 'astra_header', [ $this, 'add_plugin_header_markup' ] );
		}

		if ( null !== $this->footer ) {
			add_action( 'template_redirect', [ $this, 'remove_theme_footer_markup' ], 10 );
			add_action( 'astra_footer', [ $this, 'add_plugin_footer_markup' ] );
		}
	}

	// header actions
	public function remove_theme_header_markup() {
		remove_action( 'astra_header', 'astra_header_markup' );
	}

	public function add_plugin_header_markup() {
		do_action( 'zyreladdons/template/before_header' );
		echo '<div class="ekit-template-content-markup ekit-template-content-header">';
		echo Theme_Builder::render_builder_data( $this->header ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo '</div>';
		do_action( 'zyreladdons/template/after_header' );
	}

	// footer actions
	public function remove_theme_footer_markup() {
		remove_action( 'astra_footer', 'astra_footer_markup' );
	}

	public function add_plugin_footer_markup() {
		do_action( 'zyreladdons/template/before_footer' );
		echo '<div class="ekit-template-content-markup ekit-template-content-footer">';
		echo Theme_Builder::render_builder_data( $this->footer ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo '</div>';
		do_action( 'zyreladdons/template/after_footer' );
	}
}
