package qx

import (
	"fmt"
	"strings"
)

// func ValidateSortKeys(keys []SortKey) (err error) {
// 	for _, sk := range keys {

// 	}
// }

type SortKey struct {
	Key   string `json:"key"`
	Order string `json:"order"`
}

func JoinOrderBy(list []SortKey) (str string, err error) {
	if len(list) == 0 {
		return
	}
	var keys []string
	for _, sk := range list {
		if sk.Key == "" {
			return "", fmt.Errorf("empty key not allowed")
		}
		switch sk.Order {
		case "":
			return "", fmt.Errorf("empty order not allowed")
		case "asc":
			keys = append(keys, sk.Key+" ASC")
		case "desc":
			keys = append(keys, sk.Key+" DESC")
		default:
			return "", fmt.Errorf("Wrong order: %s", sk.Order)
		}
	}
	return "ORDER BY " + strings.Join(keys, ", "), nil
}
