package exec

import (
	"time"

	"github.com/gruntwork-io/terragrunt/internal/os/signal"
	"github.com/gruntwork-io/terragrunt/internal/venv"
)

// Option is type for passing options to the Cmd.
type Option func(*Cmd)

// WithUsePTY enables a pty for the Cmd.
func WithUsePTY(state bool) Option {
	return func(cmd *Cmd) {
		cmd.usePTY = state
	}
}

// WithEnv sets envs to the Cmd.
func WithEnv(env map[string]string) Option {
	return func(cmd *Cmd) {
		cmd.SetEnv(venv.Environ(env))
	}
}

// WithForwardSignalDelay sets forwarding signal delay to the Cmd.
func WithForwardSignalDelay(delay time.Duration) Option {
	return func(cmd *Cmd) {
		cmd.forwardSignalDelay = delay
	}
}

// WithSignalNotifier sets the source [Cmd.ForwardSignal] watches for a repeat of the
// signal it is holding. The default watches the OS.
func WithSignalNotifier(notifier signal.NotifierFunc) Option {
	return func(cmd *Cmd) {
		cmd.notifier = notifier
	}
}

// WithGracefulShutdownDelay sets the time to wait for a process to exit gracefully
// after sending an interrupt signal before escalating to SIGKILL.
// This allows processes like Terraform to clean up child processes (e.g., provider plugins).
func WithGracefulShutdownDelay(delay time.Duration) Option {
	return func(cmd *Cmd) {
		cmd.vc.SetWaitDelay(delay)
	}
}
