<?php

namespace App\Repositories\Core\Menu;

use Lavary\Menu\Item as LavaryMenuItem;

class Item extends LavaryMenuItem
{
    public function icon($icon)
    {
        $this->prepend(sprintf('<i class="%s"></i>', $icon));

        return $this;
    }

    public function order($order)
    {
        $this->data('order', $order);

        return $this;
    }

    public function activeIfRoute($routes = null)
    {
        if ($routes) {
            if (is_string($routes)) {
                $routes = [$routes];
            }

            foreach ($routes as $pattern) {
                $arr = [$pattern];
                if (! if_route_pattern($arr)) {
                    continue;
                }
                $this->activate();

                if (strstr($this->title, 'circle-o')) {
                    $this->title = str_replace('fa-circle-o', 'fa-dot-circle-o', $this->title);
                }

                return $this;
            }

            return $this;
        }

        $activeClass = $this->builder->conf('active_class');
        $this->attributes['class'] = Builder::formatGroupClass(['class' => $activeClass], $this->attributes);
        $this->isActive = true;

        if (strstr($this->title, 'circle-o')) {
            $this->title = str_replace('fa-circle-o', 'fa-dot-circle-o', $this->title);
        }

        return $this;
    }
}
