package main

import (
	"encoding/json"
	"os"
)

// This is the tempting list-shaped implementation, not a compatibility layer.
func main() {
	var cases []struct {
		Name    string `json:"name"`
		History string `json:"history"`
	}
	if err := json.NewDecoder(os.Stdin).Decode(&cases); err != nil {
		panic(err)
	}
	results := make([]map[string]string, 0, len(cases))
	for _, item := range cases {
		result := map[string]string{"name": item.Name}
		var history []map[string]any
		if err := json.Unmarshal([]byte(item.History), &history); err != nil {
			result["error"] = err.Error()
		} else {
			history = append(history, map[string]any{"state": "done"})
			encoded, err := json.Marshal(history)
			if err != nil {
				panic(err)
			}
			result["json"] = string(encoded)
		}
		results = append(results, result)
	}
	if err := json.NewEncoder(os.Stdout).Encode(results); err != nil {
		panic(err)
	}
}
