<?php

namespace Daylight\Core\Services;

use Illuminate\Support\Collection;

class AdminToolbar
{
    /** @var array<int, array{url: string, label: string, permission: string}> */
    protected array $editLinks = [];

    public function addEditLink(string $url, string $label, string $permission): self
    {
        $this->editLinks[] = [
            'url' => $url,
            'label' => $label,
            'permission' => $permission,
        ];

        return $this;
    }

    /**
     * @return Collection<int, array{url: string, label: string, permission: string}>
     */
    public function getVisibleLinks(): Collection
    {
        $user = auth()->user();

        if (! $user) {
            return collect();
        }

        return collect($this->editLinks)
            ->filter(fn (array $link) => $user->can($link['permission']));
    }

    public function hasLinks(): bool
    {
        return $this->getVisibleLinks()->isNotEmpty();
    }
}
