package main

import (
	"encoding/csv"
	"fmt"
	"image/color"
	"image/png"
	"maps"
	"os"
	"slices"

	imgutil "git.nkpl.cc/twocookedfaggots/imageutils/pkg"
)

func check(err error) {
	if err != nil {
		panic(err)
	}
}

type Set[S comparable] map[S]struct{}

func (s Set[S]) Add(v S) {
	s[v] = struct{}{}
}

func (s Set[S]) Contains(v S) bool {
	_, ok := s[v]
	return ok
}

func Map[S1 ~[]T1, S2 ~[]T2, T1, T2 any](s S1, f func(T1) T2) S2 {
	result := make(S2, len(s))
	for i := range s {
		result[i] = f(s[i])
	}
	return result
}

type hex string

func (h hex) String() string {
	return string(fmt.Sprintf("hex value: %s\n", string(h)))
}

func main() {
	img, err := png.Decode(os.Stdin)
	check(err)
	p := Set[color.Color]{}
	for y := 0; y < img.Bounds().Dy(); y++ {
		for x := 0; x < img.Bounds().Dx(); x++ {
			p.Add(img.At(x, y))
		}
	}
	w := csv.NewWriter(os.Stdout)
	s := Map[color.Palette, []string](
		slices.Collect(maps.Keys(p)), imgutil.ColorToHex,
	)
	/*
		h := make([]hex, 0, len(s))
		for i := range s {
			h = append(h, hex(s[i]))
		}
	*/
	err = w.Write(s)
	check(err)
}
