<commit_before>// Copyright 2017 ibelie, <NAME>, Joungtao. All rights reserved.
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.

package tygo

import (
	"bytes"
	"fmt"
	"log"
	"path"
	"strings"

	"io/ioutil"
)

func Typescript(dir string, name string, types []Type) {
	var buffer bytes.Buffer

	objects := make(map[string]*Object)
	for _, t := range types {
		if object, ok := t.(*Object); ok {
			if o, exist := objects[object.Name]; exist {
				log.Fatalf("[Tygo][Typescript] Object already exists: %v %v", o, object)
			}
			objects[object.Name] = object
		}
	}

	var codes []string
	for _, t := range types {
		codes = append(codes, t.Typescript(objects))
	}
	buffer.Write([]byte(fmt.Sprintf(`// Generated for tyts by tygo.  DO NOT EDIT!

declare module tyts {
	interface Type {
		__class__: string;
		ByteSize(): number;
		Serialize(): Uint8Array;
		Deserialize(data: Uint8Array): void;
	}
}

declare module tyts.tygo {%s
}`, strings.Join(codes, ""))))

	ioutil.WriteFile(path.Join(dir, name+".d.ts"), buffer.Bytes(), 0666)
	Javascript(dir, name, types)
}

func (t *Enum) Typescript(objects map[string]*Object) string {
	var enums []string
	for _, name := range t.Sorted() {
		enums = append(enums, fmt.Sprintf(`
		%s = %d`, name, t.Values[name]))
	}
	return fmt.Sprintf(`

	const enum %s {%s
	}`, t.Name, strings.Join(enums, ","))
}

func (t *Method) Typescript(objects map[string]*Object) string {
	return ""
}

func (t *Object) Typescript(objects map[string]*Object) string {
	var fields []string
	for _, field := range t.Fields {
		fields = append(fields, fmt.Sprintf(`
		%s: %s;`, field.Name, field.Typescript(objects)))
	}
	return fmt.Sprintf(`

	class %s {%s
	}`, t.Name, strings.Join(fields, ""))
}

func (t UnknownType) Typescript(objects map[string]*Object) string {
	return ""
}

func (t SimpleType) Typescript(objects map[string]*Object) string {
	switch t {
	case SimpleType_INT32:<commit_msg>add class method<commit_after>// Copyright 2017 ibelie, <NAME>, Joungtao. All rights reserved.
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.

package tygo

import (
	"bytes"
	"fmt"
	"log"
	"path"
	"strings"

	"io/ioutil"
)

func Typescript(dir string, name string, types []Type) {
	var buffer bytes.Buffer

	objects := make(map[string]*Object)
	for _, t := range types {
		if object, ok := t.(*Object); ok {
			if o, exist := objects[object.Name]; exist {
				log.Fatalf("[Tygo][Typescript] Object already exists: %v %v", o, object)
			}
			objects[object.Name] = object
		}
	}

	var codes []string
	for _, t := range types {
		codes = append(codes, t.Typescript(objects))
	}
	buffer.Write([]byte(fmt.Sprintf(`// Generated for tyts by tygo.  DO NOT EDIT!

declare module tyts {
	interface Type {
		__class__: string;
		ByteSize(): number;
		Serialize(): Uint8Array;
		Deserialize(data: Uint8Array): void;
	}
}

declare module tyts.tygo {%s
}`, strings.Join(codes, ""))))

	ioutil.WriteFile(path.Join(dir, name+".d.ts"), buffer.Bytes(), 0666)
	Javascript(dir, name, types)
}

func (t *Enum) Typescript(objects map[string]*Object) string {
	var enums []string
	for _, name := range t.Sorted() {
		enums = append(enums, fmt.Sprintf(`
		%s = %d`, name, t.Values[name]))
	}
	return fmt.Sprintf(`

	const enum %s {%s
	}`, t.Name, strings.Join(enums, ","))
}

func (t *Method) Typescript(objects map[string]*Object) string {
	return ""
}

func (t *Object) Typescript(objects map[string]*Object) string {
	var fields []string
	for _, field := range t.Fields {
		fields = append(fields, fmt.Sprintf(`
		%s: %s;`, field.Name, field.Typescript(objects)))
	}
	return fmt.Sprintf(`

	class %s {
		__class__: string;
		ByteSize(): number;
		Serialize(): Uint8Array;
		Deserialize(data: Uint8Array): void;%s
	}

	namespace %s {
		function Deserialize(data: Uint8Array): %s;
	}`, t.Name, strings.Join(fields, ""), t.Name, t.Name)
}

func (t UnknownType) Typescript(objects map[string]*Object) string {
	return ""
}

func (t SimpleType) Typescript(objects map[string]*Object) string {
	switch t {
	case SimpleType_INT32:
{'fraction_non_alphanumeric': 0.15922330097087378, 'fraction_numerical': 0.006553398058252427, 'mean_word_length': 4.183647798742139, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5789332', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1459, 'n_words': 411}