extends Node
class_name WordLibrary

# Create an array of arrays where each array contains all the words of a certain length
var wordsOfLengthX : Array = []

const MIN_WORD_LENGTH = 3
const MAX_WORD_LENGTH = 12

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	# Initialize the wordsOfLengthX array
	for i in range(MIN_WORD_LENGTH, MAX_WORD_LENGTH + 1):
		wordsOfLengthX.append([])

	# Iterate over each word in the wordList and add it to the appropriate wordsOfLengthX array
	# read a single line from the file
	var file = FileAccess.open("res://Resources/2of12inf.txt", FileAccess.READ)
	var content = file.get_as_text()

	for word in content.split("\r\n"):
		var length = word.length()
		if length >= MIN_WORD_LENGTH and length <= MAX_WORD_LENGTH:
			wordsOfLengthX[length - MIN_WORD_LENGTH].append(word)

func _getWordsOfLengthX(length: int) -> Array:
	if length < MIN_WORD_LENGTH:
		return []
	if length > MAX_WORD_LENGTH:
		return []

	return wordsOfLengthX[length - MIN_WORD_LENGTH]

func is_valid_word(word: String) -> bool:
	var length = word.length()
	var list = _getWordsOfLengthX(length)
	if (list.has(word)):
		return true
	return false

func dump_debug_info() -> void:
	for i in range(MIN_WORD_LENGTH, MAX_WORD_LENGTH + 1):
		print("Words of length " + str(i) + " : " + str(wordsOfLengthX[i - MIN_WORD_LENGTH].size()))

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
	pass

