// Code generated by neo-go contract generate-rpcwrapper --manifest <file.json> --out <file.go> [--hash <hash>] [--config <config>]; DO NOT EDIT.

// Package types contains RPC wrappers for Types contract.
package types

import (
	"errors"
	"fmt"
	"git.marketally.com/tutus-one/tutus-chain/pkg/crypto/keys"
	"git.marketally.com/tutus-one/tutus-chain/pkg/rpcclient/unwrap"
	"git.marketally.com/tutus-one/tutus-chain/pkg/smartcontract"
	"git.marketally.com/tutus-one/tutus-chain/pkg/tutusrpc/result"
	"git.marketally.com/tutus-one/tutus-chain/pkg/util"
	"git.marketally.com/tutus-one/tutus-chain/pkg/vm/stackitem"
	"math/big"
	"unicode/utf8"
)

// Hash contains contract hash.
var Hash = util.Uint160{0x33, 0x22, 0x11, 0x0, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x0}

// Unnamed is a contract-specific unnamed type used by its methods.
type Unnamed struct {
	I *big.Int
}

// UnnamedX is a contract-specific unnamedX type used by its methods.
type UnnamedX struct {
	I *big.Int
	B bool
}

// Invoker is used by ContractReader to call various safe methods.
type Invoker interface {
	Call(contract util.Uint160, operation string, params ...any) (*result.Invoke, error)
}

// ContractReader implements safe contract methods.
type ContractReader struct {
	invoker Invoker
	hash    util.Uint160
}

// NewReader creates an instance of ContractReader using Hash and the given Invoker.
func NewReader(invoker Invoker) *ContractReader {
	var hash = Hash
	return &ContractReader{invoker, hash}
}

// AAAStrings invokes `aAAStrings` method of contract.
func (c *ContractReader) AAAStrings(s [][][]string) ([][][]string, error) {
	return func(item stackitem.Item, err error) ([][][]string, error) {
		if err != nil {
			return nil, err
		}
		return func(item stackitem.Item) ([][][]string, error) {
			arr, ok := item.Value().([]stackitem.Item)
			if !ok {
				return nil, errors.New("not an array")
			}
			res := make([][][]string, len(arr))
			for i := range res {
				res[i], err = func(item stackitem.Item) ([][]string, error) {
					arr, ok := item.Value().([]stackitem.Item)
					if !ok {
						return nil, errors.New("not an array")
					}
					res := make([][]string, len(arr))
					for i := range res {
						res[i], err = func(item stackitem.Item) ([]string, error) {
							arr, ok := item.Value().([]stackitem.Item)
							if !ok {
								return nil, errors.New("not an array")
							}
							res := make([]string, len(arr))
							for i := range res {
								res[i], err = func(item stackitem.Item) (string, error) {
									b, err := item.TryBytes()
									if err != nil {
										return "", err
									}
									if !utf8.Valid(b) {
										return "", errors.New("not a UTF-8 string")
									}
									return string(b), nil
								}(arr[i])
								if err != nil {
									return nil, fmt.Errorf("item %d: %w", i, err)
								}
							}
							return res, nil
						}(arr[i])
						if err != nil {
							return nil, fmt.Errorf("item %d: %w", i, err)
						}
					}
					return res, nil
				}(arr[i])
				if err != nil {
					return nil, fmt.Errorf("item %d: %w", i, err)
				}
			}
			return res, nil
		}(item)
	}(unwrap.Item(c.invoker.Call(c.hash, "aAAStrings", s)))
}

// Any invokes `any` method of contract.
func (c *ContractReader) Any(a any) (any, error) {
	return func(item stackitem.Item, err error) (any, error) {
		if err != nil {
			return nil, err
		}
		return item.Value(), error(nil)
	}(unwrap.Item(c.invoker.Call(c.hash, "any", a)))
}

// AnyMaps invokes `anyMaps` method of contract.
func (c *ContractReader) AnyMaps(m map[*big.Int]any) (map[*big.Int]any, error) {
	return func(item stackitem.Item, err error) (map[*big.Int]any, error) {
		if err != nil {
			return nil, err
		}
		return func(item stackitem.Item) (map[*big.Int]any, error) {
			m, ok := item.Value().([]stackitem.MapElement)
			if !ok {
				return nil, fmt.Errorf("%s is not a map", item.Type().String())
			}
			res := make(map[*big.Int]any)
			for i := range m {
				k, err := m[i].Key.TryInteger()
				if err != nil {
					return nil, fmt.Errorf("key %d: %w", i, err)
				}
				v, err := m[i].Value.Value(), error(nil)
				if err != nil {
					return nil, fmt.Errorf("value %d: %w", i, err)
				}
				res[k] = v
			}
			return res, nil
		}(item)
	}(unwrap.Item(c.invoker.Call(c.hash, "anyMaps", m)))
}

// Bool invokes `bool` method of contract.
func (c *ContractReader) Bool(b bool) (bool, error) {
	return unwrap.Bool(c.invoker.Call(c.hash, "bool", b))
}

// Bools invokes `bools` method of contract.
func (c *ContractReader) Bools(b []bool) ([]bool, error) {
	return unwrap.ArrayOfBools(c.invoker.Call(c.hash, "bools", b))
}

// Bytes invokes `bytes` method of contract.
func (c *ContractReader) Bytes(b []byte) ([]byte, error) {
	return unwrap.Bytes(c.invoker.Call(c.hash, "bytes", b))
}

// Bytess invokes `bytess` method of contract.
func (c *ContractReader) Bytess(b [][]byte) ([][]byte, error) {
	return unwrap.ArrayOfBytes(c.invoker.Call(c.hash, "bytess", b))
}

// CrazyMaps invokes `crazyMaps` method of contract.
func (c *ContractReader) CrazyMaps(m map[*big.Int][]map[string][]util.Uint160) (map[*big.Int][]map[string][]util.Uint160, error) {
	return func(item stackitem.Item, err error) (map[*big.Int][]map[string][]util.Uint160, error) {
		if err != nil {
			return nil, err
		}
		return func(item stackitem.Item) (map[*big.Int][]map[string][]util.Uint160, error) {
			m, ok := item.Value().([]stackitem.MapElement)
			if !ok {
				return nil, fmt.Errorf("%s is not a map", item.Type().String())
			}
			res := make(map[*big.Int][]map[string][]util.Uint160)
			for i := range m {
				k, err := m[i].Key.TryInteger()
				if err != nil {
					return nil, fmt.Errorf("key %d: %w", i, err)
				}
				v, err := func(item stackitem.Item) ([]map[string][]util.Uint160, error) {
					arr, ok := item.Value().([]stackitem.Item)
					if !ok {
						return nil, errors.New("not an array")
					}
					res := make([]map[string][]util.Uint160, len(arr))
					for i := range res {
						res[i], err = func(item stackitem.Item) (map[string][]util.Uint160, error) {
							m, ok := item.Value().([]stackitem.MapElement)
							if !ok {
								return nil, fmt.Errorf("%s is not a map", item.Type().String())
							}
							res := make(map[string][]util.Uint160)
							for i := range m {
								k, err := func(item stackitem.Item) (string, error) {
									b, err := item.TryBytes()
									if err != nil {
										return "", err
									}
									if !utf8.Valid(b) {
										return "", errors.New("not a UTF-8 string")
									}
									return string(b), nil
								}(m[i].Key)
								if err != nil {
									return nil, fmt.Errorf("key %d: %w", i, err)
								}
								v, err := func(item stackitem.Item) ([]util.Uint160, error) {
									arr, ok := item.Value().([]stackitem.Item)
									if !ok {
										return nil, errors.New("not an array")
									}
									res := make([]util.Uint160, len(arr))
									for i := range res {
										res[i], err = func(item stackitem.Item) (util.Uint160, error) {
											b, err := item.TryBytes()
											if err != nil {
												return util.Uint160{}, err
											}
											u, err := util.Uint160DecodeBytesBE(b)
											if err != nil {
												return util.Uint160{}, err
											}
											return u, nil
										}(arr[i])
										if err != nil {
											return nil, fmt.Errorf("item %d: %w", i, err)
										}
									}
									return res, nil
								}(m[i].Value)
								if err != nil {
									return nil, fmt.Errorf("value %d: %w", i, err)
								}
								res[k] = v
							}
							return res, nil
						}(arr[i])
						if err != nil {
							return nil, fmt.Errorf("item %d: %w", i, err)
						}
					}
					return res, nil
				}(m[i].Value)
				if err != nil {
					return nil, fmt.Errorf("value %d: %w", i, err)
				}
				res[k] = v
			}
			return res, nil
		}(item)
	}(unwrap.Item(c.invoker.Call(c.hash, "crazyMaps", m)))
}

// Hash160 invokes `hash160` method of contract.
func (c *ContractReader) Hash160(h util.Uint160) (util.Uint160, error) {
	return unwrap.Uint160(c.invoker.Call(c.hash, "hash160", h))
}

// Hash160s invokes `hash160s` method of contract.
func (c *ContractReader) Hash160s(h []util.Uint160) ([]util.Uint160, error) {
	return unwrap.ArrayOfUint160(c.invoker.Call(c.hash, "hash160s", h))
}

// Hash256 invokes `hash256` method of contract.
func (c *ContractReader) Hash256(h util.Uint256) (util.Uint256, error) {
	return unwrap.Uint256(c.invoker.Call(c.hash, "hash256", h))
}

// Hash256s invokes `hash256s` method of contract.
func (c *ContractReader) Hash256s(h []util.Uint256) ([]util.Uint256, error) {
	return unwrap.ArrayOfUint256(c.invoker.Call(c.hash, "hash256s", h))
}

// Int invokes `int` method of contract.
func (c *ContractReader) Int(i *big.Int) (*big.Int, error) {
	return unwrap.BigInt(c.invoker.Call(c.hash, "int", i))
}

// Ints invokes `ints` method of contract.
func (c *ContractReader) Ints(i []*big.Int) ([]*big.Int, error) {
	return unwrap.ArrayOfBigInts(c.invoker.Call(c.hash, "ints", i))
}

// Maps invokes `maps` method of contract.
func (c *ContractReader) Maps(m map[string]string) (map[string]string, error) {
	return func(item stackitem.Item, err error) (map[string]string, error) {
		if err != nil {
			return nil, err
		}
		return func(item stackitem.Item) (map[string]string, error) {
			m, ok := item.Value().([]stackitem.MapElement)
			if !ok {
				return nil, fmt.Errorf("%s is not a map", item.Type().String())
			}
			res := make(map[string]string)
			for i := range m {
				k, err := func(item stackitem.Item) (string, error) {
					b, err := item.TryBytes()
					if err != nil {
						return "", err
					}
					if !utf8.Valid(b) {
						return "", errors.New("not a UTF-8 string")
					}
					return string(b), nil
				}(m[i].Key)
				if err != nil {
					return nil, fmt.Errorf("key %d: %w", i, err)
				}
				v, err := func(item stackitem.Item) (string, error) {
					b, err := item.TryBytes()
					if err != nil {
						return "", err
					}
					if !utf8.Valid(b) {
						return "", errors.New("not a UTF-8 string")
					}
					return string(b), nil
				}(m[i].Value)
				if err != nil {
					return nil, fmt.Errorf("value %d: %w", i, err)
				}
				res[k] = v
			}
			return res, nil
		}(item)
	}(unwrap.Item(c.invoker.Call(c.hash, "maps", m)))
}

// PublicKey invokes `publicKey` method of contract.
func (c *ContractReader) PublicKey(k *keys.PublicKey) (*keys.PublicKey, error) {
	return unwrap.PublicKey(c.invoker.Call(c.hash, "publicKey", k))
}

// PublicKeys invokes `publicKeys` method of contract.
func (c *ContractReader) PublicKeys(k keys.PublicKeys) (keys.PublicKeys, error) {
	return unwrap.ArrayOfPublicKeys(c.invoker.Call(c.hash, "publicKeys", k))
}

// Signature invokes `signature` method of contract.
func (c *ContractReader) Signature(s []byte) ([]byte, error) {
	return unwrap.Bytes(c.invoker.Call(c.hash, "signature", s))
}

// Signatures invokes `signatures` method of contract.
func (c *ContractReader) Signatures(s [][]byte) ([][]byte, error) {
	return unwrap.ArrayOfBytes(c.invoker.Call(c.hash, "signatures", s))
}

// String invokes `string` method of contract.
func (c *ContractReader) String(s string) (string, error) {
	return unwrap.UTF8String(c.invoker.Call(c.hash, "string", s))
}

// Strings invokes `strings` method of contract.
func (c *ContractReader) Strings(s []string) ([]string, error) {
	return unwrap.ArrayOfUTF8Strings(c.invoker.Call(c.hash, "strings", s))
}

// UnnamedStructs invokes `unnamedStructs` method of contract.
func (c *ContractReader) UnnamedStructs() (*Unnamed, error) {
	return itemToUnnamed(unwrap.Item(c.invoker.Call(c.hash, "unnamedStructs")))
}

// UnnamedStructsX invokes `unnamedStructsX` method of contract.
func (c *ContractReader) UnnamedStructsX() (*UnnamedX, error) {
	return itemToUnnamedX(unwrap.Item(c.invoker.Call(c.hash, "unnamedStructsX")))
}

// itemToUnnamed converts stack item into *Unnamed.
// NULL item is returned as nil pointer without error.
func itemToUnnamed(item stackitem.Item, err error) (*Unnamed, error) {
	if err != nil {
		return nil, err
	}
	_, null := item.(stackitem.Null)
	if null {
		return nil, nil
	}
	var res = new(Unnamed)
	err = res.FromStackItem(item)
	return res, err
}

// Ensure *Unnamed is a proper [stackitem.Convertible].
var _ = stackitem.Convertible(&Unnamed{})

// Ensure *Unnamed is a proper [smartcontract.Convertible].
var _ = smartcontract.Convertible(&Unnamed{})

// FromStackItem retrieves fields of Unnamed from the given
// [stackitem.Item] or returns an error if it's not possible to do to so.
// It implements [stackitem.Convertible] interface.
func (res *Unnamed) FromStackItem(item stackitem.Item) error {
	arr, ok := item.Value().([]stackitem.Item)
	if !ok {
		return errors.New("not an array")
	}
	if len(arr) != 1 {
		return errors.New("wrong number of structure elements")
	}

	var (
		index = -1
		err   error
	)
	index++
	res.I, err = arr[index].TryInteger()
	if err != nil {
		return fmt.Errorf("field I: %w", err)
	}

	return nil
}

// ToStackItem creates [stackitem.Item] representing Unnamed.
// It implements [stackitem.Convertible] interface.
func (res *Unnamed) ToStackItem() (stackitem.Item, error) {
	if res == nil {
		return stackitem.Null{}, nil
	}

	var (
		err   error
		itm   stackitem.Item
		items = make([]stackitem.Item, 0, 1)
	)
	itm, err = (*stackitem.BigInteger)(res.I), error(nil)
	if err != nil {
		return nil, fmt.Errorf("field I: %w", err)
	}
	items = append(items, itm)

	return stackitem.NewStruct(items), nil
}

// ToSCParameter creates [smartcontract.Parameter] representing Unnamed.
// It implements [smartcontract.Convertible] interface so that Unnamed
// could be used with invokers.
func (res *Unnamed) ToSCParameter() (smartcontract.Parameter, error) {
	if res == nil {
		return smartcontract.Parameter{Type: smartcontract.AnyType}, nil
	}

	var (
		err  error
		prm  smartcontract.Parameter
		prms = make([]smartcontract.Parameter, 0, 1)
	)
	prm, err = smartcontract.NewParameterFromValue(res.I)
	if err != nil {
		return smartcontract.Parameter{}, fmt.Errorf("field I: %w", err)
	}
	prms = append(prms, prm)

	return smartcontract.Parameter{Type: smartcontract.ArrayType, Value: prms}, nil
}

// itemToUnnamedX converts stack item into *UnnamedX.
// NULL item is returned as nil pointer without error.
func itemToUnnamedX(item stackitem.Item, err error) (*UnnamedX, error) {
	if err != nil {
		return nil, err
	}
	_, null := item.(stackitem.Null)
	if null {
		return nil, nil
	}
	var res = new(UnnamedX)
	err = res.FromStackItem(item)
	return res, err
}

// Ensure *UnnamedX is a proper [stackitem.Convertible].
var _ = stackitem.Convertible(&UnnamedX{})

// Ensure *UnnamedX is a proper [smartcontract.Convertible].
var _ = smartcontract.Convertible(&UnnamedX{})

// FromStackItem retrieves fields of UnnamedX from the given
// [stackitem.Item] or returns an error if it's not possible to do to so.
// It implements [stackitem.Convertible] interface.
func (res *UnnamedX) FromStackItem(item stackitem.Item) error {
	arr, ok := item.Value().([]stackitem.Item)
	if !ok {
		return errors.New("not an array")
	}
	if len(arr) != 2 {
		return errors.New("wrong number of structure elements")
	}

	var (
		index = -1
		err   error
	)
	index++
	res.I, err = arr[index].TryInteger()
	if err != nil {
		return fmt.Errorf("field I: %w", err)
	}

	index++
	res.B, err = arr[index].TryBool()
	if err != nil {
		return fmt.Errorf("field B: %w", err)
	}

	return nil
}

// ToStackItem creates [stackitem.Item] representing UnnamedX.
// It implements [stackitem.Convertible] interface.
func (res *UnnamedX) ToStackItem() (stackitem.Item, error) {
	if res == nil {
		return stackitem.Null{}, nil
	}

	var (
		err   error
		itm   stackitem.Item
		items = make([]stackitem.Item, 0, 2)
	)
	itm, err = (*stackitem.BigInteger)(res.I), error(nil)
	if err != nil {
		return nil, fmt.Errorf("field I: %w", err)
	}
	items = append(items, itm)

	itm, err = stackitem.NewBool(res.B), error(nil)
	if err != nil {
		return nil, fmt.Errorf("field B: %w", err)
	}
	items = append(items, itm)

	return stackitem.NewStruct(items), nil
}

// ToSCParameter creates [smartcontract.Parameter] representing UnnamedX.
// It implements [smartcontract.Convertible] interface so that UnnamedX
// could be used with invokers.
func (res *UnnamedX) ToSCParameter() (smartcontract.Parameter, error) {
	if res == nil {
		return smartcontract.Parameter{Type: smartcontract.AnyType}, nil
	}

	var (
		err  error
		prm  smartcontract.Parameter
		prms = make([]smartcontract.Parameter, 0, 2)
	)
	prm, err = smartcontract.NewParameterFromValue(res.I)
	if err != nil {
		return smartcontract.Parameter{}, fmt.Errorf("field I: %w", err)
	}
	prms = append(prms, prm)

	prm, err = smartcontract.NewParameterFromValue(res.B)
	if err != nil {
		return smartcontract.Parameter{}, fmt.Errorf("field B: %w", err)
	}
	prms = append(prms, prm)

	return smartcontract.Parameter{Type: smartcontract.ArrayType, Value: prms}, nil
}
