package cmdpacks

import (
	"context"
	"lmika.dev/cmd/dynamo-browse/internal/common/ui/commandctrl"
	"lmika.dev/cmd/dynamo-browse/internal/dynamo-browse/controllers"
	"ucl.lmika.dev/ucl"
)

type optModule struct {
	settingsController *controllers.SettingsController
}

func (m optModule) pbSet(ctx context.Context, args ucl.CallArgs) (any, error) {
	var (
		name    string
		newVale string
	)

	if args.NArgs() == 1 {
		if err := args.Bind(&name); err != nil {
			return nil, err
		}
	} else {
		if err := args.Bind(&name, &newVale); err != nil {
			return nil, err
		}
	}

	commandctrl.PostMsg(ctx, m.settingsController.SetSetting(name, newVale))
	return nil, nil
}

func moduleOpt(
	settingsController *controllers.SettingsController,
) ucl.Module {
	m := &optModule{
		settingsController: settingsController,
	}

	return ucl.Module{
		Name: "opt",
		Builtins: map[string]ucl.BuiltinHandler{
			"set": m.pbSet,
		},
	}
}
