<?php
/**
 * @package   Essentials YOOtheme Pro 2.2.16 build 0813.1608
 * @author    ZOOlanders https://www.zoolanders.com
 * @copyright Copyright (C) Joolanders, SL
 * @license   http://www.gnu.org/licenses/gpl.html GNU/GPL
 */

namespace ZOOlanders\YOOessentials\Listener;

use YOOtheme\Config;
use YOOtheme\Metadata;
use YOOtheme\Path;
use YOOtheme\Url;
use YOOtheme\File;
use ZOOlanders\YOOessentials\Api\MaxMind\GeoIp;

class LoadCustomizerData
{
    public Config $config;
    public Metadata $metadata;
    public GeoIp $geoip;

    public function __construct(Config $config, Metadata $metadata, GeoIp $geoip)
    {
        $this->geoip = $geoip;
        $this->config = $config;
        $this->metadata = $metadata;
    }

    public function handle(): void
    {
        $this->config->set('customizer.yooessentials.geoipcity', $this->geoip->isCityDb());
        $this->config->set('customizer.yooessentials.geoipcountry', $this->geoip->isCountryDb());
        $this->config->set('customizer.yooessentials.requestUrl', Url::route('route'));
        $this->config->addFile('yooessentials.core.fields', Path::get('../../config/fields.json'));

        // hint that and addon exist
        $this->config->set('yooessentials.addons.access', File::exists('~yooessentials/modules/access/bootstrap.php'));
        $this->config->set('yooessentials.addons.source', File::exists('~yooessentials/modules/source/bootstrap.php'));
        $this->config->set('yooessentials.addons.form', File::exists('~yooessentials/modules/form/bootstrap.php'));
        $this->config->set('yooessentials.addons.icon', File::exists('~yooessentials/modules/icon/bootstrap.php'));
        $this->config->set('yooessentials.addons.dynamic', File::exists('~yooessentials/modules/dynamic/bootstrap.php'));
        $this->config->set('yooessentials.addons.layout', File::exists('~yooessentials/modules/layout/bootstrap.php'));
        $this->config->set('yooessentials.addons.element', File::exists('~yooessentials/modules/element/bootstrap.php'));

        // load builder core assets
        $this->metadata->set('script:yooessentials-core', [
            'src' => '~yooessentials_url/modules/core/customizer-ess.min.js',
            'defer' => true,
        ]);
        $this->metadata->set('style:yooessentials-core', [
            'href' => '~yooessentials_url/modules/core/customizer-ess.min.css',
            'defer' => true,
        ]);
    }
}
