class_name ContentId
extends RefCounted

## Stable authored-content identity serialized as canonical lowercase UUID text.
## It is independent of runtime entities, server database entries and wire GUIDs.

var uuid_text: String:
	get:
		return _uuid_text

var _uuid_text: String


## Normalizes surrounding whitespace and hexadecimal letter case.
func _init(uuid_text_value: String) -> void:
	_uuid_text = uuid_text_value.strip_edges().to_lower()


## Reports whether the value has the canonical 8-4-4-4-12 UUID text shape.
func is_valid() -> bool:
	if _uuid_text.length() != 36:
		return false
	for character_index in range(_uuid_text.length()):
		if character_index in [8, 13, 18, 23]:
			if _uuid_text[character_index] != "-":
				return false
		elif not _is_lowercase_hexadecimal_character(_uuid_text[character_index]):
			return false
	return true


## Compares stable content identity by normalized UUID text.
func equals(other_content_id: ContentId) -> bool:
	return other_content_id != null and _uuid_text == other_content_id.uuid_text


## Returns the canonical dictionary/serialization key.
func to_key() -> String:
	return _uuid_text


func _is_lowercase_hexadecimal_character(character: String) -> bool:
	return (character >= "0" and character <= "9") or (character >= "a" and character <= "f")
