<?php

declare(strict_types=1);

namespace Plugin\tnt_stabila_shop\src\Helper;

final class ShopRuntime
{
    public static function shopUrl(): string
    {
        return self::constantString('URL_SHOP');
    }

    public static function applicationVersion(): string
    {
        return self::constantString('APPLICATION_VERSION');
    }

    public static function applicationBuildSha(): string
    {
        return self::constantString('APPLICATION_BUILD_SHA');
    }

    public static function timezone(): \DateTimeZone|null
    {
        $timezone = self::constantString('SHOP_TIMEZONE');
        if ($timezone === '') {
            return null;
        }

        try {
            return new \DateTimeZone($timezone);
        } catch (\DateException) {
            return null;
        }
    }

    private static function constantString(string $name): string
    {
        if (\defined($name) === false) {
            return '';
        }

        $value = \constant($name);

        return \is_scalar($value) ? (string)$value : '';
    }
}
