package asset

import (
	"strings"
)

const (
	formatRaw string = "raw"
	formatZip string = "zip"
)

// RemoveExtFromAsset removes file extensions from asset names and returns the format.
// It recognizes various archive formats including tar variants, compression formats,
// and platform-specific formats. Returns the asset name without extension and the format.
func RemoveExtFromAsset(assetName string) (string, string) {
	formats := []string{
		"tar.br",
		"tar.bz2",
		"tar.gz",
		"tar.lz4",
		"tar.sz",
		"tar.xz",
		"tbr",
		"tbz",
		"tbz2",
		"tgz",
		"tlz4",
		"tsz",
		"txz",

		"tar.zst",

		formatZip,
		"gz",
		"bz2",
		"lz4",
		"sz",
		"xz",
		"zst",

		"dmg",
		"pkg",

		"rar",
		"tar",
	}
	for _, format := range formats {
		if s, ok := strings.CutSuffix(assetName, "."+format); ok {
			return s, format
		}
	}
	return assetName, formatRaw
}

// getFormat extracts the file format from an asset name.
// It uses RemoveExtFromAsset to determine the format type.
func getFormat(assetName string) string {
	_, format := RemoveExtFromAsset(assetName)
	return format
}
