<?php

namespace MasterAddons\Modules\Utilities;

use \Elementor\Controls_Manager;
use \MasterAddons\Inc\Classes\Helper;

/**
 * Author Name: Liton Arefin
 * Author URL: https://jeweltheme.com
 * Date: 6/5/2021
 */

if (!defined('ABSPATH')) {
    exit;
} // Exit if accessed directly.

if (!class_exists('MasterAddons\Modules\Utilities\IconsExtended')) {
class IconsExtended
{

    /*
	* Instance of this class
	*/
    private static $instance = null;

    public function __construct()
    {
        // Add new Icons to Icons Manager
        add_filter('elementor/icons_manager/additional_tabs', [$this, 'jltma_add_icons_manager_tab'], 100, 3);
    }

    // Add Section Controls
    public function jltma_add_icons_manager_tab($tabs)
    {
        // Adds Icons Library options
        // Ensure proper URL formatting with trailing slash
        $assets_url = trailingslashit(JLTMA_URL) . 'assets/';

        $tabs['elementor-icons'] = [
            'name'          => 'elementor-icons',
            'label'         => __('Elementor Icons', 'master-addons' ),
            'prefix'        => 'eicon-',
            'displayPrefix' => 'elementor-icons',
            'labelIcon'     => 'jltma-icon jltma-icon-ma elementor-icons eicon eicon-elementor-circle jltma-font-manager',
            'ver'           => JLTMA_VER,
            'fetchJson'     => $assets_url . 'fonts/elementor-icon/elementor-icons.js?v=' . JLTMA_VER,
            'native'        => true,
        ];

        $tabs['simple-line-icons'] = [
            'name'          => 'simple-line-icons',
            'label'         => __('Simple Line Icons', 'master-addons' ),
            'url'           => $assets_url . 'fonts/simple-line-icons/simple-line-icons.css',
            'enqueue'       => [$assets_url . 'fonts/simple-line-icons/simple-line-icons.css'],
            'prefix'        => 'icon-',
            'displayPrefix' => 'simple-line-icons',
            'labelIcon'     => 'jltma-icon jltma-icon-ma simple-line-icons icon-heart jltma-font-manager',
            'ver'           => JLTMA_VER,
            'fetchJson'     => $assets_url . 'fonts/simple-line-icons/simple-line-icons.js?v=' . JLTMA_VER,
            'native'        => false,
        ];

        $tabs['iconic-fonts'] = [
            'name'          => 'iconic-fonts',
            'label'         => __('Iconic Font Icons', 'master-addons' ),
            'url'           => $assets_url . 'fonts/iconic-fonts/iconic-font.min.css',
            'enqueue'       => [$assets_url . 'fonts/iconic-fonts/iconic-font.min.css'],
            'prefix'        => 'im-',
            'displayPrefix' => 'im',
            'labelIcon'     => 'jltma-icon jltma-icon-ma iconic-fonts im im-flag jltma-font-manager',
            'ver'           => JLTMA_VER,
            'fetchJson'     => $assets_url . 'fonts/iconic-fonts/iconic-fonts.js?v=' . JLTMA_VER,
            'native'        => false,
        ];

        $tabs['linear-icons'] = [
            'name'          => 'linear-icons',
            'label'         => __('Linear Icons', 'master-addons' ),
            'url'           => $assets_url . 'fonts/linear-icons/linear-icons.css',
            'enqueue'       => [$assets_url . 'fonts/linear-icons/linear-icons.css'],
            'prefix'        => 'lnr-',
            'displayPrefix' => 'lnr',
            'labelIcon'     => 'jltma-icon jltma-icon-ma linear-icons lnr lnr-flag jltma-font-manager',
            'ver'           => JLTMA_VER,
            'fetchJson'     => $assets_url . 'fonts/linear-icons/linear-icons.js?v=' . JLTMA_VER,
            'native'        => false,
        ];

        $tabs['material-icons'] = [
            'name'          => 'material-icons',
            'label'         => __('Material Icons', 'master-addons' ),
            'url'           => $assets_url . 'fonts/material-icons/material-icons.css',
            'enqueue'       => [$assets_url . 'fonts/material-icons/material-icons.css'],
            'prefix'        => 'jltma-material-icon-',
            'displayPrefix' => 'jltma-material-icon',
            'labelIcon'     => 'jltma-icon jltma-icon-ma material-icons jltma-material-icon-flag jltma-font-manager',
            'ver'           => JLTMA_VER,
            'fetchJson'     => $assets_url . 'fonts/material-icons/material-icons.js?v=' . JLTMA_VER,
            'native'        => false,
        ];


        // Pro Icon Libraries - filtered through Pro_Modules.php
        $pro_icons = apply_filters('master_addons/modules/icons_extended/pro_icons', [], $assets_url);
        foreach ($pro_icons as $key => $icon_config) {
            $tabs[$key] = $icon_config;
        }

        // Check Enabled Icons Libraries - only filter MA's own icon tabs
        $jltma_get_icons_library_settings = \MasterAddons\Inc\Admin\Settings\Settings::get_icons() ?: [];
        $jltma_icon_keys = [
            'elementor-icons', 'simple-line-icons', 'iconic-fonts',
            'linear-icons', 'material-icons',
        ];
        // Include pro icon keys if any
        foreach ($pro_icons as $key => $icon_config) {
            $jltma_icon_keys[] = $key;
        }

        // Remove disabled MA icon libraries, but keep all other tabs intact
        foreach ($jltma_icon_keys as $key) {
            if (!isset($jltma_get_icons_library_settings[$key]) || $jltma_get_icons_library_settings[$key] != 1) {
                unset($tabs[$key]);
            }
        }

        return $tabs;
    }


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

if (class_exists('MasterAddons\Modules\Utilities\IconsExtended')) {
    IconsExtended::get_instance();
}
