<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
class VI_WOOCOMMERCE_LUCKY_WHEEL_Plugins_Translatepress extends WLWL_Multi_Languages {
	public function __construct() {
		if (  class_exists( 'WPML\Plugins')) {
			return;
		}
		if (  class_exists('Polylang') ) {
			return;
		}
		if ( ! class_exists('TRP_Translate_Press') ) {
			return;
		}
		parent::__construct();
	}
	public function get_current_language() {
		if ( isset( $this->cache['current_language'] ) ) {
			return $this->cache['current_language'];
		}
		if (function_exists('trp_get_current_language')){
			$current_language=trp_get_current_language();
		}else {
			global $TRP_LANGUAGE;
			$current_language = $TRP_LANGUAGE;
		}
		if (!$current_language){
			$current_language = get_locale();
		}
		if ($current_language === $this->get_default_language()){
			$current_language ='';
		}
		$this->cache['current_language'] = $current_language;
		return $this->cache['current_language'];
	}
	public function get_default_language() {
		if ( isset( $this->cache['default_language']) ) {
			return $this->cache['default_language'];
		}
		$trp_settings   = TRP_Translate_Press::get_trp_instance()->get_component( 'settings' )->get_settings();
		$this->cache['default_language'] = $trp_settings['default-language']??get_locale();
		return $this->cache['default_language'];
	}
	public function get_languages_data() {
		if ( isset( $this->cache['languages_data'] ) ) {
			return $this->cache['languages_data'];
		}
		$languages_data =[];
		$languages = trp_get_languages();
		foreach ( $languages as $key => $value ) {
			$languages_data[$key]=[
				'code' => $key,
				'translated_name' => $value,
				'url' => '' ,
				'country_flag_url' => $this->get_flag_url($key, $value),
			];
		}
		$this->cache['languages_data'] = $languages_data;
		return $this->cache['languages_data'];
	}
	public function get_flag_url($language_code, $language_name) {
		if ( isset( $this->cache['country_flag_url'][$language_code] ) ) {
			return $this->cache['country_flag_url'][$language_code];
		}
		$flags_path = TRP_PLUGIN_URL .'assets/images/flags/';
		$flags_path = apply_filters( 'trp_flags_path', $flags_path, $language_code );
		$flag_file_name = $language_code .'.png';
		$flag_file_name = apply_filters( 'trp_flag_file_name', $flag_file_name, $language_code );
		$this->cache['country_flag_url'][$language_code] = $flags_path . $flag_file_name;
		return $this->cache['country_flag_url'][$language_code] ;
	}
}