<?php
/**
 * Actirise Core Functions
 *
 * @package actirise
 * @since 3.2.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

/**
 * Generate HTML div element for ad format with proper attributes and classes.
 *
 * Creates a div element with data-actirise-slot attribute and device-specific CSS classes
 * for ad placement. Device classes are prefixed with 'device-' and sanitized for output.
 *
 * @since 3.2.0
 *
 * @param string $slot      The ad slot identifier to be used in data-actirise-slot attribute.
 * @param string $device    Space-separated list of device types for CSS class generation.
 * @param string $injection Optional. The injection method ('shortcode', 'php', or empty for none).
 *
 * @return string HTML div element with escaped attributes and classes.
 */
function actirise_generate_html_ad_format( $slot, $device, $injection = '' ) {
	$availe_slots = array( 'aside-desktop', 'intext', 'top-content', 'feed', 'in-feed', 'top-page' );

	if ( empty( $slot ) || empty( $device ) || ! in_array( $slot, $availe_slots, true ) || ( $device !== 'mobile' && $device !== 'desktop' ) || ( $slot === 'aside-desktop' && $device !== 'desktop' ) ) {
		return 'Incorrectly configured shortcode: ' . esc_html( $slot );
	}

	$class_list = 'device-desktop';

	if ( $device === 'desktop' ) {
		$class_list = 'device-desktop device-tablet';
	} else {
		$class_list = 'device-mobile';
	}

	// Build data attributes
	$data_attrs = 'data-actirise-slot="' . esc_attr( $slot ) . '"';
	if ( ! empty( $injection ) && in_array( $injection, array( 'shortcode', 'php' ), true ) ) {
		$data_attrs .= ' data-wp-injection="' . esc_attr( $injection ) . '"';
	}

	return '<div ' . $data_attrs . ' class="' . esc_attr( $class_list ) . ' ads"></div>';
}

/**
 * Output HTML ad format directly to the page.
 *
 * Convenience function that generates and immediately echoes the HTML ad format.
 * Both slot and device parameters are converted to lowercase before processing.
 *
 * @since 3.2.0
 *
 * @param string $slot   The ad slot identifier (will be converted to lowercase).
 * @param string $device Space-separated list of device types (will be converted to lowercase).
 *
 * @return void Outputs HTML directly, no return value.
 */
function actirise_format( $slot, $device ) {
	echo wp_kses(
		actirise_generate_html_ad_format( strtolower( $slot ), strtolower( $device ), 'php' ),
		array(
			'div' => array(
				'data-actirise-slot' => array(),
				'data-wp-injection'  => array(),
				'class'              => array(),
			),
		)
	);
}
