<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;

if (! function_exists('current_route')) {
    /**
     * Retrieve the current route in another locale.
     *
     * @param  string|null  $locale
     * @param  string|null  $fallback
     * @param  bool  $absolute
     * @return string
     */
    function current_route(?string $locale = null, ?string $fallback = null, bool $absolute = true): string
    {
        if (is_null($fallback)) {
            $fallback = url(request()->server('REQUEST_URI'));
        }

        $route = Route::getCurrentRoute();

        if (! $route) {
            return $fallback;
        }

        $name = Str::replaceFirst(
            locale().'.',
            "{$locale}.",
            $route->getName()
        );

        if (! $name || ! in_array($locale, locales()) || ! Route::has($name)) {
            return $fallback;
        }

        return route($name, array_merge(
            (array) $route->parameters,
            (array) request()->getQueryString()
        ), $absolute);
    }
}

if (! function_exists('localized_route')) {
    /**
     * Retrieve a localized route.
     *
     * @param  string  $name
     * @param  mixed  $parameters
     * @param  string|null  $locale
     * @param  bool  $absolute
     * @return string
     */
    function localized_route(string $name, $parameters = [], ?string $locale = null, bool $absolute = true): string
    {
        if (! in_array($locale, locales())) {
            $locale = locale();
        }

        return route("$locale.$name", $parameters, $absolute);
    }
}

if (! function_exists('current_route_is')) {
    /**
     * Check if the current route is the given name.
     *
     * @param  string  $name
     * @return bool Returns true if the current route is the given name, false otherwise.
     */
    function current_route_is($name): bool
    {
        $route = Route::getCurrentRoute();

        if (! $route) {
            return false;
        }

        $current_name = Str::replaceFirst(
            locale().'.',
            '',
            $route->getName()
        );

        return $current_name === $name;
    }
}
