package connector

import (
	"regexp"
	"strings"

	"github.com/pingcap/errors"
)

const (
	InvalidUrlFormat = "url `%s` format is invalid"
)

var (
	PortRegexp = regexp.MustCompile(`^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$`)
)

// url pkackage will not used here for cdc can compatible with multiple downstream
// only {host}:{port} as downstream endpoint is accept
func validateEndpointUrl(s string) error {
	epList := strings.Split(s, ":")
	if len(epList) != 2 {
		return errors.Errorf(InvalidUrlFormat, s)
	}
	// validate port number
	if !PortRegexp.MatchString(epList[1]) {
		return errors.Errorf(InvalidUrlFormat, s)
	}
	return nil
}
