<?php

namespace modules\subusers\traits;

use modules\subusers\classes\subusers_permission_node;
use modules\subusers\helpers\subusers_permission_node_key;
use modules\subusers\helpers\subusers_permission_type;

trait subusers_permission_nodes_t
{
    /**
     * Permission nodes group name
     * @var string $group_name
     */
    public string $group_name;

    /**
     * Description
     * @var string $description
     */
    public string $description;

    public function __construct(string $group_name, string $description)
    {
        $this->group_name = $group_name;
        $this->description = $description;
    }
    /**
     * @var subusers_permission_node[]
     */
    public array $nodes = [];

    /**
     * @param subusers_permission_node_key $nodeKey
     * @param string $name
     * @param string $description
     * @param subusers_permission_type $type
     * @param bool $value
     * @return void
     */
    public function addNode(subusers_permission_node_key $nodeKey, string $name, string $description, subusers_permission_type $type, bool $value): void
    {
        $this->nodes[$nodeKey->name] = new subusers_permission_node($nodeKey, $name, $description, $type, $value);
    }

    public function getNode(subusers_permission_node_key $nodeKey): ?subusers_permission_node
    {
        return $this->nodes[$nodeKey->name] ?? null;
    }

    public function getNodeByKey(string $nodeKey): ?subusers_permission_node
    {
        return $this->nodes[$nodeKey] ?? null;
    }

    public function getNodeByName(string $nodeKey): ?subusers_permission_node
    {
        return $this->nodes[$nodeKey] ?? null;
    }
}

