<?php

namespace App\Observers;

use App\Models\MenuItem;

class MenuItemObserver
{
    public function saved(MenuItem $item): void
    {
        $this->flushParent($item);
    }

    public function deleted(MenuItem $item): void
    {
        $this->flushParent($item);
    }

    private function flushParent(MenuItem $item): void
    {
        $menu = $item->menu()->first();
        if ($menu) {
            app(MenuObserver::class)->flush($menu);
        }
    }
}
