<?php

/**
 * Async toast stack — global container for both server-rendered flash
 * messages and JS-triggered notifications via showAsyncFlash().
 *
 * Mounted once at the bottom of the shell. Empty unless flash messages
 * are pending, in which case they render inline and the auto-dismiss
 * component (data-app-component="flash-auto-dismiss") fades them out.
 */

use MintyPHP\Http\Request;
use MintyPHP\Session;

$messages = is_array($appFlashMessages ?? null) ? $appFlashMessages : [];
$timeouts = [
    'success' => 5000,
    'info' => 6000,
    'warning' => 7000,
    'error' => 0,
];
$variantIcons = [
    'success' => 'bi-check-circle-fill',
    'error' => 'bi-x-octagon-fill',
    'warning' => 'bi-exclamation-triangle-fill',
    'info' => 'bi-info-circle-fill',
];
$returnTarget = Request::pathWithQuery();

?>
<div class="app-toast-stack" data-app-component="flash-auto-dismiss" aria-live="polite">
  <?php foreach ($messages as $message): ?>
    <?php
    $type = (string) ($message['type'] ?? 'info');
    $timeout = $timeouts[$type] ?? 5000;
    $isAssertive = $type === 'error' || $type === 'warning';
    $iconClass = $variantIcons[$type] ?? $variantIcons['info'];
    ?>
    <div class="notice" data-variant="<?php e($type); ?>"
      data-flash-timeout="<?php e((string) $timeout); ?>"
      role="<?php echo $isAssertive ? 'alert' : 'status'; // raw-html-ok: hardcoded ARIA role values ?>">
      <span class="notice-icon" aria-hidden="true">
        <i class="bi <?php e($iconClass); ?>"></i>
      </span>
      <span class="notice-content"><?php e(t($message['message'] ?? '')); ?></span>
      <form method="post" action="flash/dismiss/<?php e((string) ($message['id'] ?? '')); ?>" class="notice-dismiss-form">
        <input type="hidden" name="return" value="<?php e($returnTarget); ?>">
        <button type="submit" class="notice-dismiss" aria-label="<?php e(t('Dismiss')); ?>">
          <i class="bi bi-x-lg"></i>
        </button>
        <?php Session::getCsrfInput(); ?>
      </form>
    </div>
  <?php endforeach; ?>
</div>
