// Code generated by go-enum DO NOT EDIT.
// Version:
// Revision:
// Build Date:
// Built By:

package model

import (
	"database/sql/driver"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"strings"
)

const (
	FacetLogicAnd FacetLogic = "and"
	FacetLogicOr  FacetLogic = "or"
)

var ErrInvalidFacetLogic = fmt.Errorf("not a valid FacetLogic, try [%s]", strings.Join(_FacetLogicNames, ", "))

var _FacetLogicNames = []string{
	string(FacetLogicAnd),
	string(FacetLogicOr),
}

// FacetLogicNames returns a list of possible string values of FacetLogic.
func FacetLogicNames() []string {
	tmp := make([]string, len(_FacetLogicNames))
	copy(tmp, _FacetLogicNames)
	return tmp
}

// FacetLogicValues returns a list of the values for FacetLogic
func FacetLogicValues() []FacetLogic {
	return []FacetLogic{
		FacetLogicAnd,
		FacetLogicOr,
	}
}

// String implements the Stringer interface.
func (x FacetLogic) String() string {
	return string(x)
}

// IsValid provides a quick way to determine if the typed value is
// part of the allowed enumerated values
func (x FacetLogic) IsValid() bool {
	_, err := ParseFacetLogic(string(x))
	return err == nil
}

var _FacetLogicValue = map[string]FacetLogic{
	"and": FacetLogicAnd,
	"or":  FacetLogicOr,
}

// ParseFacetLogic attempts to convert a string to a FacetLogic.
func ParseFacetLogic(name string) (FacetLogic, error) {
	if x, ok := _FacetLogicValue[name]; ok {
		return x, nil
	}
	// Case insensitive parse, do a separate lookup to prevent unnecessary cost of lowercasing a string if we don't need to.
	if x, ok := _FacetLogicValue[strings.ToLower(name)]; ok {
		return x, nil
	}
	return FacetLogic(""), fmt.Errorf("%s is %w", name, ErrInvalidFacetLogic)
}

// MarshalText implements the text marshaller method.
func (x FacetLogic) MarshalText() ([]byte, error) {
	return []byte(string(x)), nil
}

// UnmarshalText implements the text unmarshaller method.
func (x *FacetLogic) UnmarshalText(text []byte) error {
	tmp, err := ParseFacetLogic(string(text))
	if err != nil {
		return err
	}
	*x = tmp
	return nil
}

var errFacetLogicNilPtr = errors.New("value pointer is nil") // one per type for package clashes

// Scan implements the Scanner interface.
func (x *FacetLogic) Scan(value interface{}) (err error) {
	if value == nil {
		*x = FacetLogic("")
		return
	}

	// A wider range of scannable types.
	// driver.Value values at the top of the list for expediency
	switch v := value.(type) {
	case string:
		*x, err = ParseFacetLogic(v)
	case []byte:
		*x, err = ParseFacetLogic(string(v))
	case FacetLogic:
		*x = v
	case *FacetLogic:
		if v == nil {
			return errFacetLogicNilPtr
		}
		*x = *v
	case *string:
		if v == nil {
			return errFacetLogicNilPtr
		}
		*x, err = ParseFacetLogic(*v)
	default:
		return errors.New("invalid type for FacetLogic")
	}

	return
}

// Value implements the driver Valuer interface.
func (x FacetLogic) Value() (driver.Value, error) {
	return x.String(), nil
}

type NullFacetLogic struct {
	FacetLogic FacetLogic
	Valid      bool
	Set        bool
}

func NewNullFacetLogic(val interface{}) (x NullFacetLogic) {
	err := x.Scan(val) // yes, we ignore this error, it will just be an invalid value.
	_ = err            // make any errcheck linters happy
	return
}

// Scan implements the Scanner interface.
func (x *NullFacetLogic) Scan(value interface{}) (err error) {
	if value == nil {
		x.FacetLogic, x.Valid = FacetLogic(""), false
		return
	}

	err = x.FacetLogic.Scan(value)
	x.Valid = (err == nil)
	return
}

// Value implements the driver Valuer interface.
func (x NullFacetLogic) Value() (driver.Value, error) {
	if !x.Valid {
		return nil, nil
	}
	return x.FacetLogic.String(), nil
}

// MarshalJSON correctly serializes a NullFacetLogic to JSON.
func (n NullFacetLogic) MarshalJSON() ([]byte, error) {
	const nullStr = "null"
	if n.Valid {
		return json.Marshal(n.FacetLogic)
	}
	return []byte(nullStr), nil
}

// UnmarshalJSON correctly deserializes a NullFacetLogic from JSON.
func (n *NullFacetLogic) UnmarshalJSON(b []byte) error {
	n.Set = true
	var x interface{}
	err := json.Unmarshal(b, &x)
	if err != nil {
		return err
	}
	err = n.Scan(x)
	return err
}

// MarshalGQL correctly serializes a NullFacetLogic to GraphQL.
func (n NullFacetLogic) MarshalGQL(w io.Writer) {
	bytes, err := json.Marshal(n)
	if err == nil {
		_, _ = w.Write(bytes)
	}
}

// UnmarshalGQL correctly deserializes a NullFacetLogic from GraphQL.
func (n *NullFacetLogic) UnmarshalGQL(v any) error {
	if v == nil {
		return nil
	}
	str, ok := v.(string)
	if !ok {
		return errors.New("value is not a string")
	}
	if str == "null" {
		return nil
	}
	return n.UnmarshalJSON([]byte("\"" + str + "\""))
}
