package regression
import (
	"modify/processor"
	"modify/utils"
	"os"
	"path/filepath"
	"testing"
)
func ApiAdaptor(content string, regex string, lua string) (string, int, int, error) {
	command := utils.ModifyCommand{
		Pattern:  regex,
		LuaExpr:  lua,
		LogLevel: "TRACE",
	}
	commands, err := processor.ProcessRegex(content, command)
	if err != nil {
		return "", 0, 0, err
	}
	result, modifications := utils.ExecuteModifications(commands, content)
	return result, modifications, len(commands), nil
}
func TestTalentsMechanicOutOfRange(t *testing.T) {
	given := `
    
    
      
      
    
    
    
      
        
      
    
    
      
        
      
      
        
          
            
              
            
          
        
      
    
  `
	actual := `
    
    
      
      
    
    
    
      
        
      
    
    
      
        
      
      
        
          
            
              
            
          
        
      
    
  `
	result, mods, matches, err := ApiAdaptor(given, `!anyvalue="(?!num)"!anyvalue="(?!num)"!anyvalue="(?!num)"!anyamount="(?!num)"`, "movementspeed=round(movementspeed*1.5, 2) duration=round(duration*2, 2) repairspeed=round(repairspeed*2, 2) durationv=duration")
	if err != nil {
		t.Fatalf("Error processing content: %v", err)
	}
	if matches != 4 {
		t.Errorf("Expected 4 matches, got %d", matches)
	}
	if mods != 4 {
		t.Errorf("Expected 4 modifications, got %d", mods)
	}
	if result != actual {
		t.Errorf("expected %s, got %s", actual, result)
	}
}
func TestIndexExplosions_ShouldNotPanic(t *testing.T) {
	cwd, err := os.Getwd()
	if err != nil {
		t.Fatalf("Error getting current working directory: %v", err)
	}
	given, err := os.ReadFile(filepath.Join(cwd, "..", "testfiles", "OutpostItems.xml"))
	if err != nil {
		t.Fatalf("Error reading file: %v", err)
	}
	expected, err := os.ReadFile(filepath.Join(cwd, "..", "testfiles", "OutpostItemsExpected.xml"))
	if err != nil {
		t.Fatalf("Error reading file: %v", err)
	}
	result, _, _, err := ApiAdaptor(string(given), `(?-s)LightComponent!anyrange="(!num)"`, "*4")
	if err != nil {
		t.Fatalf("Error processing content: %v", err)
	}
	// We don't really care how many god damn matches there are as long as the result is correct
	// if matches != 45 {
	// 	t.Errorf("Expected 45 match, got %d", matches)
	// }
	//
	// if mods != 45 {
	// 	t.Errorf("Expected 45 modification, got %d", mods)
	// }
	if string(result) != string(expected) {
		t.Errorf("expected %s, got %s", expected, result)
	}
}