<?php
namespace Elementor_Icon_Vault;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Registers the bundled icon libraries with Elementor's native Icon Manager
 * via the official `elementor/icons_manager/additional_tabs` filter, so the
 * icons appear inside every Elementor widget's icon picker and inherit all
 * existing color, size, and hover controls automatically.
 */
class Icon_Library_Loader {

	private static $instance = null;

	/**
	 * All bundled libraries, keyed by their unique tab id.
	 * Each library is a free, open-source, redistributable icon font.
	 */
	public static function get_libraries() {
		return array(
			'themify-icons'      => array(
				'label'         => __( 'Themify Icons', 'icon-vault-for-elementor' ),
				'css'           => 'themify-icons.css',
				'json'          => 'themify-icons.json',
				'prefix'        => 'ti-',
				'displayPrefix' => 'ti',
				'labelIcon'     => 'ti ti-wand',
			),
			'line-awesome-solid' => array(
				'label'         => __( 'Line Awesome - Solid', 'icon-vault-for-elementor' ),
				'css'           => 'line-awesome.css',
				'json'          => 'line-awesome-solid.json',
				'prefix'        => 'la-',
				'displayPrefix' => 'las',
				'labelIcon'     => 'las la-heart',
			),
			'line-awesome-regular' => array(
				'label'         => __( 'Line Awesome - Regular', 'icon-vault-for-elementor' ),
				'css'           => 'line-awesome.css',
				'json'          => 'line-awesome-regular.json',
				'prefix'        => 'la-',
				'displayPrefix' => 'lar',
				'labelIcon'     => 'lar la-heart',
			),
			'line-awesome-brands' => array(
				'label'         => __( 'Line Awesome - Brands', 'icon-vault-for-elementor' ),
				'css'           => 'line-awesome.css',
				'json'          => 'line-awesome-brands.json',
				'prefix'        => 'la-',
				'displayPrefix' => 'lab',
				'labelIcon'     => 'lab la-wordpress',
			),
		);
	}

	public static function instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	private function __construct() {
		add_filter( 'elementor/icons_manager/additional_tabs', array( $this, 'register_icon_tabs' ) );
	}

	/**
	 * Whether a given library is currently enabled in plugin settings.
	 * Libraries are enabled by default unless explicitly disabled.
	 */
	private function is_enabled( $library_key ) {
		$settings = get_option( EIV_OPTION_KEY, array() );
		$disabled = isset( $settings['disabled_libraries'] ) ? (array) $settings['disabled_libraries'] : array();
		return ! in_array( $library_key, $disabled, true );
	}

	/**
	 * Add each enabled bundled library as its own tab in Elementor's icon picker.
	 *
	 * @param array $tabs Existing tabs registered by Elementor and other plugins.
	 * @return array
	 */
	public function register_icon_tabs( $tabs ) {

		foreach ( self::get_libraries() as $key => $lib ) {

			if ( ! $this->is_enabled( $key ) ) {
				continue;
			}

			$css_url  = EIV_PLUGIN_URL . 'assets/css/' . $lib['css'];
			$json_url = EIV_PLUGIN_URL . 'assets/js/' . $lib['json'];

			$tabs[ $key ] = array(
				'name'          => $key,
				'label'         => $lib['label'],
				'url'           => $css_url,
				'enqueue'       => array( $css_url ),
				'prefix'        => $lib['prefix'],
				'displayPrefix' => $lib['displayPrefix'],
				'labelIcon'     => $lib['labelIcon'],
				'ver'           => EIV_VERSION,
				'fetchJson'     => $json_url,
				'native'        => false,
			);
		}

		return $tabs;
	}
}
