// Copyright © 2014 Steve Francia <spf@spf13.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package cast

import (
	
	
	
	
	
	
	
)

var errNegativeNotAllowed = errors.New("unable to cast negative value")

type float64EProvider interface {
	Float64() (float64, error)
}

type float64Provider interface {
	Float64() float64
}

// Number is a type parameter constraint for functions accepting number types.
//
// It represents the supported number types this package can cast to.
type Number interface {
	int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64
}

type integer interface {
	int | int8 | int16 | int32 | int64
}

type unsigned interface {
	uint | uint8 | uint16 | uint32 | uint64
}

type float interface {
	float32 | float64
}

// ToNumberE casts any value to a [Number] type.
func [ Number]( any) (, error) {
	var  

	switch any().(type) {
	case int:
		return toNumberE[](, parseNumber[])
	case int8:
		return toNumberE[](, parseNumber[])
	case int16:
		return toNumberE[](, parseNumber[])
	case int32:
		return toNumberE[](, parseNumber[])
	case int64:
		return toNumberE[](, parseNumber[])
	case uint:
		return toUnsignedNumberE[](, parseNumber[])
	case uint8:
		return toUnsignedNumberE[](, parseNumber[])
	case uint16:
		return toUnsignedNumberE[](, parseNumber[])
	case uint32:
		return toUnsignedNumberE[](, parseNumber[])
	case uint64:
		return toUnsignedNumberE[](, parseNumber[])
	case float32:
		return toNumberE[](, parseNumber[])
	case float64:
		return toNumberE[](, parseNumber[])
	default:
		return 0, fmt.Errorf("unknown number type: %T", )
	}
}

// ToNumber casts any value to a [Number] type.
func [ Number]( any)  {
	,  := ToNumberE[]()

	return 
}

// toNumber's semantics differ from other "to" functions.
// It returns false as the second parameter if the conversion fails.
// This is to signal other callers that they should proceed with their own conversions.
func toNumber[ Number]( any) (, bool) {
	, _ = indirect()

	switch s := .(type) {
	case :
		return , true
	case int:
		return (), true
	case int8:
		return (), true
	case int16:
		return (), true
	case int32:
		return (), true
	case int64:
		return (), true
	case uint:
		return (), true
	case uint8:
		return (), true
	case uint16:
		return (), true
	case uint32:
		return (), true
	case uint64:
		return (), true
	case float32:
		return (), true
	case float64:
		return (), true
	case bool:
		if  {
			return 1, true
		}

		return 0, true
	case nil:
		return 0, true
	case time.Weekday:
		return (), true
	case time.Month:
		return (), true
	}

	return 0, false
}

func toNumberE[ Number]( any,  func(string) (, error)) (, error) {
	,  := toNumber[]()
	if  {
		return , nil
	}

	, _ = indirect()

	switch s := .(type) {
	case string:
		if  == "" {
			return 0, nil
		}

		,  := ()
		if  != nil {
			return 0, fmt.Errorf(errorMsgWith, , , , )
		}

		return , nil
	case json.Number:
		if  == "" {
			return 0, nil
		}

		,  := (string())
		if  != nil {
			return 0, fmt.Errorf(errorMsgWith, , , , )
		}

		return , nil
	case float64EProvider:
		if ,  := any().(float64); ! {
			return 0, fmt.Errorf(errorMsg, , , )
		}

		,  := .Float64()
		if  != nil {
			return 0, fmt.Errorf(errorMsg, , , )
		}

		return (), nil
	case float64Provider:
		if ,  := any().(float64); ! {
			return 0, fmt.Errorf(errorMsg, , , )
		}

		return (.Float64()), nil
	default:
		if ,  := resolveAlias();  {
			return (, )
		}

		return 0, fmt.Errorf(errorMsg, , , )
	}
}

func toUnsignedNumber[ Number]( any) (, bool, bool) {
	, _ = indirect()

	switch s := .(type) {
	case :
		return , true, true
	case int:
		if  < 0 {
			return 0, false, false
		}

		return (), true, true
	case int8:
		if  < 0 {
			return 0, false, false
		}

		return (), true, true
	case int16:
		if  < 0 {
			return 0, false, false
		}

		return (), true, true
	case int32:
		if  < 0 {
			return 0, false, false
		}

		return (), true, true
	case int64:
		if  < 0 {
			return 0, false, false
		}

		return (), true, true
	case uint:
		return (), true, true
	case uint8:
		return (), true, true
	case uint16:
		return (), true, true
	case uint32:
		return (), true, true
	case uint64:
		return (), true, true
	case float32:
		if  < 0 {
			return 0, false, false
		}

		return (), true, true
	case float64:
		if  < 0 {
			return 0, false, false
		}

		return (), true, true
	case bool:
		if  {
			return 1, true, true
		}

		return 0, true, true
	case nil:
		return 0, true, true
	case time.Weekday:
		if  < 0 {
			return 0, false, false
		}

		return (), true, true
	case time.Month:
		if  < 0 {
			return 0, false, false
		}

		return (), true, true
	}

	return 0, true, false
}

func toUnsignedNumberE[ Number]( any,  func(string) (, error)) (, error) {
	, ,  := toUnsignedNumber[]()
	if  {
		return , nil
	}

	, _ = indirect()

	if ! {
		return 0, errNegativeNotAllowed
	}

	switch s := .(type) {
	case string:
		if  == "" {
			return 0, nil
		}

		,  := ()
		if  != nil {
			return 0, fmt.Errorf(errorMsgWith, , , , )
		}

		return , nil
	case json.Number:
		if  == "" {
			return 0, nil
		}

		,  := (string())
		if  != nil {
			return 0, fmt.Errorf(errorMsgWith, , , , )
		}

		return , nil
	case float64EProvider:
		if ,  := any().(float64); ! {
			return 0, fmt.Errorf(errorMsg, , , )
		}

		,  := .Float64()
		if  != nil {
			return 0, fmt.Errorf(errorMsg, , , )
		}

		if  < 0 {
			return 0, errNegativeNotAllowed
		}

		return (), nil
	case float64Provider:
		if ,  := any().(float64); ! {
			return 0, fmt.Errorf(errorMsg, , , )
		}

		 := .Float64()

		if  < 0 {
			return 0, errNegativeNotAllowed
		}

		return (), nil
	default:
		if ,  := resolveAlias();  {
			return (, )
		}

		return 0, fmt.Errorf(errorMsg, , , )
	}
}

func parseNumber[ Number]( string) (, error) {
	var  

	switch any().(type) {
	case int:
		,  := parseInt[int]()

		return (), 
	case int8:
		,  := parseInt[int8]()

		return (), 
	case int16:
		,  := parseInt[int16]()

		return (), 
	case int32:
		,  := parseInt[int32]()

		return (), 
	case int64:
		,  := parseInt[int64]()

		return (), 
	case uint:
		,  := parseUint[uint]()

		return (), 
	case uint8:
		,  := parseUint[uint8]()

		return (), 
	case uint16:
		,  := parseUint[uint16]()

		return (), 
	case uint32:
		,  := parseUint[uint32]()

		return (), 
	case uint64:
		,  := parseUint[uint64]()

		return (), 
	case float32:
		,  := strconv.ParseFloat(, 32)

		return (), 
	case float64:
		,  := strconv.ParseFloat(, 64)

		return (), 

	default:
		return 0, fmt.Errorf("unknown number type: %T", )
	}
}

func parseInt[ integer]( string) (, error) {
	,  := strconv.ParseInt(trimDecimal(), 0, 0)
	if  != nil {
		return 0, 
	}

	return (), nil
}

func parseUint[ unsigned]( string) (, error) {
	,  := strconv.ParseUint(strings.TrimLeft(trimDecimal(), "+"), 0, 0)
	if  != nil {
		return 0, 
	}

	return (), nil
}

func parseFloat[ float]( string) (, error) {
	var  

	var  any
	var  error

	switch any().(type) {
	case float32:
		,  := strconv.ParseFloat(, 32)

		 = float32()
		 = 
	case float64:
		,  := strconv.ParseFloat(, 64)

		 = float64()
		 = 
	}

	return .(), 
}

// ToFloat64E casts an interface to a float64 type.
func ( any) (float64, error) {
	return toNumberE[float64](, parseFloat[float64])
}

// ToFloat32E casts an interface to a float32 type.
func ( any) (float32, error) {
	return toNumberE[float32](, parseFloat[float32])
}

// ToInt64E casts an interface to an int64 type.
func ( any) (int64, error) {
	return toNumberE[int64](, parseInt[int64])
}

// ToInt32E casts an interface to an int32 type.
func ( any) (int32, error) {
	return toNumberE[int32](, parseInt[int32])
}

// ToInt16E casts an interface to an int16 type.
func ( any) (int16, error) {
	return toNumberE[int16](, parseInt[int16])
}

// ToInt8E casts an interface to an int8 type.
func ( any) (int8, error) {
	return toNumberE[int8](, parseInt[int8])
}

// ToIntE casts an interface to an int type.
func ( any) (int, error) {
	return toNumberE[int](, parseInt[int])
}

// ToUintE casts an interface to a uint type.
func ( any) (uint, error) {
	return toUnsignedNumberE[uint](, parseUint[uint])
}

// ToUint64E casts an interface to a uint64 type.
func ( any) (uint64, error) {
	return toUnsignedNumberE[uint64](, parseUint[uint64])
}

// ToUint32E casts an interface to a uint32 type.
func ( any) (uint32, error) {
	return toUnsignedNumberE[uint32](, parseUint[uint32])
}

// ToUint16E casts an interface to a uint16 type.
func ( any) (uint16, error) {
	return toUnsignedNumberE[uint16](, parseUint[uint16])
}

// ToUint8E casts an interface to a uint type.
func ( any) (uint8, error) {
	return toUnsignedNumberE[uint8](, parseUint[uint8])
}

func trimZeroDecimal( string) string {
	var  bool
	for  := len();  > 0; -- {
		switch [-1] {
		case '.':
			if  {
				return [:-1]
			}
		case '0':
			 = true
		default:
			return 
		}
	}
	return 
}

var stringNumberRe = regexp.MustCompile(`^([-+]?\d*)(\.\d*)?$`)

// see [BenchmarkDecimal] for details about the implementation
func trimDecimal( string) string {
	if !strings.Contains(, ".") {
		return 
	}

	 := stringNumberRe.FindStringSubmatch()
	if  != nil {
		// matches[1] is the captured integer part with sign
		 = [1]

		// handle special cases
		switch  {
		case "-", "+":
			 += "0"
		case "":
			 = "0"
		}

		return 
	}

	return 
}