<?php

namespace SHServ\Middleware;

use \SHServ\Models\Modes;

class ModesContext {

    protected ?Modes $model = null;
    protected bool $synced = false;

    protected function model(): Modes {
        if(!$this -> model) {
            $this -> model = new Modes();
        }

        if(!$this -> synced) {
            $this -> model -> sync_registry_tags();
            $this -> synced = true;
        }

        return $this -> model;
    }

    public function is(String $tag): bool {
        return $this -> model() -> is_active($tag);
    }

    public function any(Array $tags): bool {
        return $this -> model() -> any_active($tags);
    }

    public function all(Array $tags): bool {
        return $this -> model() -> all_active($tags);
    }

    public function active(): Array {
        return $this -> model() -> active_tags();
    }

    public function enable(String $tag): bool {
        return $this -> model() -> enable($tag);
    }

    public function disable(String $tag): bool {
        return $this -> model() -> disable($tag);
    }

    public function toggle(String $tag): bool {
        if($this -> is($tag)) {
            return $this -> disable($tag);
        }

        return $this -> enable($tag);
    }

    public function meta(String $tag): Array {
        $definitions = \ControlScripts\ModesRegistry::definitions();

        return $definitions[$tag] ?? [ "label" => $tag, "description" => "" ];
    }
}
