package common

/*
 * This file is part of Autogits.
 *
 * Copyright © 2024 SUSE LLC
 *
 * Autogits is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 2 of the License, or (at your option) any later
 * version.
 *
 * Autogits is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * Foobar. If not, see <https://www.gnu.org/licenses/>.
 */

import (
	"fmt"
	"slices"
	"strings"
)

func SplitStringNoEmpty(str, sep string) []string {
	ret := slices.DeleteFunc(strings.Split(str, sep), func(s string) bool {
		return len(strings.TrimSpace(s)) == 0
	})
	for i := range ret {
		ret[i] = strings.TrimSpace(ret[i])
	}
	return ret
}

func TranslateHttpsToSshUrl(url string) (string, error) {
	const url1 = "https://src.opensuse.org/"
	const url2 = "https://src.suse.de/"

	if len(url) > len(url1) && url[0:len(url1)] == url1 {
		return "gitea@src.opensuse.org:" + url[len(url1):], nil
	}
	if len(url) > len(url2) && url[0:len(url2)] == url2 {
		return "gitea@src.suse.de:" + url[len(url2):], nil
	}

	return "", fmt.Errorf("Unknown input url %s", url)
}

