<?php

namespace App\Providers;

use App\Data\Validation\NetteValidator;
use App\Data\Validation\ValidatesArray;
use App\Services\CorsService;
use Illuminate\Support\ServiceProvider;
use Nette\Schema\Expect;
use Nette\Schema\Schema;

class CorsServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->when(CorsService::class)
            ->needs(ValidatesArray::class)
            ->give(fn () => static::determineConfigValidator());
    }

    private static function determineConfigValidator(): ValidatesArray
    {
        return new NetteValidator(
            new \Nette\Schema\Processor(),
            static::getConfigValidationSchema(),
        );
    }

    private static function getConfigValidationSchema(): Schema
    {
        return Expect::structure([
            'path' => Expect::string(),
            'origin' => Expect::string()->nullable(),
            'methods' => Expect::arrayOf(Expect::string()),
            'headers' => Expect::arrayOf(Expect::string()),
        ]);
    }
}
