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

if ( ! function_exists('eurostocks_get_language_details') ) {
    /**
     * Gets Polylang / WPML / fallback language details in a single function call.
     *
     * @param array $accepted_languages Optional. Allowed languages (for your logic).
     * @return array {
     *     @type array  $languages      Full language info from Polylang or WPML (or empty fallback).
     *     @type array  $language_codes Slugs/codes for recognized languages.
     *     @type string $default_lang   The default language code.
     *     @type string $current_lang   The current language code.
     *     @type bool   $diflang        Whether current language differs from default.
     *     @type bool   $show_lang      Whether more than one language is available.
     * }
     */
    function eurostocks_get_language_details($accepted_languages = array('nl', 'de', 'en', 'fr', 'es')) {

        $languages       = array();
        $language_codes  = array();
        $default_lang    = 'en';
        $current_lang    = 'en';
        $diflang         = false;
        $show_lang       = false;

        // Polylang detection
        if ( function_exists('pll_the_languages') ) {
            /** @disregard pll_the_languages */
            $languages = pll_the_languages(array('raw' => 1));
            $language_codes = array_map(
                function ($lang) {
                    return $lang['slug'];
                },
                $languages
            );
            $language_codes = array_values($language_codes);
            /** @disregard pll_the_languages */
            $default_lang  = pll_default_language();
            /** @disregard pll_the_languages */
            $current_lang  = pll_current_language();

        // WPML detection
        } elseif ( defined('ICL_SITEPRESS_VERSION') ) {
            $languages = apply_filters('wpml_active_languages', null);
            $language_codes = array_map(
                function ($lang) {
                    return $lang['language_code'];
                },
                $languages
            );
            $language_codes = array_values($language_codes);

            $default_lang = apply_filters('wpml_default_language', null);
            $current_lang = apply_filters('wpml_current_language', null);

        // Fallback (no Polylang / WPML)
        }elseif(function_exists('weglot_get_current_language')) {
            /** @disregard weglot_get_current_language */
            // $weglot = weglot_get_languages_available();
            /** @disregard weglot_get_destination_languages */
            $weglotdest = weglot_get_destination_languages();
            // get language_to from weglotdest

            $destinations = array_map(function ($lang) {
                return $lang['language_to']; // Access the value directly from the array
            }, iterator_to_array($weglotdest));
   
            
            $language_codes = array_values($destinations);
            // add default lang to language_codes
            /** @disregard weglot_get_default_language */
            $language_codes[] = weglot_get_original_language();
            /** @disregard weglot_get_default_language */
            $default_lang = weglot_get_original_language();
            /** @disregard weglot_get_default_language */
            $current_lang = weglot_get_current_language();

          } else {
            // Use the site locale
            $default_locale = get_locale(); // e.g. 'nl_NL'
            switch ($default_locale) {
                case 'nl_NL':
                    $default_lang = 'nl';
                    break;
                case 'de_DE':
                    $default_lang = 'de';
                    break;
                case 'fr_FR':
                    $default_lang = 'fr';
                    break;
                case 'es_ES':
                    $default_lang = 'es';
                    break;
                default:
                    $default_lang = 'en';
            }
            $current_lang = $default_lang;
        }

        // Are we on a different language than the default?
        $diflang = ($current_lang !== $default_lang);


        // Do we show the language switch? Only if multiple languages exist
        if ($language_codes && count($language_codes) > 1) {
            $show_lang = true;
        }

        return array(
            'languages'      => $languages,
            'language_codes' => $language_codes,
            'default_lang'   => $default_lang,
            'current_lang'   => $current_lang,
            'diflang'        => $diflang,
            'show_lang'      => $show_lang,
        );
    }
}

if ( ! function_exists('eurostocks_normalize_makes_list') ) {
    /**
     * Normalizes the stored makes payload into a consistent list.
     *
     * @param mixed $makes Raw makes option value or API response.
     * @return array<int, array{key:string,description:string}>
     */
    function eurostocks_normalize_makes_list($makes) {
        if ( ! is_array($makes) ) {
            return array();
        }

        $normalized = array();

        foreach ( $makes as $make ) {
            if ( is_array($make) ) {
                $key = isset($make['key']) ? sanitize_text_field((string) $make['key']) : '';
                $description = isset($make['description']) ? sanitize_text_field((string) $make['description']) : '';

                if ( '' === $key ) {
                    continue;
                }

                if ( '' === $description ) {
                    $description = $key;
                }

                $normalized[] = array(
                    'key' => $key,
                    'description' => $description,
                );
                continue;
            }

            if ( is_string($make) ) {
                $make = sanitize_text_field($make);

                if ( '' === $make ) {
                    continue;
                }

                $normalized[] = array(
                    'key' => $make,
                    'description' => $make,
                );
            }
        }

        return $normalized;
    }
}
