<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
function enqueue_elementor_template_assets( $template_slug, $render = true ) {

	$template_id = Sifency_Addons_Frontend_Helper::find_matching_template( $template_slug );

	if ( ! $template_id || ! class_exists( '\Elementor\Plugin' ) ) {
		return;
	}

	/**
	 * Ensure Elementor frontend styles are registered first
	 */
	add_action(
		'elementor/frontend/after_enqueue_styles',
		function () use ( $template_id ) {

			\Elementor\Plugin::instance()->frontend->enqueue_scripts();

			if ( class_exists( '\Elementor\Core\Files\CSS\Post' ) ) {
				$css_file = new \Elementor\Core\Files\CSS\Post( $template_id );
			} elseif ( class_exists( '\Elementor\Post_CSS_File' ) ) {
				$css_file = new \Elementor\Post_CSS_File( $template_id );
			} else {
				$css_file = null;
			}

			if ( $css_file ) {
				$css_file->enqueue();
			}
		}
	);

	/**
	 * Render Elementor content (optional)
	 */
	if ( $render ) {
		ob_start();
		\Elementor\Plugin::instance()->frontend
			->get_builder_content_for_display( $template_id, true );
		ob_end_clean();
	}
}
