<?php

namespace App\Repositories\Core\User;

class Basket
{
    public static function first($key)
    {
        $data = self::get($key);

        return array_shift($data);
    }

    public static function last($key)
    {
        $data = self::get($key);

        return $data ? array_pop($data) : false;
    }

    public static function set($key, $data)
    {
        return session([$key => $data]);
    }

    public static function get($key)
    {
        return session($key);
    }

    public static function add($key, $value)
    {
        $data = self::isExist($key) ? self::get($key) : [];
        if (array_search($value, $data) === false) {
            array_push($data, $value);
            self::set($key, $data);
        }

        return count($data);
    }

    public static function remove($key, $value)
    {
        $data = self::get($key);
        $index = array_search($value, $data);
        if ($index !== false) {
            unset($data[$index]);
        }

        return self::set($key, $data);
    }

    public static function isExist($key)
    {
        return session()->has($key);
    }

    public static function reset($key)
    {
        return session()->forget($key);
    }

    public static function resetAll()
    {
        return session()->flush();
    }
}
