package pipeline

import (
	"fmt"

	"gitea.westgate.pw/ShadowsOverWestgate/sow-tools/internal/erf"
)

func ensureUniqueChunkResources(chunk hakChunk) error {
	seen := map[string]string{}
	for _, asset := range chunk.Assets {
		key := fmt.Sprintf("%s:%04x", asset.Resource.Name, asset.Resource.Type)
		if previous, exists := seen[key]; exists {
			return fmt.Errorf("resource %s from %s conflicts with %s inside generated hak %s", chunkResourceLabel(asset), asset.Rel, previous, chunk.Name)
		}
		seen[key] = asset.Rel
	}
	return nil
}

func chunkResourceLabel(asset assetResource) string {
	extension, ok := erf.ExtensionForResourceType(asset.Resource.Type)
	if !ok {
		return asset.Resource.Name
	}
	return asset.Resource.Name + "." + extension
}
