<?php

namespace App\Providers;

use App\Facades\FormComponent;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\ComponentAttributeBag;

class FormServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('FormComponent', FormComponent::class);
        $loader = AliasLoader::getInstance();
        $loader->alias('FormComponent', FormComponent::class);
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        Blade::componentNamespace('App\\View\Forms', 'forms');
        $this->bootMacros();
    }

    
    /**
     * @psalm-suppress UndefinedMethod
     */
    private function bootMacros(): void
    {
        ComponentAttributeBag::macro('hasStartsWith', function ($key) {
            return (bool) $this->whereStartsWith($key)->first();
        });
    }
}
