<?php
declare(strict_types=1);

function text_has_thai_script(string $text): bool
{
    return preg_match('/\p{Thai}/u', $text) === 1;
}

function text_has_latin_script(string $text): bool
{
    return preg_match('/[A-Za-z]/', $text) === 1;
}

function flash_message_is_mixed_language(string $text): bool
{
    return text_has_thai_script($text) && text_has_latin_script($text);
}

function localized_flash_message_string(string $th, string $en, array $replacements = array()): string
{
    $encoded = json_encode(
        localized_flash_message($th, $en, $replacements),
        JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
    );

    return is_string($encoded) ? '__CCW_FLASH__' . $encoded : $en;
}

function normalize_flash_message_payload(string|array $message): string|array
{
    if (is_array($message)) {
        return $message;
    }

    $message = trim((string) $message);
    if ($message === '') {
        return '';
    }

    if (str_starts_with($message, '__CCW_FLASH__')) {
        $decoded = json_decode(substr($message, strlen('__CCW_FLASH__')), true);
        if (is_array($decoded) && isset($decoded['th'], $decoded['en'])) {
            return $decoded;
        }
    }

    $thaiDictionary = translation_dictionary_for_language('th');
    if (isset($thaiDictionary[$message])) {
        return localized_flash_message((string) $thaiDictionary[$message], $message);
    }

    $englishDictionary = translation_dictionary_for_language('en');
    if (isset($englishDictionary[$message])) {
        return localized_flash_message($message, (string) $englishDictionary[$message]);
    }

    $thaiMessage = translate_text($message, 'th');
    $englishMessage = translate_text($message, 'en');

    if ($thaiMessage === '' || flash_message_is_mixed_language($thaiMessage)) {
        $thaiMessage = $message;
    }

    if ($englishMessage === '' || flash_message_is_mixed_language($englishMessage)) {
        $englishMessage = $message;
    }

    return localized_flash_message($thaiMessage, $englishMessage);
}

function redirect_with_flash(string $path, string $type, string|array $message, array $oldInput = array()): void
{
    $_SESSION['flash'] = array(
        'type' => $type,
        'message' => normalize_flash_message_payload($message),
    );

    if (!empty($oldInput)) {
        $_SESSION['old_input'] = $oldInput;
    } else {
        unset($_SESSION['old_input']);
    }

    redirect($path);
}

function pull_flash(): ?array
{
    if (!isset($_SESSION['flash']) || !is_array($_SESSION['flash'])) {
        return null;
    }

    $flash = $_SESSION['flash'];
    unset($_SESSION['flash']);

    return $flash;
}

function pull_old_input(): array
{
    $oldInput = $_SESSION['old_input'] ?? array();
    unset($_SESSION['old_input']);

    return is_array($oldInput) ? $oldInput : array();
}

function old_value(array $oldInput, string $key, string $default = ''): string
{
    if (!array_key_exists($key, $oldInput)) {
        return $default;
    }

    $value = $oldInput[$key];

    if (is_array($value)) {
        return $default;
    }

    return trim((string) $value);
}

function old_array(array $oldInput, string $key): array
{
    $value = $oldInput[$key] ?? array();

    return is_array($value) ? $value : array();
}
