package schema

import (
	"path/filepath"
	"strings"
)

// SwapSuffix returns the path of a sibling artifact.
//
// Drops path's file extension, optionally trims infix from the resulting stem,
// then appends suffix and rejoins with path's directory.
// Example: SwapSuffix("a/b.json", "", "-status.json") == "a/b-status.json".
func SwapSuffix(path, infix, suffix string) string {
	base := filepath.Base(path)
	stem := base
	if ext := filepath.Ext(base); ext != "" {
		stem = base[:len(base)-len(ext)]
	}
	if infix != "" {
		stem = strings.TrimSuffix(stem, infix)
	}
	return filepath.Join(filepath.Dir(path), stem+suffix)
}
