<?php

use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Module\IndividualListModule;
use Fisharebest\Webtrees\Module\ModuleListInterface;
use Fisharebest\Webtrees\Tree;

/**
 * @var IndividualListModule|null          $module
 * @var array<string,array<string,string>> $surnames
 * @var bool                               $totals
 * @var Tree                               $tree
 */

$maximum = max(array_map(static fn (array $x): int => max($x), $surnames));
$minimum = min(array_map(static fn (array $x): int => min($x), $surnames));

?>

<div class="wt-surnames-column-list px-3" style="columns: 15rem; column-rule: solid thin gray; border: solid thin gray;">
    <?php foreach ($surnames as $surn => $surns) : ?>
        <?php foreach ($surns as $spfxsurn => $count) : ?>
            <?php
            $size     = 1.0 + ($maximum === $minimum ? 0 : 1.5 * ($count - $minimum) / ($maximum - $minimum));
            $label    = $spfxsurn ? e($spfxsurn) : I18N::translate('No surname');

            if ($totals) {
                $item = I18N::translate('%1$s (%2$s)', '<bdi>' . $label . '</bdi>', I18N::number($count));
            } else {
                $item = '<bdi>' . $label . '</bdi>';
            }
            ?>
            <div class="wt-surnames-column-list-item">
                <?php if ($module instanceof ModuleListInterface) : ?>
                    <a href="<?= e($module->listUrl($tree, ['surname' => $surn])) ?>">
                        <?= $item ?>
                    </a>
                <?php else : ?>
                    <?= $item ?>
                <?php endif ?>
            </div>
        <?php endforeach ?>
    <?php endforeach ?>
</div>
