<?php

if (!function_exists("env")) {
  function env(string $key, mixed $default = null): mixed
  {
    return $_ENV[$key] ?? $default;
  }
}

function formatDateTimeWib(?string $datetime): string
{
  if (!$datetime) {
    return '-';
  }

  return (new DateTime($datetime, new DateTimeZone('UTC')))
    ->setTimezone(new DateTimeZone('Asia/Jakarta'))
    ->format('d M Y H:i:s');
}


// env("APP_NAME");

// asset("css/style.css");

// url("/dashboard");

// redirect("/login");

// dd($data);

function normalize(mixed $value): mixed
{
  if (is_array($value)) {
    return array_map('normalize', $value);
  }

  if ($value instanceof UnitEnum) {
    return $value->value;
  }

  if (is_object($value)) {
    $reflection = new ReflectionObject($value);
    $result = [];

    foreach ($reflection->getProperties() as $property) {
      $property->setAccessible(true);
      $result[$property->getName()] = normalize($property->getValue($value));
    }

    return $result;
  }

  return $value;
}

function dd(mixed ...$vars): never
{
  echo '<pre>';
  echo json_encode(
    normalize(count($vars) === 1 ? $vars[0] : $vars),
    JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
  );
  echo '</pre>';

  exit;
}

// config("database.host");

// old("username");

// hours_ago
function hours_ago(string $date): string
{
  // string to date
  $date = new DateTime($date);

  return $date->diff(new DateTime())->format('%h jam %i menit');
}
