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

// dict-type.go
package types

import (
	"git.portale-stac.it/go-pkg/expr/kern"
	"git.portale-stac.it/go-pkg/expr/types/dict"
)

func ContextToDict(ctx kern.ExprContext) (dt *dict.DictType) {
	var keys []string
	// Variables
	keys = ctx.EnumVars(nil)
	vars := dict.MakeDict()
	for _, key := range keys {
		value, _ := ctx.GetVar(key)
		vars.SetItem(key, value)
	}

	// Functions
	keys = ctx.EnumFuncs(func(name string) bool { return true })
	funcs := dict.MakeDict()
	for _, key := range keys {
		funcInfo, _ := ctx.GetFuncInfo(key)
		funcs.SetItem(key, funcInfo)
	}

	dt = dict.MakeDict()
	dt.SetItem("vars", vars)
	dt.SetItem("funcs", funcs)
	return
}

func ContextToString(ctx kern.ExprContext, opt kern.FmtOpt) string {
	if dict, ok := ctx.ToDict().(*dict.DictType); ok {
		return dict.ToString(opt)
	}
	return ""
}
