package main

import (
	"os"
	"path/filepath"
	"strings"
)

// join concatenates with a separator (tiny helper to keep call sites readable).
func join(parts []string, sep string) string { return strings.Join(parts, sep) }

// short trims the well-known org prefixes off a module path for display.
func short(modPath string) string {
	for _, pre := range []string{"github.com/nexuscrm/", "github.com/pingcap-inc/"} {
		if strings.HasPrefix(modPath, pre) {
			return strings.TrimPrefix(modPath, pre)
		}
	}
	return modPath
}

// splitNonEmpty splits s on sep and drops empty fields.
func splitNonEmpty(s string, sep rune) []string {
	var out []string
	for _, part := range strings.Split(s, string(sep)) {
		if strings.TrimSpace(part) != "" {
			out = append(out, part)
		}
	}
	return out
}

// writeFile writes content to a repo-relative path, creating parent dirs.
func writeFile(root, rel, content string) error {
	abs := filepath.Join(root, filepath.FromSlash(rel))
	if err := os.MkdirAll(filepath.Dir(abs), 0o755); err != nil {
		return err
	}
	return os.WriteFile(abs, []byte(content), 0o644)
}
