package json

import (
	"encoding/json"
	"fmt"
	"io"
)

type PrinterJSON struct {
	output io.Writer
}

func NewJSON(output io.Writer) *PrinterJSON {
	return &PrinterJSON{
		output: output,
	}
}

func (p *PrinterJSON) PrintObject(value any) error {
	encoded, err := json.MarshalIndent(value, "", "  ")
	if err != nil {
		return fmt.Errorf("encode response: %w", err)
	}

	_, err = fmt.Fprintln(p.output, string(encoded))
	return err
}

func (p *PrinterJSON) PrintList(values []any) error {
	encoded, err := json.MarshalIndent(values, "", "  ")
	if err != nil {
		return fmt.Errorf("encode response: %w", err)
	}

	_, err = fmt.Fprintln(p.output, string(encoded))
	return err
}
