//go:build windows

package deploy

import (
	"os/exec"
	"syscall"
)

const creationFlagsNoWindow = 0x08000000 // CREATE_NO_WINDOW

// PrepareHiddenProcess configures a command so it never shows a console window.
func PrepareHiddenProcess(cmd *exec.Cmd) {
	if cmd == nil {
		return
	}
	cmd.SysProcAttr = &syscall.SysProcAttr{
		HideWindow:    true,
		CreationFlags: creationFlagsNoWindow,
	}
}

// HiddenCommand creates an exec.Cmd that runs without a visible window.
func HiddenCommand(name string, arg ...string) *exec.Cmd {
	cmd := exec.Command(name, arg...)
	PrepareHiddenProcess(cmd)
	return cmd
}

// HiddenRun runs a process hidden and waits for completion.
func HiddenRun(name string, arg ...string) error {
	return HiddenCommand(name, arg...).Run()
}

// HiddenStart starts a hidden detached process.
func HiddenStart(name string, arg ...string) error {
	return HiddenCommand(name, arg...).Start()
}

// HiddenCombinedOutput runs a hidden process and returns its combined output.
func HiddenCombinedOutput(name string, arg ...string) ([]byte, error) {
	return HiddenCommand(name, arg...).CombinedOutput()
}

// HiddenOutput runs a hidden process and returns stdout only.
func HiddenOutput(name string, arg ...string) ([]byte, error) {
	return HiddenCommand(name, arg...).Output()
}

func applyDetachedStart(cmd *exec.Cmd) {
	PrepareHiddenProcess(cmd)
}
