<?php

namespace App\Services\Menu\Adapters;

use Illuminate\Support\Facades\Route;

class RouteAdapter implements LinkAdapter
{
    public function resolve(array $linkTarget, string $locale, array $context = []): ?string
    {
        $name = $linkTarget['route'] ?? null;
        if (! $name || ! Route::has($name)) {
            return null;
        }

        // Locale-aware route override — if a `{name}.{locale}` variant exists
        // (e.g. `store.pricing.tr` for the TR slug `/fiyatlar`), prefer it.
        // Lets menu items declare a single canonical route name and still
        // surface the right URL per visitor language.
        $localized = $name . '.' . $locale;
        if (Route::has($localized)) {
            $name = $localized;
        }

        try {
            return route($name, $linkTarget['params'] ?? []);
        } catch (\Throwable $e) {
            return null;
        }
    }

    public function isValid(array $linkTarget): bool
    {
        $name = $linkTarget['route'] ?? null;
        return is_string($name) && Route::has($name);
    }

    public function searchable(): bool
    {
        return false;
    }
}
