<?php

namespace Prodigy\includes\helpers;

use Elementor\Plugin;
use Prodigy\Includes\Prodigy_Elementor_Template_Loader;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Prodigy_Layouts helper
 */
class Prodigy_Elementor_Layouts {

	/**
	 * Helper method to determine if the action is related to Elementor
	 *
	 * @return bool
	 */
	public static function is_elementor_action(): bool {
		return ( isset( $_GET['action'] ) && $_GET['action'] === 'elementor' ) ||
				( isset( $_POST['action'] ) && $_POST['action'] === 'elementor_ajax' );
	}


	/**
	 * Check if current page using elementor template
	 *
	 * @return bool
	 */
	public static function is_elementor_template(): bool {
		if ( ! is_plugin_active( 'elementor-pro/elementor-pro.php' ) || ! did_action( 'elementor/loaded' ) ) {
			return false;
		}

		return Prodigy_Elementor_Template_Loader::is_elementor_page()
				|| ( Prodigy_Elementor_Template_Loader::is_include_single_template() && ! Prodigy_Elementor_Template_Loader::is_exclude_single_template() )
				|| ( Prodigy_Elementor_Template_Loader::is_include_archive_template() && ! Prodigy_Elementor_Template_Loader::is_exclude_archive_template() )
				|| ( Prodigy_Elementor_Template_Loader::is_include_shop_template() && ! Prodigy_Elementor_Template_Loader::is_exclude_shop_template() );
	}


	/**
	 * @return bool
	 */
	public static function is_elementor_live_preview(): bool {
		if ( Prodigy_Elementor::is_elementor_enabled() && ( Plugin::$instance->editor->is_edit_mode() || self::is_elementor_get_template_data_action() ) ) {
			return true;
		}

		return false;
	}


	/**
	 * @return bool
	 */
	public static function is_elementor_get_template_data_action(): bool {
		if (
			isset( $_POST['action'], $_POST['initial_document_id'], $_POST['editor_post_id'], $_POST['actions'], $GLOBALS['post'] ) &&
			$_POST['action'] === 'elementor_ajax'
		) {
				$raw_actions      = wp_unslash( (string) $_POST['actions'] );
				$sanitized_json   = wp_kses( $raw_actions, array() );
				$elementor_action = self::parse_elementor_action( $sanitized_json );

			if ( $elementor_action === 'get_template_data' ) {
				return true;
			}
		}

		return false;
	}


	/**
	 *
	 * @param string $action_data
	 * @return string|null
	 */
	private static function parse_elementor_action( string $action_data ): ?string {
		if ( $action_data === '' ) {
			return null;
		}

		$data = json_decode( $action_data, true );

		if ( ! is_array( $data ) || empty( $data ) ) {
			return null;
		}

		$first = reset( $data );

		if ( ! is_array( $first ) || ! isset( $first['action'] ) ) {
			return null;
		}

		return (string) $first['action'];
	}

	/**
	 * Check if using archive elementor templates
	 *
	 * @return bool
	 */
	public static function is_using_archive_elementor_templates(): bool {
		$elementor_options = get_option( 'elementor_pro_theme_builder_conditions' );
		if ( ! isset( $elementor_options['archive'] ) ) {
			return false;
		}

		return true;
	}


	/**
	 * Check if using some elementor templates
	 *
	 * @return bool
	 */
	public static function is_using_elementor_templates(): bool {
		$elementor_options = get_option( 'elementor_pro_theme_builder_conditions' );
		if ( ! isset( $elementor_options['archive'] ) && ! isset( $elementor_options['single'] ) ) {
			return false;
		}

		return true;
	}
}
