package chromedp

import (
	
	
	

	
	
	
)

// Screenshot is an element query action that takes a screenshot of the first element
// node matching the selector.
//
// It's supposed to act the same as the command "Capture node screenshot" in Chrome.
//
// Behavior notes: the Protocol Monitor shows that the command sends the following
// CDP commands too:
//   - Emulation.clearDeviceMetricsOverride
//   - Network.setUserAgentOverride with {"userAgent": ""}
//   - Overlay.setShowViewportSizeOnResize with {"show": false}
//
// These CDP commands are not sent by chromedp. If it does not work as expected,
// you can try to send those commands yourself.
//
// See [CaptureScreenshot] for capturing a screenshot of the browser viewport.
//
// See [screenshot] for an example of taking a screenshot of the entire page.
//
// [screenshot]: https://github.com/chromedp/examples/tree/master/screenshot
func ( interface{},  *[]byte,  ...QueryOption) QueryAction {
	return ScreenshotScale(, 1, , ...)
}

// ScreenshotScale is like [Screenshot] but accepts a scale parameter that
// specifies the page scale factor.
func ( interface{},  float64,  *[]byte,  ...QueryOption) QueryAction {
	if  == nil {
		panic("picbuf cannot be nil")
	}

	return QueryAfter(, func( context.Context,  runtime.ExecutionContextID,  ...*cdp.Node) error {
		if len() < 1 {
			return fmt.Errorf("selector %q did not return any nodes", )
		}

		// get box model
		var  page.Viewport
		if  := callFunctionOnNode(, [0], getClientRectJS, &);  != nil {
			return 
		}

		// The "Capture node screenshot" command does not handle fractional dimensions properly.
		// Let's align with puppeteer:
		// https://github.com/puppeteer/puppeteer/blob/bba3f41286908ced8f03faf98242d4c3359a5efc/src/common/Page.ts#L2002-L2011
		,  := math.Round(.X), math.Round(.Y)
		.Width, .Height = math.Round(.Width+.X-), math.Round(.Height+.Y-)
		.X, .Y = , 

		.Scale = 

		// take screenshot of the box
		,  := page.CaptureScreenshot().
			WithFormat(page.CaptureScreenshotFormatPng).
			WithCaptureBeyondViewport(true).
			WithFromSurface(true).
			WithClip(&).
			Do()
		if  != nil {
			return 
		}

		* = 
		return nil
	}, append(, NodeVisible)...)
}

// CaptureScreenshot is an action that captures/takes a screenshot of the
// current browser viewport.
//
// It's supposed to act the same as the command "Capture screenshot" in
// Chrome. See the behavior notes of Screenshot for more information.
//
// See the [Screenshot] action to take a screenshot of a specific element.
//
// See [screenshot] for an example of taking a screenshot of the entire page.
//
// [screenshot]: https://github.com/chromedp/examples/tree/master/screenshot
func ( *[]byte) Action {
	if  == nil {
		panic("res cannot be nil")
	}

	return ActionFunc(func( context.Context) error {
		var  error
		*,  = page.CaptureScreenshot().
			WithFromSurface(true).
			Do()
		return 
	})
}

// FullScreenshot takes a full screenshot with the specified image quality of
// the entire browser viewport.
//
// It's supposed to act the same as the command "Capture full size screenshot"
// in Chrome. See the behavior notes of Screenshot for more information.
//
// The valid range of the compression quality is [0..100]. When this value is
// 100, the image format is png; otherwise, the image format is jpeg.
func ( *[]byte,  int) EmulateAction {
	if  == nil {
		panic("res cannot be nil")
	}
	return ActionFunc(func( context.Context) error {
		 := page.CaptureScreenshotFormatPng
		if  != 100 {
			 = page.CaptureScreenshotFormatJpeg
		}

		// capture screenshot
		var  error
		*,  = page.CaptureScreenshot().
			WithCaptureBeyondViewport(true).
			WithFromSurface(true).
			WithFormat().
			WithQuality(int64()).
			Do()
		if  != nil {
			return 
		}
		return nil
	})
}