<?php

namespace App\Livewire\Components;

use Livewire\Component;

class LanguageSwitcher extends Component
{
    public string $locale;

    public function mount(): void
    {
        $this->locale = session('locale', app()->getLocale());
    }

    public function switchTo(string $locale): void
    {
        if (!in_array($locale, ['ar', 'en'])) {
            return;
        }

        session(['locale' => $locale]);
        app()->setLocale($locale);
        $this->locale = $locale;

        $this->redirect(request()->header('Referer', '/'), navigate: true);
    }

    public function render()
    {
        return view('livewire.components.language-switcher');
    }
}
