<?php

declare(strict_types=1);

namespace App\Livewire\Components\SwitchBox;

use Illuminate\Contracts\View\View;
use Nwidart\Modules\Facades\Module as ModuleFacade;

/**
 * Class ModuleStateSwitch
 */
final class ModuleStateSwitch extends SwitchBox
{
    public function mount(?string $moduleName = null): void
    {
        if (is_null($moduleName)) {
            return;
        }

        $this->setProperties($moduleName);
    }

    private function setProperties(string $moduleName): void
    {
        /* @var \Nwidart\Modules\Laravel\Module $module */
        $module = ModuleFacade::findOrFail($moduleName);
        $this->switchId = $module->getLowerName();
        $this->state = $module->isEnabled();
    }

    public function render(): View
    {
        return view('livewire.components.switchbox.switchbox');
    }

    public function changeState(?string $moduleName = null): void
    {
        if (is_null($moduleName)) {
            return;
        }

        $module = ModuleFacade::findOrFail($moduleName);
        $module->isEnabled() ? $module->disable() : $module->enable();
    }
}
