package eapi

import (
	"encoding/json"
	"github.com/spf13/cast"
)

// Var is an universal variable type implementer.
type Var struct {
	value interface{} // Underlying value.
}

// NewVar creates and returns a new Var with given `value`.
func NewVar(value interface{}) *Var {
	return &Var{
		value: value,
	}
}

// Bytes converts and returns `v` as []byte.
func (v *Var) Bytes() ([]byte, error) {
	return json.Marshal(v.value)
}

// String converts and returns `v` as string.
func (v *Var) String() string {
	return cast.ToString(v.value)
}

// Scan .
func (v *Var) Scan(i any) (err error) {
	b, err := v.Bytes()
	if err = json.Unmarshal(b, &i); err != nil {
		return
	}
	return
}
