package errs

import (
	"errors"
	"runtime"
	"runtime/debug"
)

type Error struct {
	Err      error
	Stack    []byte
	Function string
	File     string
	Line     int
}

func (e *Error) Error() string { return e.Err.Error() }
func (e *Error) Unwrap() error { return e.Err }

func throw(err error) {
	if err == nil {
		return
	}
	var existing *Error
	if errors.As(err, &existing) {
		if existing == err {
			panic(existing)
		}
		panic(&Error{
			Err:      err,
			Stack:    existing.Stack,
			Function: existing.Function,
			File:     existing.File,
			Line:     existing.Line,
		})
	}

	e := &Error{
		Err:   err,
		Stack: debug.Stack(),
	}

	pcs := make([]uintptr, 1)
	if n := runtime.Callers(3, pcs); n > 0 {
		frame, _ := runtime.CallersFrames(pcs).Next()
		e.Function = frame.Function
		e.File = frame.File
		e.Line = frame.Line
	}

	panic(e)
}

func Catch(err *error, fn ...func(*error)) {
	r := recover()
	if r == nil {
		return
	}
	e, ok := r.(*Error)
	if !ok {
		panic(r)
	}
	*err = e
	if len(fn) > 0 && fn[0] != nil {
		fn[0](err)
	}
}

func resolve(err error, h []any) error {
	if len(h) == 0 {
		return err
	}
	switch f := h[0].(type) {
	case func() error:
		if f != nil {
			return f()
		}
	case func(error) error:
		if f != nil {
			return f(err)
		}
	}
	return err
}

func TryE(err error, h ...any) {
	if err == nil {
		return
	}
	if e := resolve(err, h); e != nil {
		throw(e)
	}
}

func TryV[T any](v T, err error, h ...any) T {
	if err == nil {
		return v
	}
	if e := resolve(err, h); e != nil {
		throw(e)
	}
	return v
}
