<?php

namespace Blazemedia\DriveKIntegrator;

class Environment {
        
    public static function init(): void {
        
        self::defineConstants();
    }
   

    /**
     * Definisce le costanti di supporto di base.
     *
     * @return array
     */
    protected static function defineConstants(): void {

        if(!defined('BLAZE_DRIVEK_INTEGRATOR_PATH')) define('BLAZE_DRIVEK_INTEGRATOR_PATH', self::getPluginPath());
        if(!defined('BLAZE_DRIVEK_INTEGRATOR_URI')) define('BLAZE_DRIVEK_INTEGRATOR_URI',  self::getPluginUri());
        if(!defined('BLAZE_DRIVEK_INTEGRATOR_VERSION')) define('BLAZE_DRIVEK_INTEGRATOR_VERSION', '1.0.0');
    }


    /**
     * Restituisce il valore di una costante di ambiente (es. BLZ_API_RENDERING_MANAGER).
     * Se la costante non è definita, restituisce il valore di default.
     *
     * @param string $key
     * @param mixed|null $default
     * @return mixed
     */
    public static function get(string $key, mixed $default = null): mixed {

        if (!defined($key)) return $default;
        
        $json = json_decode(constant($key));

        if (json_last_error() === JSON_ERROR_NONE) {
            return $json;
        }

        return constant($key);
    }


    private static function getPluginPath(): string {

        $path = \plugin_dir_path( __DIR__ );

        return str_replace('src/', '', $path);
    }

    private static function getPluginUri(): string {

        $uri = \plugin_dir_url( __DIR__ );
        
        return str_replace('src/', '', $uri);
    }
}
