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

// simple-var-store.go
package expr

import "fmt"

type SimpleVarStore struct {
	varStore map[string]any
}

func NewSimpleVarStore() *SimpleVarStore {
	return &SimpleVarStore{
		varStore: make(map[string]any),
	}
}

func (ctx *SimpleVarStore) Clone() (clone ExprContext) {
	clone = &SimpleVarStore{
		varStore: CloneMap(ctx.varStore),
	}
	return clone
}

func (ctx *SimpleVarStore) GetVar(varName string) (v any, exists bool) {
	v, exists = ctx.varStore[varName]
	return
}

func (ctx *SimpleVarStore) setVar(varName string, value any) {
	ctx.varStore[varName] = value
}

func (ctx *SimpleVarStore) SetVar(varName string, value any) {
	if allowedValue, ok := fromGenericAny(value); ok {
		ctx.varStore[varName] = allowedValue
	} else {
		panic(fmt.Errorf("unsupported type %T of value %v", value, value))
	}
}

func (ctx *SimpleVarStore) EnumVars(acceptor func(name string) (accept bool)) (varNames []string) {
	varNames = make([]string, 0)
	for name := range ctx.varStore {
		if acceptor != nil {
			if acceptor(name) {
				varNames = append(varNames, name)
			}
		} else {
			varNames = append(varNames, name)
		}
	}
	return
}

func (ctx *SimpleVarStore) GetFuncInfo(name string) (f ExprFunc, exists bool) {
	return
}

func (ctx *SimpleVarStore) Call(name string, args []any) (result any, err error) {
	return
}

func (ctx *SimpleVarStore) RegisterFunc(name string, functor Functor, minArgs, maxArgs int) {
}

func (ctx *SimpleVarStore) EnumFuncs(acceptor func(name string) (accept bool)) (funcNames []string) {
	return
}