<?php

namespace App\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Helpers\Lang;

class LangController extends Controller
{
    public function set(Request $request)
    {
        $lang = $request->get('set', 'en');
        Lang::set($lang);
        
        $referer = $_SERVER['HTTP_REFERER'] ?? '/';
        if (str_contains($referer, '/language') || str_contains($referer, '/lang')) $referer = '/';
        return Response::redirect($referer);
    }

    public function setAjax(Request $request)
    {
        $lang = $request->post('lang', 'en');
        Lang::set($lang);
        return Response::json(['ok' => true]);
    }
}
