<?php

namespace Botble\Menu\Widgets\Fronts;

use Botble\Base\Forms\FieldOptions\NameFieldOption;
use Botble\Base\Forms\FieldOptions\SelectFieldOption;
use Botble\Base\Forms\Fields\SelectField;
use Botble\Base\Forms\Fields\TextField;
use Botble\Base\Supports\RepositoryHelper;
use Botble\Menu\Models\Menu;
use Botble\Menu\Models\Menu as MenuModel;
use Botble\Widget\AbstractWidget;
use Botble\Widget\Forms\WidgetForm;

class CustomMenu extends AbstractWidget
{
    public function __construct()
    {
        parent::__construct([
            'name' => trans('packages/menu::menu.widget_custom_menu'),
            'description' => trans('packages/menu::menu.widget_custom_menu_description'),
            'menu_id' => null,
        ]);
    }

    protected function settingForm(): WidgetForm|string|null
    {
        $menus = MenuModel::query()
            ->wherePublished();

        $menus = RepositoryHelper::applyBeforeExecuteQuery($menus, new Menu())
            ->pluck('name', 'slug')
            ->all();

        return WidgetForm::createFromArray($this->getConfig())
            ->add('name', TextField::class, NameFieldOption::make())
            ->add(
                'menu_id',
                SelectField::class,
                SelectFieldOption::make()
                    ->label(trans('packages/menu::menu.select_menu'))
                    ->choices($menus)
                    ->searchable()
            );
    }
}
