package nix_test

import (
	"bytes"
	"compress/gzip"
	_ "embed"
	"io"
	"strings"
)

var instSample = map[string]string{
	"bad fields":              segmentPrefix + `instantiated 'config.sub-948ae97' ` + segmentSuffix,
	"unexpected quotes left":  segmentPrefix + `instantiated 'config.sub-948ae97' -> /n'` + segmentSuffix,
	"unexpected quotes right": segmentPrefix + `instantiated 'config.sub-948ae97' -> '/n` + segmentSuffix,
	"unexpected quotes short": segmentPrefix + `instantiated 'config.sub-948ae97' -> ''` + segmentSuffix,
	"not absolute":            segmentPrefix + `instantiated 'config.sub-948ae97' -> ' '` + segmentSuffix,
	"good segment":            segmentPrefix + segmentBody + segmentSuffix,

	"getchoo atlas":        getchooAtlasOut,
	"getchoo glados":       getchooGladosOut,
	"pluiedev pappardelle": pluiedevPappardelleOut,
}

var instWant = map[string][]string{
	"getchoo atlas":        getchooAtlasInstantiated,
	"getchoo glados":       getchooGladosInstantiated,
	"pluiedev pappardelle": pluiedevPappardelleInstantiated,
}

var collectWant = map[string][]string{
	"getchoo atlas":        getchooAtlasCollective,
	"getchoo glados":       getchooGladosCollective,
	"pluiedev pappardelle": pluiedevPappardelleCollective,
}

var drvShow = map[string][]byte{
	"getchoo atlas":        getchooAtlasShow,
	"getchoo glados":       getchooGladosShow,
	"pluiedev pappardelle": pluiedevPappardelleShow,
}

var (
	segmentPrefix = string(sampleMustGunzip(segmentPrefixGzip))
	segmentBody   = string(sampleMustGunzip(segmentBodyGzip))
	segmentSuffix = string(sampleMustGunzip(segmentSuffixGzip))
	segmentWant   = sampleSplitPaths(sampleMustGunzip(segmentWantGzip))
	segmentEarly  = sampleSplitPaths(sampleMustGunzip(segmentEarlyGzip))

	//go:embed testdata/segment/prefix.gz
	segmentPrefixGzip []byte
	//go:embed testdata/segment/body.gz
	segmentBodyGzip []byte
	//go:embed testdata/segment/suffix.gz
	segmentSuffixGzip []byte
	//go:embed testdata/segment/want.gz
	segmentWantGzip []byte
	//go:embed testdata/segment/early.gz
	segmentEarlyGzip []byte
)

func sampleSplitPaths(data []byte) []string {
	return strings.Split(strings.TrimSpace(string(data)), "\n")
}

func sampleMustGunzip(data []byte) []byte {
	var u []byte
	if r, err := gzip.NewReader(bytes.NewReader(data)); err != nil {
		panic(err.Error())
	} else if u, err = io.ReadAll(r); err != nil {
		panic(err.Error())
	}
	return u
}

// github:getchoo/borealis#atlas

var (
	getchooAtlasOut = string(sampleMustGunzip(getchooAtlasOutGzip))
	//go:embed testdata/getchoo_atlas.gz
	getchooAtlasOutGzip []byte

	getchooAtlasInstantiated = sampleSplitPaths(sampleMustGunzip(getchooAtlasInstantiatedGzip))
	//go:embed testdata/instantiated/getchoo_atlas.gz
	getchooAtlasInstantiatedGzip []byte

	getchooAtlasShow = sampleMustGunzip(getchooAtlasShowGzip)
	//go:embed testdata/derivation/show_getchoo_atlas.json.gz
	getchooAtlasShowGzip []byte

	getchooAtlasCollective = sampleSplitPaths(sampleMustGunzip(getchooAtlasCollectiveGzip))
	//go:embed testdata/derivation/collect_getchoo_atlas.gz
	getchooAtlasCollectiveGzip []byte

	getchooAtlasStdin = string(sampleMustGunzip(getchooAtlasStdinGzip))
	//go:embed testdata/format/stdin_getchoo_atlas.gz
	getchooAtlasStdinGzip []byte
)

// github:getchoo/borealis#glados

var (
	getchooGladosOut = string(sampleMustGunzip(getchooGladosOutGzip))
	//go:embed testdata/getchoo_glados.gz
	getchooGladosOutGzip []byte

	getchooGladosInstantiated = sampleSplitPaths(sampleMustGunzip(getchooGladosInstantiatedGzip))
	//go:embed testdata/instantiated/getchoo_glados.gz
	getchooGladosInstantiatedGzip []byte

	getchooGladosShow = sampleMustGunzip(getchooGladosShowGzip)
	//go:embed testdata/derivation/show_getchoo_glados.json.gz
	getchooGladosShowGzip []byte

	getchooGladosCollective = sampleSplitPaths(sampleMustGunzip(getchooGladosCollectiveGzip))
	//go:embed testdata/derivation/collect_getchoo_glados.gz
	getchooGladosCollectiveGzip []byte

	getchooGladosStdin = string(sampleMustGunzip(getchooGladosStdinGzip))
	//go:embed testdata/format/stdin_getchoo_glados.gz
	getchooGladosStdinGzip []byte
)

// git+https://tangled.sh/@pluie.me/flake#pappardelle

var (
	pluiedevPappardelleOut = string(sampleMustGunzip(pluiedevPappardelleGzip))
	//go:embed testdata/pluiedev_pappardelle.gz
	pluiedevPappardelleGzip []byte

	pluiedevPappardelleInstantiated = sampleSplitPaths(sampleMustGunzip(pluiedevPappardelleInstantiatedGzip))
	//go:embed testdata/instantiated/pluiedev_pappardelle.gz
	pluiedevPappardelleInstantiatedGzip []byte

	pluiedevPappardelleShow = sampleMustGunzip(pluiedevPappardelleShowGzip)
	//go:embed testdata/derivation/show_pluiedev_pappardelle.json.gz
	pluiedevPappardelleShowGzip []byte

	pluiedevPappardelleCollective = sampleSplitPaths(sampleMustGunzip(pluiedevPappardelleCollectiveGzip))
	//go:embed testdata/derivation/collect_pluiedev_pappardelle.gz
	pluiedevPappardelleCollectiveGzip []byte

	pluiedevPappardelleStdin = string(sampleMustGunzip(pluiedevPappardelleStdinGzip))
	//go:embed testdata/format/stdin_pluiedev_pappardelle.gz
	pluiedevPappardelleStdinGzip []byte
)
