# Frontmatter Processing Utilities
# Functions for extracting and processing YAML frontmatter from markdown files

# Extract frontmatter from markdown file
export def extract_frontmatter [input_file: string] {
    try {
        let content = (open $input_file | lines)
        if ($content | first) == "---" {
            mut frontmatter = []
            mut in_frontmatter = false
            mut line_count = 0

            for line in $content {
                $line_count = ($line_count + 1)
                if $line_count == 1 and $line == "---" {
                    $in_frontmatter = true
                    continue
                }
                if $in_frontmatter and $line == "---" {
                    break
                }
                if $in_frontmatter and not ($line | str starts-with "#") {  # Skip comment lines
                    $frontmatter = ($frontmatter | append $line)
                }
            }

            $frontmatter | str join "\n"
        } else {
            ""
        }
    } catch {
        ""
    }
}

# Extract value from frontmatter
export def extract_from_frontmatter [frontmatter: string, key: string, default: string] {
    if ($frontmatter | is-empty) {
        return $default
    }

    let lines = ($frontmatter | lines)
    for line in $lines {
        # Simple approach: look for "key: value" pattern
        if ($line | str starts-with $"($key):") {
            let value_part = ($line | str replace $"($key):" "" | str trim)
            # Remove quotes if present
            let clean_value = ($value_part | str replace -a '"' '' | str replace -a "'" '')
            return $clean_value
        }
    }

    $default
}

# Extract YAML array from frontmatter (for tags)
export def extract_yaml_array [frontmatter: string, key: string] {
    let tags_str = extract_from_frontmatter $frontmatter $key "[]"
    if $tags_str == "[]" or ($tags_str | is-empty) {
        return []
    }

    # Parse YAML array format: ["item1", "item2", "item3"]
    let tags = ($tags_str | str replace -a '[' '' | str replace -a ']' '' | str replace -a '"' '' | split row ',' | each { |tag| $tag | str trim })
    $tags | where $it != ""
}

# Collect all categories and tags from markdown files
export def collect_categories_and_tags [source_dir: string] {
    mut all_categories = []
    mut all_tags = []

    let md_files = (glob $"($source_dir)/**/*.md")
    for md_file in $md_files {
        let frontmatter = extract_frontmatter $md_file
        let published = (extract_from_frontmatter $frontmatter "published" "true")

        if ($published | str downcase) == "true" {
            # Extract category (singular)
            let category = (extract_from_frontmatter $frontmatter "category" "")
            if not ($category | is-empty) {
                $all_categories = ($all_categories | append $category)
            }

            # Extract tags (array)
            let tags = extract_yaml_array $frontmatter "tags"
            for tag in $tags {
                $all_tags = ($all_tags | append $tag)
            }
        }
    }

    {
        categories: ($all_categories | uniq),
        tags: ($all_tags | uniq)
    }
}
