<?php

namespace App\Controllers;

use App\Controllers\BaseController;

/**
 * Class ViewMode
 *
 * This class extends the BaseController class and provides functionality to set the session variable 'view_mode'
 * to the specified view mode and redirect the user to the previous URL.
 */
class ViewMode extends BaseController
{
    /**
     * This method sets the session variable 'view_mode' to the provided $view_mode parameter and redirects
     * the user to the previous URL.
     *
     * @param string $view_mode (optional) The view mode to set. Defaults to VIEW_MODE_IMPRESION.
     * @return \CodeIgniter\HTTP\ResponseInterface|null The redirect response or null if no previous URL is set.
     */
    public function index($view_mode = VIEW_MODE_IMPRESION)
    {
        $session = session();
        $viewMode = $view_mode;
        $session->remove('view_mode');
        $session->set('view_mode', $viewMode);
        $url = previous_url();
        return redirect()->to($url);
    }
}
