//go:build ignore

// This program generates hashes.go. It can be invoked by running
// go generate.
package main

import (
	"log"
	"os"
	"strconv"
	"text/template"

	"git.marketally.com/tutus-one/tutus-chain/pkg/core/native/nativenames"
)

// srcTmpl is a nativeids package template.
const srcTmpl = `// Code generated by "go generate go run gen.go"; DO NOT EDIT.

//go:generate go run gen.go

// package nativeids contains IDs of all native contracts.
package nativeids

// IDs of all native contracts.
const (
{{- range .Natives }}
	// {{ .Name }} is an ID of native {{ .Name }} contract.
	{{ .Name }} int32 = {{ .ID }}
{{- end }}
)
`

type (
	// Config contains parameters for the nativehashes package generation.
	Config struct {
		Natives []NativeInfo
	}

	// NativeInfo contains information about native contract needed for
	// nativeids package generation.
	NativeInfo struct {
		Name string
		ID   string
	}
)

// srcTemplate is a parsed nativehashes package template.
var srcTemplate = template.Must(template.New("nativeids").Parse(srcTmpl))

func main() {
	f, err := os.Create("ids.go")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	var cfg = Config{Natives: make([]NativeInfo, len(nativenames.All))}
	for i, name := range nativenames.All {
		cfg.Natives[i] = NativeInfo{
			Name: name,
			ID:   strconv.Itoa(-i - 1),
		}
	}

	srcTemplate.Execute(f, cfg)
}
