<?php

declare(strict_types=1);

namespace App\Http\Api\Scope;

use Illuminate\Support\Str;

class ScopeParser
{
    /**
     * Parse scope instance from string.
     */
    public static function parse(string $scope): Scope
    {
        if (blank($scope)) {
            return new GlobalScope();
        }

        if (Str::contains($scope, '.')) {
            return new RelationScope(
                Str::of($scope)
                    ->explode('.')
                    ->map(fn (string $scopePart) => Str::singular($scopePart))
                    ->join('.')
            );
        }

        return new TypeScope(Str::singular($scope));
    }
}
