<?php
/**
 * BookiFlex Core
 *
 * @package    BookiFlex
 * @subpackage Core
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */


declare(strict_types=1);
namespace BookiFlex\Core\Provider;

use DateTimeZone;

defined( 'ABSPATH' ) || exit;

class LocalTimeZone
{
    public static function get(): DateTimeZone
    {
        try {
            $timezone = get_option('timezone_string');

            if ($timezone) {
                return new DateTimeZone($timezone);
            }

            // If timezone_string is not set, we use an offset (gmt_offset)
            $offset    = (float) get_option('gmt_offset');
            $hours     = (int) $offset;
            $minutes   = ($offset - $hours) * 60;
            $offsetStr = sprintf('%+03d:%02d', $hours, abs($minutes));

            return new DateTimeZone($offsetStr);
        } catch (\Throwable $e) {
            throw new \InvalidArgumentException($e->getMessage());
        }
    }
}
