<?php

use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Illuminate\Support\Collection;

/**
 * @var int        $book_size
 * @var int        $generations
 * @var Individual $individual
 * @var bool       $show_spouse
 */

$children = $individual->spouseFamilies()->map(static function (Family $family): Collection {
    return $family->children();
})->flatten();

?>

<h3><?= I18N::translate('Family of %s', $individual->fullName()) ?></h3>

<div class="d-flex wt-chart-family-book">
    <?= view('modules/family-book-chart/children', ['children' => $children, 'generations' => $book_size - 1]) ?>
    <div class="align-self-center">
        <?= view('chart-box', ['individual' => $individual]) ?>
        <?php if ($show_spouse): ?>
            <?php foreach ($individual->spouseFamilies() as $family): ?>
                <?= view('chart-box', ['individual' => $family->spouse($individual)]) ?>
            <?php endforeach ?>
        <?php endif ?>
    </div>
    <div class="wt-chart-horizontal-spacer">
        <div class="wt-chart-horizontal-line"></div>
    </div>
    <?= view('modules/family-book-chart/parents', ['generations' => $book_size - 1, 'family' => $individual->primaryChildFamily()]) ?>
</div>

<?php if ($generations > 1): ?>
    <?php foreach ($children as $child): ?>
        <hr class="wt-family-separator">
        <?= view('modules/family-book-chart/chart', ['individual' => $child, 'generations' => $generations - 1, 'book_size' => $book_size, 'show_spouse' => $show_spouse]) ?>
    <?php endforeach ?>
<?php endif ?>
