package main

import (
	"encoding/base64"
	"flag"
	"io/ioutil"
	"os"
	"text/template"
)

type data struct {
	Locale string
	Data   string
}

// This is a code generator for language pack constructors. Check dicts/en/pack.go for an example
func main() {
	var d data
	var path string
	flag.StringVar(&d.Locale, "locale", "", "The locale abbreviation this language pack is generated for")
	flag.StringVar(&path, "path", "", "The path to where the data file is located")
	flag.Parse()
	b, err := ioutil.ReadFile(path)
	if err != nil {
		panic(err)
	}
	d.Data = base64.StdEncoding.EncodeToString(b)

	t := template.Must(template.New("pack").Parse(packTemplate))
	t.Execute(os.Stdout, d)
}

var packTemplate = `// Code generated by golem/dicts/cmd/generate_pack.go DO NOT EDIT.
package {{.Locale}}

import (
	"bytes"
	"compress/zlib"
	"encoding/base64"
	"io/ioutil"

	"github.com/aaaton/golem/v4"
)

const locale = "{{.Locale}}"

// LanguagePack is an implementation of the generic golem.LanguagePack interface for {{.Locale}}
type LanguagePack struct {
}

// New creates a language pack for {{.Locale}}
func New() golem.LanguagePack {
	return &LanguagePack{}
}

// GetResource returns the dictionary of lemmatized words
func (l *LanguagePack) GetResource() ([]byte, error) {
	b, err := base64.StdEncoding.DecodeString(data)
	if err != nil {
		return nil, err
	}
	r, err := zlib.NewReader(bytes.NewReader(b))
	if err != nil {
		return nil, err
	}
	return ioutil.ReadAll(r)
}

// GetLocale returns the language name
func (l *LanguagePack) GetLocale() string {
	return locale
}

const data = "{{.Data}}"

`
