<?php
/**
 * Block editor header language badge helper.
 *
 * Adds the current post language label and flag to the block editor header.
 *
 * @package WPR_Translate
 */

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

/**
 * Localize the post language data for the block editor header badge.
 *
 * @param string $hook Current admin page hook.
 */
function wpr_translate_enqueue_post_editor_language_badge_assets( $hook ) {
    // Only target post editor screens.
    if ( 'post.php' !== $hook && 'post-new.php' !== $hook ) {
        return;
    }

    $language_code = '';

    if ( function_exists( 'wpr_translate_return_current_language_admin' ) ) {
        // Prefer the admin language selector when available.
        $language_code = sanitize_key( wpr_translate_return_current_language_admin() );
    }

    if ( '' === $language_code && function_exists( 'wpr_translation_get_default_language_code' ) && function_exists( 'wpestate_translation_get_active_languages' ) ) {
        // Fall back to the default language when none is assigned.
        $languages     = wpestate_translation_get_active_languages();
        $language_code = sanitize_key( wpr_translation_get_default_language_code( $languages ) );
    }

    if ( '' === $language_code ) {
        // Fall back to the stored option used by legacy integrations.
        $language_code = sanitize_key( get_option( 'wpestate_default_language', '' ) );
    }

    if ( '' === $language_code ) {
        // Final fallback to English when no default is stored.
        $language_code = 'en';
    }

    $language_name = '';
    $flag_code     = '';

    if ( function_exists( 'wpr_translate_get_language' ) ) {
        // Load the language metadata for display.
        $language_data = wpr_translate_get_language( $language_code );

        if ( is_array( $language_data ) ) {
            if ( ! empty( $language_data['name'] ) ) {
                $language_name = sanitize_text_field( $language_data['name'] );
            }

            if ( ! empty( $language_data['flag'] ) ) {
                $flag_code = sanitize_key( $language_data['flag'] );
            }
        }
    }

    if ( '' === $language_name ) {
        // Use the language code as a label when no name is stored.
        $language_name = strtoupper( $language_code );
    }

    if ( '' === $flag_code && '' !== $language_code ) {
        // Default to the language code when no explicit flag is set.
        $flag_code = sanitize_key( $language_code );
    }

    $flag_url = '';

    if ( '' !== $flag_code && defined( 'WPR_TRANSLATE_URL' ) ) {
        // Build the flag URL from the plugin assets.
        $flag_url = esc_url_raw( WPR_TRANSLATE_URL . 'assets/img/flags/4x3/' . $flag_code . '.svg' );
    }

    $payload = array(
        'label'   => sanitize_text_field( $language_name ),
        'code'    => sanitize_text_field( $language_code ),
        'flagUrl' => $flag_url,
        /* translators: %s: Language name. */
        'flagAlt' => sanitize_text_field( sprintf( __( '%s flag', 'wpr-translate' ), $language_name ) ),
    );

    // Attach the language payload to the existing admin bundle.
    wp_localize_script( 'wpr-translate-admin', 'wprTranslatePostLanguage', $payload );
}
add_action( 'admin_enqueue_scripts', 'wpr_translate_enqueue_post_editor_language_badge_assets', 20 );
