<?php

    use ncc\Classes\Runtime;
    use ncc\Enums\Versions;
    use ncc\Exceptions\ConfigurationException;
    use ncc\ncc;

    if(!defined('NCC_INIT'))
    {
        if(!file_exists('%ncc_install' . DIRECTORY_SEPARATOR . 'autoload.php'))
        {
            throw new RuntimeException('Cannot locate file \'%ncc_install' . DIRECTORY_SEPARATOR . 'autoload.php\'');
        }

        require('%ncc_install' . DIRECTORY_SEPARATOR . 'autoload.php');

        if(!function_exists('import'))
        {
            /**
             * Attempts to import a package into the current runtime, returns the imported package name
             *
             * @param string $package
             * @param string $version
             * @param array $options
             * @return string
             */
            function import(string $package, string $version=Versions::LATEST->value, array $options=[]): string
            {
                try
                {
                    return Runtime::import($package, $version, $options);
                }
                catch(Exception $e)
                {
                    throw new RuntimeException(sprintf('Unable to import package \'%s\': %s', $package, $e->getMessage()), $e->getCode(), $e);
                }
            }
        }

        if(!function_exists('execute'))
        {
            /**
             * Executes the main execution point of an imported package and returns the evaluated result
             * This method may exit the program without returning a value
             *
             * @param string $package
             * @return mixed
             * @throws ConfigurationException
             */
            function execute(string $package): mixed
            {
                return Runtime::execute($package);
            }
        }

        if(!function_exists('get_imported'))
        {
            /**
             * Returns an array of all imported packages
             *
             * @return array
             */
            function get_imported(): array
            {
                return Runtime::getImportedPackages();
            }
        }

        if(!function_exists('ncc_constants'))
        {
            /**
             * Returns an array of constants defined by NCC
             *
             * @return array
             */
            function ncc_constants(): array
            {
                return ncc::getConstants();
            }
        }
    }