<?php

/**
 * Shared details-aside action list.
 *
 * @var array<int, array<string, mixed>> $asideActions
 */

use MintyPHP\Session;

$asideActions = is_array($asideActions ?? null) ? $asideActions : [];
if (!$asideActions) {
    return;
}
$appendCssClass = static function (string $baseClass, string $appendClass): string {
    $combined = trim($baseClass . ' ' . $appendClass);
    return preg_replace('/\s+/', ' ', $combined) ?? $combined;
};
$resolveToneClass = static function (string $tone, string $detailActionKind, string $name, string $value): string {
    $normalizedTone = strtolower(trim($tone));
    if ($normalizedTone === 'success') {
        return 'app-action-success';
    }
    if ($normalizedTone === 'danger') {
        return 'app-action-danger';
    }
    if ($normalizedTone === 'neutral') {
        return '';
    }

    $normalizedActionKind = strtolower(trim($detailActionKind));
    if (in_array($normalizedActionKind, ['delete', 'danger', 'purge', 'revoke'], true)) {
        return 'app-action-danger';
    }
    if (in_array($normalizedActionKind, ['save', 'save-close', 'create', 'create-close'], true)) {
        return 'app-action-success';
    }

    $normalizedName = strtolower(trim($name));
    $normalizedValue = strtolower(trim($value));
    if (
        $normalizedName === 'action'
        && in_array($normalizedValue, ['create', 'create_close', 'save', 'save_close'], true)
    ) {
        return 'app-action-success';
    }

    return '';
};
?>

<details name="details-actions">
  <summary><?php e(t('Actions')); ?></summary>
  <hr>
  <div class="app-details-aside-actions">
    <?php foreach ($asideActions as $action): ?>
      <?php
      $item = is_array($action) ? $action : [];
      $itemType = (string) ($item['type'] ?? 'link');
      $itemLabel = (string) ($item['label'] ?? '');
      if ($itemLabel === '') {
        continue;
      }
      ?>
      <?php if ($itemType === 'form'): ?>
        <?php
        $itemMethod = strtoupper((string) ($item['method'] ?? 'POST'));
        $itemAction = (string) ($item['action'] ?? '');
        $itemConfirm = (string) ($item['confirm'] ?? '');
        $itemConfirmTitle = (string) ($item['confirmTitle'] ?? '');
        $itemConfirmConfirmLabel = (string) ($item['confirmConfirmLabel'] ?? '');
        $itemConfirmCancelLabel = (string) ($item['confirmCancelLabel'] ?? '');
        $itemConfirmVariant = strtolower(trim((string) ($item['confirmVariant'] ?? '')));
        $itemConfirmFocus = strtolower(trim((string) ($item['confirmFocus'] ?? '')));
        $itemButtonClass = (string) ($item['class'] ?? 'secondary outline small');
        $itemNeedsCsrf = !array_key_exists('csrf', $item) ? true : !empty($item['csrf']);
        $itemTarget = (string) ($item['target'] ?? '');
        $itemDetailActionKind = strtolower(trim((string) ($item['detailActionKind'] ?? '')));
        $itemDetailSubmitLock = strtolower(trim((string) ($item['detailSubmitLock'] ?? '')));
        $itemTone = (string) ($item['tone'] ?? '');
        $itemName = (string) ($item['name'] ?? '');
        $itemValue = (string) ($item['value'] ?? '');
        $itemFields = is_array($item['fields'] ?? null) ? $item['fields'] : [];
        $itemButtonClass = $appendCssClass(
          $itemButtonClass,
          $resolveToneClass($itemTone, $itemDetailActionKind, $itemName, $itemValue)
        );
        ?>
        <?php if ($itemAction !== ''): ?>
          <form
            method="<?php e($itemMethod); ?>"
            action="<?php e($itemAction); ?>"
            <?php if ($itemTarget !== ''): ?>target="<?php e($itemTarget); ?>"<?php endif; ?>
            <?php if ($itemConfirm !== ''): ?>data-detail-confirm-message="<?php e($itemConfirm); ?>"<?php endif; ?>
            <?php if ($itemConfirmTitle !== ''): ?>data-detail-confirm-title="<?php e($itemConfirmTitle); ?>"<?php endif; ?>
            <?php if ($itemConfirmConfirmLabel !== ''): ?>data-detail-confirm-confirm-label="<?php e($itemConfirmConfirmLabel); ?>"<?php endif; ?>
            <?php if ($itemConfirmCancelLabel !== ''): ?>data-detail-confirm-cancel-label="<?php e($itemConfirmCancelLabel); ?>"<?php endif; ?>
            <?php if ($itemConfirmVariant !== ''): ?>data-detail-confirm-variant="<?php e($itemConfirmVariant); ?>"<?php endif; ?>
            <?php if ($itemConfirmFocus !== ''): ?>data-detail-confirm-focus="<?php e($itemConfirmFocus); ?>"<?php endif; ?>
            <?php if ($itemDetailActionKind !== ''): ?>data-detail-action-kind="<?php e($itemDetailActionKind); ?>"<?php endif; ?>
            <?php if ($itemDetailSubmitLock !== ''): ?>data-detail-submit-lock="<?php e($itemDetailSubmitLock); ?>"<?php endif; ?>
          >
            <?php if ($itemNeedsCsrf && $itemMethod !== 'GET'): ?>
              <?php Session::getCsrfInput(); ?>
            <?php endif; ?>
            <?php foreach ($itemFields as $fieldName => $fieldValue): ?>
              <?php if ($fieldName !== ''): ?>
                <input type="hidden" name="<?php e((string) $fieldName); ?>" value="<?php e((string) $fieldValue); ?>">
              <?php endif; ?>
            <?php endforeach; ?>
            <button type="submit" class="<?php e($itemButtonClass); ?>">
              <?php e($itemLabel); ?>
            </button>
          </form>
        <?php endif; ?>
      <?php else: ?>
        <?php
        $itemHref = (string) ($item['href'] ?? '');
        $itemLinkClass = (string) ($item['class'] ?? 'secondary outline small');
        $itemTarget = (string) ($item['target'] ?? '');
        $itemRel = (string) ($item['rel'] ?? '');
        $itemTone = (string) ($item['tone'] ?? '');
        $itemDetailActionKind = strtolower(trim((string) ($item['detailActionKind'] ?? '')));
        $itemName = (string) ($item['name'] ?? '');
        $itemValue = (string) ($item['value'] ?? '');
        $itemLinkClass = $appendCssClass(
          $itemLinkClass,
          $resolveToneClass($itemTone, $itemDetailActionKind, $itemName, $itemValue)
        );
        ?>
        <?php if ($itemHref !== ''): ?>
          <a
            role="button"
            class="<?php e($itemLinkClass); ?>"
            href="<?php e($itemHref); ?>"
            <?php if ($itemTarget !== ''): ?>target="<?php e($itemTarget); ?>"<?php endif; ?>
            <?php if ($itemRel !== ''): ?>rel="<?php e($itemRel); ?>"<?php endif; ?>
          >
            <?php e($itemLabel); ?>
          </a>
        <?php endif; ?>
      <?php endif; ?>
    <?php endforeach; ?>
  </div>
</details>
<hr>
