<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Blade;
use Illuminate\View\ComponentAttributeBag;
use Livewire\Livewire;

class BaseServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     */
    public function register(): void
    {
        Blade::componentNamespace('App\\View\\Ui', 'ui');
        Blade::componentNamespace('App\\View\\Page', 'page');
        Blade::componentNamespace('App\\View\\Form', 'form');

        ComponentAttributeBag::macro('mergeWhen', function ($condition, $key, $attributes) {
            /** @var ComponentAttributeBag */
            $self = $this;
            return $condition ? $self->merge([$key => $attributes]) : $self;
        });

        Livewire::resolveMissingComponent(function ($name) {
            'modules.dashboard.components.dashboard-component';
            if (str($name)->startsWith('modules.')) {
                return str($name)->explode('.')->map(fn ($name) => str($name)->studly()->toString())->implode('\\');
            }

            return null;
        });
    }

    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
    }
}
