<?php

declare(strict_types=1);

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

/**
 * @var int        $generations
 * @var Individual $individual
 * @var bool       $spouses
 */

$children = $individual->spouseFamilies()->map(static fn (Family $family): Collection => $family->children())->flatten();

?>

<div class="d-flex wt-chart-hourglass">
    <?= view('modules/hourglass-chart/children', ['children' => $children, 'generations' => $generations - 1, 'spouses' => $spouses]) ?>
    <div class="align-self-center">
        <?= view('chart-box', ['individual' => $individual]) ?>
        <?php if ($spouses) : ?>
            <?php foreach ($individual->spouseFamilies() as $family) : ?>
                <?= view('chart-box', ['individual' => $family->spouse($individual)]) ?>
            <?php endforeach ?>
        <?php endif ?>
    </div>
    <?php if ($individual->childFamilies()->first() !== null) : ?>
        <?= view('modules/hourglass-chart/parents', ['generations' => $generations - 1, 'family' => $individual->childFamilies()->first()]) ?>
    <?php endif ?>
</div>

<?php View::push('javascript') ?>
<script>
    $(".wt-chart-hourglass").on("click", ".hourglass-arrow[data-wt-chart-xref]", function () {
        $(this.parentNode).load(this.dataset.wtChartXref);

        return false;
    });
</script>
<?php View::endpush() ?>
