<?php
/**
 * Shortcodes for the statement and the feedback route.
 */

defined( 'ABSPATH' ) || exit;

class TSFFE_Block {

	public static function init(): void {
		add_shortcode( 'tsffe_statement', array( __CLASS__, 'statement_shortcode' ) );
		add_shortcode( 'tsffe_feedback', array( __CLASS__, 'feedback_shortcode' ) );
	}

	/**
	 * @return array<string, mixed>
	 */
	private static function get_data(): array {
		$data = get_option( TSFFE_Page::OPTION_STATEMENT, array() );

		return is_array( $data ) ? $data : array();
	}

	public static function statement_shortcode( $atts = array(), $content = '' ): string {
		$data = self::get_data();

		if ( empty( $data ) ) {
			return '<p>' . esc_html__( 'The accessibility statement has not been generated yet.', 'tendforge-statement-forge-for-eaa' ) . '</p>';
		}

		// On the dedicated statement page the page title already IS
		// "Accessibility Statement" (the theme's H1), so the inline heading would
		// repeat it. Embedded anywhere else, keep the heading.
		$on_statement_page = get_the_ID() && get_the_ID() === TSFFE_Page::get_page_id();

		// TSFFE_Render::html() returns wp_kses_post()-sanitized markup.
		return TSFFE_Render::html( $data, ! $on_statement_page );
	}

	public static function feedback_shortcode( $atts = array(), $content = '' ): string {
		$data = self::get_data();

		if ( empty( $data ) ) {
			return '<p>' . esc_html__( 'The accessibility feedback details have not been set yet.', 'tendforge-statement-forge-for-eaa' ) . '</p>';
		}

		// TSFFE_Render::feedback_html() returns wp_kses_post()-sanitized markup.
		return TSFFE_Render::feedback_html( $data );
	}
}
