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

namespace WeCodeArt\Support;

defined( 'ABSPATH' ) || exit;

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

/**
 * Plugins
 */
class Plugins implements Integration {

	use Singleton;
	use Asset;

	/**
	 * Get Conditionals
	 *
	 * @return void
	 */
	public static function get_conditionals() {
		return [ 'is_theme_admin' ];
	}

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

	/**
	 * Load assets for option page.
	 *
	 * @since   6.1.2
	 * @version	6.2.9
	 */
	public function enqueue_assets() {
		$version = wecodeart( 'version' );

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

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

		$installers = get_prop( wecodeart_config( 'installers' ), [ 'plugins' ], [] );
		foreach( wecodeart( 'integrations' )->all() as $key => $installer ) {
			$instance = $installer::get_instance();
			
			if( ! method_exists( $instance, 'installer' ) ) {
				continue;
			}

			$installers[] = $instance->installer();
		}

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

		wp_localize_script( $this->make_handle(), 'wecodeartPlugins', apply_filters( 'wecodeart/filter/support/plugins/admin', [
			'installers' 	=> (array) $installers,
			'plugins'		=> self::get_plugins_dir( array_keys( get_plugins() ) ),
			'plugins_active'=> self::get_plugins_dir( array_values( get_option( 'active_plugins' ) ) ),
		] ) );

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

	/**
	 * Get plugin directories.
	 *
	 * @since   6.1.2
	 * @version	6.1.2
	 */
	public static function get_plugins_dir( array $plugins = [] ): array {
		return array_map( 'dirname', $plugins );
	}

	/**
	 * Get default description.
	 *
	 * @since   6.1.2
	 * @version	6.1.2
	 */
	public static function get_default_description( string $name = '', string $type = 'internal' ): string {
		$string = '';

		switch( $type ) :
			
			case 'internal':
			$string = sprintf( esc_html__( 'Our theme comes pre-packaged with built-in support for the %s plugin.', 'wecodeart' ), $name );
			break;

			case 'external':
			$string = sprintf( esc_html__( 'A bridge between WeCodeArt Framework and %s plugin.', 'wecodeart' ), $name );
			break;

		endswitch;

		return $string;
	}
}