<?php

namespace Modules\Dashboard;

use Livewire\Livewire;

class DashboardFactory
{
    /**
     * @var array<int, class-string<Block>>
     */
    private array $blocks = [];

    /**
     * @return array<int, Block>
     */
    public function load(): array
    {
        return collect($this->blocks)->map(fn ($block) => app($block))->toArray();
    }

    /**
     * @param class-string<Block> $block
     */
    public function register(string $block): self
    {
        $this->blocks[] = $block;

        $componentName = str($block)->replace('\\', '.')->explode('.')->map(fn ($part) => str($part)->lcfirst()->kebab())->implode('.');
        Livewire::component($componentName, $block);

        return $this;
    }

    public function purge(): self
    {
        $this->blocks = [];

        return $this;
    }
}
