package main

import (
	
	
	
	
	
	
)

func main() {
	 := extractMermaidDiagrams(os.Args[1], os.Args[2])
	if  != nil {
		fmt.Println("Error:", )
	}
}

func extractMermaidDiagrams( string,  string) error {
	// Open the Markdown file
	,  := os.Open()
	if  != nil {
		return fmt.Errorf("failed to open file: %v", )
	}
	defer .Close()

	 := strings.Split(, " ")

	// Regex to detect mermaid code blocks
	 := regexp.MustCompile("^```mermaid$")
	 := regexp.MustCompile("^```$")

	// Scanner to read the file line by line
	 := bufio.NewScanner()

	 := false
	 := 1
	var  strings.Builder // Store diagram content temporarily

	for .Scan() {
		 := .Text()

		// Check for the start of a mermaid block
		if .MatchString() {
			 = true
			.Reset() // Clear the previous content
			continue
		}

		// Check for the end of a mermaid block
		if  && .MatchString() {
			// skip if not requested
			if !slices.Contains(, fmt.Sprintf("%d", )) {
				continue
			}

			 = false

			// Write the extracted diagram to a .mmd file
			 := fmt.Sprintf("diagram_%d.mmd", )
			 := os.WriteFile(, []byte(.String()), 0644)
			if  != nil {
				return fmt.Errorf("failed to write diagram file: %v", )
			}
			fmt.Printf("Extracted diagram %d into %s\n", , )

			++
			continue
		}

		// Collect lines for the mermaid block
		if  {
			.WriteString( + "\n")
		}
	}

	// Check for scanner errors
	if  := .Err();  != nil {
		return fmt.Errorf("error reading file: %v", )
	}

	return nil
}