package nextcloud

import "strings"

// SidecarPathForSource maps a source document to its TipTap sidecar path,
// e.g. /docs/report.docx → /docs/report.ultidoc.json.
func SidecarPathForSource(sourcePath string) string {
	sourcePath = NormalizeClientPath(sourcePath)
	dir := "/"
	name := strings.TrimPrefix(sourcePath, "/")
	if i := strings.LastIndex(name, "/"); i >= 0 {
		dir = "/" + name[:i]
		name = name[i+1:]
	}
	base := name
	if dot := strings.LastIndex(name, "."); dot > 0 {
		base = name[:dot]
	}
	sidecar := base + ultidocSidecarSuffix
	if dir == "/" {
		return "/" + sidecar
	}
	return dir + "/" + sidecar
}

// IsUltidocSidecarPath reports whether path ends with .ultidoc.json.
func IsUltidocSidecarPath(path string) bool {
	return IsUltidocSidecarName(fileNameFromPath(path))
}
