// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.

// control.go
package expr

import "strings"

// Preset control variables
const (
	ControlLastResult   = "last"
	ControlBoolShortcut = "_bool_shortcut"
	ControlImportPath   = "_import_path"
)

// Other control variables
const (
	control_export_all = "_export_all"
)

// Initial values
const (
	init_import_path = "~/.local/lib/go-pkg/expr:/usr/local/lib/go-pkg/expr:/usr/lib/go-pkg/expr"
)

func initDefaultVars(ctx ExprContext) {
	ctx.SetVar(ControlBoolShortcut, true)
	ctx.SetVar(ControlImportPath, init_import_path)
}

func enable(ctx ExprContext, name string) {
	if strings.HasPrefix(name, "_") {
		ctx.SetVar(name, true)
	} else {
		ctx.SetVar("_"+name, true)
	}
}

func disable(ctx ExprContext, name string) {
	if strings.HasPrefix(name, "_") {
		ctx.SetVar(name, false)
	} else {
		ctx.SetVar("_"+name, false)
	}
}

func isEnabled(ctx ExprContext, name string) (status bool) {
	if v, exists := ctx.GetVar(name); exists {
		if b, ok := v.(bool); ok {
			status = b
		}
	}
	return
}

func getControlString(ctx ExprContext, name string) (s string, exists bool) {
	var v any
	if v, exists = ctx.GetVar(name); exists {
		s, exists = v.(string)
	}
	return
}