import '../media/ids.dart';

String buildGlobalKey(ServerId serverId, String ratingKey) => '$serverId:$ratingKey';

/// Separator used by profile-owned rows whose public media identity is still
/// [buildGlobalKey]. Profile ids are generated by the app and do not contain
/// this character.
const String profileScopedGlobalKeySeparator = '|';

/// Builds a profile-owned sync-rule key from [profileId] and public media id.
String buildProfileScopedGlobalKey(String profileId, ServerId serverId, String ratingKey) {
  return '$profileId$profileScopedGlobalKeySeparator${buildGlobalKey(serverId, ratingKey)}';
}

/// Parses a globalKey string (format: "serverId:ratingKey") into its components.
///
/// Returns `null` if the key does not contain a colon separator.
/// Uses [indexOf] so ratingKeys containing colons are handled correctly.
({ServerId serverId, String ratingKey})? parseGlobalKey(String globalKey) {
  final idx = globalKey.indexOf(':');
  if (idx <= 0) return null;
  return (serverId: ServerId(globalKey.substring(0, idx)), ratingKey: globalKey.substring(idx + 1));
}
