<?php
/**
 * MenuX Free — Multilingual Support
 * Auto-detects WPML, Polylang, TranslatePress, Multilingual Press.
 * @package MenuX
 */

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

function menux_get_supported_languages( $force_refresh = false ) {
    $cached = get_transient( 'menux_supported_languages' );
    if ( $cached !== false && ! $force_refresh ) {
        return $cached;
    }

    $langs = array();

    // WPML
    if ( defined( 'ICL_SITEPRESS_VERSION' ) && function_exists( 'icl_get_languages' ) ) {
        $wpml = icl_get_languages( 'skip_missing=0' );
        if ( ! empty( $wpml ) && is_array( $wpml ) ) {
            foreach ( $wpml as $l ) {
                if ( empty( $l['language_code'] ) ) continue;
                $langs[] = array(
                    'code'  => sanitize_text_field( $l['language_code'] ),
                    'label' => isset( $l['translated_name'] ) ? sanitize_text_field( $l['translated_name'] ) : $l['language_code'],
                );
            }
        }
    }

    // Polylang
    if ( empty( $langs ) && function_exists( 'pll_languages_list' ) ) {
        $pll = pll_languages_list( array( 'fields' => array() ) );
        if ( ! empty( $pll ) && is_array( $pll ) ) {
            foreach ( $pll as $l ) {
                $langs[] = array(
                    'code'  => isset( $l->locale ) ? sanitize_text_field( $l->locale ) : sanitize_text_field( $l->slug ),
                    'label' => isset( $l->name )   ? sanitize_text_field( $l->name )   : sanitize_text_field( $l->slug ),
                );
            }
        }
    }

    // TranslatePress
    if ( empty( $langs ) && class_exists( 'TRP_Translate_Press' ) ) {
        $trp = get_option( 'trp_settings', array() );
        if ( ! empty( $trp['translation-languages'] ) && is_array( $trp['translation-languages'] ) ) {
            $names = isset( $trp['translation-languages-names'] ) ? $trp['translation-languages-names'] : array();
            foreach ( $trp['translation-languages'] as $i => $locale ) {
                $langs[] = array(
                    'code'  => sanitize_text_field( $locale ),
                    'label' => isset( $names[ $i ] ) ? sanitize_text_field( $names[ $i ] ) : sanitize_text_field( $locale ),
                );
            }
        }
    }

    // Multilingual Press
    if ( empty( $langs ) && function_exists( 'mlp_get_available_languages' ) ) {
        $mlp = mlp_get_available_languages();
        if ( ! empty( $mlp ) && is_array( $mlp ) ) {
            foreach ( $mlp as $locale => $name ) {
                $langs[] = array(
                    'code'  => sanitize_text_field( $locale ),
                    'label' => sanitize_text_field( $name ),
                );
            }
        }
    }

    // Fallback: built-in languages (English, Italian, Russian).
    // No external multilingual plugin is required — every menu item gets an
    // EN / IT / RU label field out of the box. The frontend switches between
    // them based on the page's <html lang> attribute (see assets/js/frontend.js).
    if ( empty( $langs ) ) {
        $langs = menux_builtin_languages();
    }

    set_transient( 'menux_supported_languages', $langs, HOUR_IN_SECONDS );
    return $langs;
}

/**
 * Built-in language set used when no multilingual plugin is detected.
 * The site's own locale (if it is one of the built-in languages) is moved to
 * the front so it becomes the default label rendered before JS runs.
 *
 * @return array List of { code, label } entries.
 */
function menux_builtin_languages() {
    $builtin = array(
        array( 'code' => 'en-US', 'label' => 'English'  ),
        array( 'code' => 'it-IT', 'label' => 'Italiano' ),
        array( 'code' => 'ru-RU', 'label' => 'Русский'  ),
    );

    /**
     * Filter the built-in language set (codes must stay in `xx-XX` form).
     * @param array $builtin Default English / Italian / Russian list.
     */
    $builtin = apply_filters( 'menux_builtin_languages', $builtin );

    // Promote the site locale's language to the front when it matches.
    $site_prefix = strtolower( substr( get_locale(), 0, 2 ) );
    $primary     = array();
    $rest        = array();
    foreach ( $builtin as $lang ) {
        if ( strtolower( substr( $lang['code'], 0, 2 ) ) === $site_prefix ) {
            $primary[] = $lang;
        } else {
            $rest[] = $lang;
        }
    }

    return array_merge( $primary, $rest );
}

/* AJAX: force-refresh languages from admin panel */
add_action( 'wp_ajax_menux_reload_languages', 'menux_ajax_reload_languages' );
function menux_ajax_reload_languages() {
    if ( ! current_user_can( 'manage_options' ) ) wp_die( 'Unauthorized' );
    check_ajax_referer( 'menux_reload_languages_nonce', 'nonce' );
    delete_transient( 'menux_supported_languages' );
    $langs = menux_get_supported_languages( true );
    wp_send_json_success( $langs );
}
