package main

import (
	"bytes"
	"flag"
	"os"
	"text/template"
)

const (
	numberOfTypes     = 100
	defaultOutputFile = "tlv_types_generated.go"
)

const typeCodeTemplate = `// Code generated by tlv/internal/gen; DO NOT EDIT.

package tlv

{{- range $index, $element := . }}

type tlvType{{ $index }} struct{}

func (t *tlvType{{ $index }}) TypeVal() Type {
	return {{ $index }}
}

func (t *tlvType{{ $index }}) tlv() {}

type TlvType{{ $index }} = *tlvType{{ $index }}
{{- end }}
`

func main() {
	// Create a slice of items that the template can range over.
	var items []struct{}
	for i := uint16(0); i <= numberOfTypes; i++ {
		items = append(items, struct{}{})
	}

	tpl, err := template.New("tlv").Parse(typeCodeTemplate)
	if err != nil {
		panic(err)
	}

	// Execute the template
	var out bytes.Buffer
	err = tpl.Execute(&out, items)
	if err != nil {
		panic(err)
	}

	outputFile := flag.String(
		"o", defaultOutputFile, "Output file for generated code",
	)
	flag.Parse()

	err = os.WriteFile(*outputFile, out.Bytes(), 0644)
	if err != nil {
		panic(err)
	}
}
