<?php

namespace App\Livewire;

use Livewire\Component;
use Throwable;

class BaseVolt extends Component
{
    protected function notify(string $type, string $message, string $title = null, int $timeout = 4000)
    {
        $this->dispatch(
            'toast',
            type: $type,
            title: $title ?? ucfirst($type),
            message: $message,
            timeout: $timeout
        );
    }

    protected function success(string $message)
    {
        $this->notify('success', $message);
    }

    protected function error(string $message)
    {
        $this->notify('error', $message, 'Error', 5000);
    }

    protected function warning(string $message)
    {
        $this->notify('warning', $message);
    }

    protected function info(string $message)
    {
        $this->notify('info', $message);
    }

    protected function handleException(Throwable $e)
    {
        report($e);

        $this->error(
            app()->isLocal()
                ? $e->getMessage()
                : 'Something went wrong.'
        );
    }
}
