//go:build windows

package client

import "strings"

const screenshotPSScript = `
$ErrorActionPreference = 'Stop'
Add-Type -AssemblyName System.Windows.Forms,System.Drawing
$bounds = [System.Windows.Forms.SystemInformation]::VirtualScreen
$b = New-Object Drawing.Bitmap $bounds.Width, $bounds.Height
$g = [Drawing.Graphics]::FromImage($b)
$g.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.Size)
$enc = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | Where-Object { $_.MimeType -eq 'image/jpeg' }
$ep = New-Object System.Drawing.Imaging.EncoderParameters(1)
$ep.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter([System.Drawing.Imaging.Encoder]::Quality, 55)
$ms = New-Object IO.MemoryStream
$b.Save($ms, $enc, $ep)
[Convert]::ToBase64String($ms.ToArray())
`

func extractScreenshotBase64(out []byte) string {
	s := strings.TrimSpace(string(out))
	s = strings.TrimPrefix(s, "\ufeff")
	best := ""
	for _, part := range strings.Fields(s) {
		var b strings.Builder
		for _, r := range part {
			if (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '+' || r == '/' || r == '=' {
				b.WriteRune(r)
			}
		}
		cleaned := b.String()
		if len(cleaned) > len(best) {
			best = cleaned
		}
	}
	if len(best) >= 100 {
		return best
	}
	return strings.Map(func(r rune) rune {
		if (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '+' || r == '/' || r == '=' {
			return r
		}
		return -1
	}, s)
}
