package renderer

import "../token"
import "core:fmt"
import "core:strings"
import "core:unicode"

get_text_content :: proc(token: ^token.Token, b: ^strings.Builder) {
	if token.type == .Text {
		if v, ok := token.value.(string); ok {
			strings.write_string(b, v)
		}
	}
	for child in token.children {
		get_text_content(child, b)
	}
}

check_only_var_defs :: proc(token: ^token.Token) -> bool {
	for child in token.children {
		if child.type == .VariableDef || child.type == .BreakLine {
			continue
		}
		if child.type == .Text {
			if child.value != nil {
				if v, ok := child.value.(string); ok && v != "" {
					return false
				}
			}
			has_non_var_def := false
			for gc in child.children {
				if gc.type != .VariableDef {
					has_non_var_def = true
					break
				}
			}
			if has_non_var_def {
				return false
			}
			continue
		}
		return false
	}
	return true
}

slugifyifyifyify :: proc(text: string) -> string {
	b := strings.builder_make(context.temp_allocator)

	for ch in text {
		lower_ch := unicode.to_lower(ch)

		if unicode.is_letter(lower_ch) || unicode.is_digit(lower_ch) {
			strings.write_rune(&b, lower_ch)
		} else if lower_ch == ' ' || lower_ch == '_' || lower_ch == '-' {
			strings.write_rune(&b, '-')
		}
	}

	res := strings.to_string(b)

	for {
		new_res, replaced := strings.replace_all(res, "--", "-", context.temp_allocator)
		if !replaced {
			break
		}
		res = new_res
	}

	res = strings.trim(res, "-")

	return res
}

get_alignment_attribute :: proc(tok: ^token.Token) -> string {
	if len(tok.children) > 0 {
		first_child := tok.children[0]
		if first_child.type == .Left {
			return "left"
		} else if first_child.type == .Right {
			return "right"
		} else if first_child.type == .Middle {
			return "center"
		}
	}
	return ""
}
