<?php

namespace Plainwork\Ui\Component;

class Menu extends AbstractTag
{
    private array $items = [];
    private bool $vertical = false;
    public $collapsibleLabel = null;

    // public function __construct(array $items = [])
    // {
        // foreach ($items as $item) {
            // $this->add($item);
        // }
    // }

    public function __construct(...$items)
    {
        if ((1 === count($items)) && is_array(current($items))){
            $items = current($items);
        }
        foreach ($items as $item) {
            $this->add($item);
        }
    }

    public function setCollapsible($label): self
    {
        $this->collapsibleLabel = $label;

        return $this;
    }

    public function hasItems(): bool
    {
        return $this->items ? true : false;
    }

    public function add($item, ?int $weight = null)
    {
        if (null === $weight) {
            $weight = count($this->items) + 1;
        }

        return $this->addWithOrder($item, $weight);
    }

    public function addWithOrder($item, int $weight): self
    {
        $this->items[] = [$weight, $item];

        return $this;
    }

    public function getItems(): array
    {
        $ret = $this->items;
        usort($ret, fn($a, $b) => ($a[0] - $b[0]));
        $ret = array_column($ret, 1);

        return $ret;
    }

    public function isVertical(): bool
    {
        return $this->vertical;
    }

    public function setVertical(bool $v = true): self
    {
        $this->vertical = $v;

        return $this;
    }

    public function setHorizontal(bool $v = true): self
    {
        $this->vertical = !$v;

        return $this;
    }
}
