<?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\Themes
 * @copyright   Copyright (c) 2025, WeCodeArt Framework
 * @since 		6.1.2
 * @version		6.3.7
 */

namespace WeCodeArt\Support;

defined( 'ABSPATH' ) || exit;

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

/**
 * Themes
 */
class Themes implements Integration {

	use Singleton;
	use Asset;
	use No_Conditionals;

	const NOTICE_ID = 'wecodeart/themes/notice';

	/**
	 * Hooks
	 *
	 * @return  void
	 */
	public function register_hooks() {
		add_action( 'admin_enqueue_scripts', 	[ $this, 'enqueue_assets' 	] );
		add_action( 'admin_notices', 			[ $this, 'manage_notice' 	] );
	}

	/**
	 * Load assets for option page.
	 *
	 * @since   6.1.2
	 * @version	6.1.2
	 */
	public function enqueue_assets() {
		if( ! wecodeart_if( 'is_theme_admin' ) ) {
			return;
		}

		$version = wecodeart( 'version' );

		wp_enqueue_style(
			$this->make_handle(),
			$this->get_asset( 'css', 'admin/themes' ),
			[ 'wecodeart-admin' ],
			$version
		);

		wp_enqueue_script(
			$this->make_handle(),
			$this->get_asset( 'js', 'admin/themes' ),
			[ 'wecodeart-admin' ],
			$version,
			true
		);

		$installers = get_prop( wecodeart_config( 'installers' ), [ 'themes' ], [] );

		usort( $installers, function ( $a, $b ) {
			return ( $a['slug'] === null ) <=> ( $b['slug'] === null );
		} );

		wp_localize_script( $this->make_handle(), 'wecodeartThemes', [
			'installed'		=> (array) array_keys( wp_get_themes() ),
			'installers' 	=> (array) $installers,
			'child'			=> is_child_theme() ? get_stylesheet() : false,
		] );

		wp_set_script_translations( $this->make_handle(), 'wecodeart', wecodeart_config( 'directories' )['languages'] );
	}

	/**
	 * Manage Notice
	 *
	 * @since 	6.1.2
	 * @version	6.1.2
	 */
	public function manage_notice() {
		$template = wecodeart_template( [ 'admin/notification', 'theme' ], [
			'link' => admin_url( '/themes.php?page=wecodeart&tab=themes#manager' )
		], false );

		$notification = new Notification( $template, [
			'id'			=> self::NOTICE_ID,
			'type'     		=> Notification::WARNING,
			'priority' 		=> 1,
			'class'			=> 'notice is-dismissible',
			'capabilities' 	=> 'switch_themes',
		] );

		if( get_user_option( self::NOTICE_ID ) === 'seen' || is_child_theme() || wecodeart_if( 'is_theme_admin' ) ) {
			set_transient( self::NOTICE_ID, true, DAY_IN_SECONDS );
			Notifications::get_instance()->remove_notification( $notification );
			return;
		}
		
		if( get_transient( self::NOTICE_ID ) === false ) {
			set_transient( self::NOTICE_ID, true, DAY_IN_SECONDS );
			Notifications::get_instance()->add_notification( $notification );
		}
	}
}