<?php
/**
 * WeCodeArt Framework.
 *
 * WARNING: This file is part of the core WeCodeArt Framework. DO NOT edit this file under any circumstances.
 * Please do all modifications in the form of a child theme.
 *
 * @package 	WeCodeArt Framework
 * @subpackage 	Support\Assets
 * @copyright   Copyright (c) 2025, WeCodeArt Framework
 * @since 		5.4.5
 * @version		6.4.5
 */

namespace WeCodeArt\Support;

defined( 'ABSPATH' ) || exit;

use WeCodeArt\Admin\Notifications;
use WeCodeArt\Admin\Notifications\Notification;
use WeCodeArt\Config\Interfaces\Integration;
use WeCodeArt\Config\Traits\{ Singleton, No_Conditionals };
use function WeCodeArt\Functions\get_prop;

/**
 * The Locale object.
 */
final class Locale implements Integration {

	use Singleton;
	use No_Conditionals;
	
	const NOTICE_ID = 'wecodeart/locale/notice';

	/**
	 * Register hooks
	 */
	public function register_hooks() {
		\add_action( 'after_setup_theme',	[ $this, 'translations' ] );
		\add_action( 'admin_notices',		[ $this, 'manage_notice' ] );
	}

	/**
	 * Load Translations
	 *
	 * @since	5.4.5
	 * @version 6.4.5
	 *
	 * @return 	void
	 */
	public function translations() {
		$paths 	= wecodeart_config( 'paths' );
		$lang 	= wecodeart_config( 'directories' )['languages'];

		// Loads wp-content/themes/child-theme-name/languages/it_IT.mo.
		load_theme_textdomain( 'wecodeart', $paths['child'] . $lang );

		// Loads wp-content/themes/wecodeart/languages/it_IT.mo.
		load_theme_textdomain( 'wecodeart', $paths['directory'] . $lang );

		// Loads wp-content/languages/themes/wecodeart-it_IT.mo.
		load_theme_textdomain( 'wecodeart', trailingslashit( WP_LANG_DIR ) . 'themes' );
	}

	/**
	 * Manage Notices
	 *
	 * @since 	5.5.5
	 * @version	6.1.2
	 */
	public function manage_notice() {
		$translated = [ 'en_US', 'ro_RO', 'nl_BE' ];

		$current_lang = get_user_locale();

		if( in_array( $current_lang, $translated ) ) {
			return;
		}

		require_once ABSPATH . 'wp-admin/includes/translation-install.php';
		$translations = wp_get_available_translations();

		if ( $translation = get_prop( $translations, $current_lang, [] ) ) {
			$translation = [
				'code'	=> $translation['language'],
				'name' 	=> $translation['native_name'],
				'lang'	=> current( $translation['iso'] ), 	
			];
		}

		if( empty( $translation ) ) {
			return;
		}

		$language_link 	= sprintf( '//translate.wordpress.org/projects/wp-themes/wecodeart/%s/default/', get_prop( $translation, [ 'lang' ] ) );
		
		$data = [
			'language' 	=> (object) $translation,
			'wp_link'	=> $language_link,
		];

		$notification = new Notification( wecodeart_template( [ 'admin/notification', 'locale' ], $data, false ), [
			'id'			=> self::NOTICE_ID,
			'type'     		=> Notification::INFO,
			'priority' 		=> 1,
			'class'			=> 'notice is-dismissible',
			'capabilities' 	=> 'switch_themes',
		] );
		
		if( get_user_option( self::NOTICE_ID ) === 'seen' ) {
			Notifications::get_instance()->remove_notification( $notification );
			set_transient( self::NOTICE_ID, true, WEEK_IN_SECONDS );
			return;
		}

		if( get_transient( self::NOTICE_ID ) === false ) {
			Notifications::get_instance()->add_notification( $notification );
			set_transient( self::NOTICE_ID, true, WEEK_IN_SECONDS );
		}
	}
}