package adif

import "strings"

// The ADIF "QSL Via" enumeration, used by QSL_SENT_VIA and QSL_RCVD_VIA. It
// says how a card travelled, and is a different thing entirely from QSL_VIA,
// which is the manager's callsign.
const (
	QSLViaBureau     = "B"
	QSLViaDirect     = "D"
	QSLViaElectronic = "E"
	// QSLViaManager is import-only in the standard: it may be read from another
	// logger's file, never written to one. OpsLog keeps it when it arrives so
	// the operator's own data is not silently altered, and NormaliseQSLVia is
	// the only place that decides so.
	QSLViaManager = "M"
)

// NormaliseQSLVia folds what other loggers and OpsLog's own older versions put
// in a routing field down to the ADIF enumeration.
//
// It accepts the letter, the English word, and the French one — OpsLog wrote
// "Bureau", "Direct" and "Electronic" in full for a long time, and the QSL
// Manager panel still shows those words to a French operator. Anything it does
// not recognise comes back empty rather than being passed through: this feeds
// an enumerated ADIF field, and inventing a value there breaks the file for
// every other logger that reads it.
func NormaliseQSLVia(s string) string {
	switch strings.ToUpper(strings.TrimSpace(s)) {
	case "B", "BUREAU", "BURO", "VIA BUREAU":
		return QSLViaBureau
	case "D", "DIRECT":
		return QSLViaDirect
	case "E", "ELECTRONIC", "ELECTRONIQUE", "ÉLECTRONIQUE", "OQRS":
		return QSLViaElectronic
	case "M", "MANAGER":
		return QSLViaManager
	}
	return ""
}

// IsQSLViaRouting reports whether a QSL_VIA value is in fact a routing method
// that ended up in the manager field.
//
// It exists for one repair: OpsLog's QSL Manager panel wrote "Bureau",
// "Direct" and "Electronic" into QSL_VIA, and imports folded QSL_SENT_VIA
// there too, so logs hold a mixture of managers and routing words in one
// column. A manager is a callsign, never one of these six words, so the test
// is exact — but it is deliberately narrow: anything else, including a manager
// whose callsign happens to be unusual, is left alone.
func IsQSLViaRouting(s string) bool {
	return NormaliseQSLVia(s) != ""
}
