/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{encodeData as t,getFormatAndQuality as i,encode as h,getMaximumResolutionScale as n}from"../../support/screenshotUtils.js";const e={flipY:!0,premultipliedAlpha:!0};async function o(t,n){const o=await a(t,n),{format:r,quality:l}=i(n?.format,n?.quality);return h(o,{format:r,quality:l},e)}async function r(i,h){const n=await a(i,h);return t(n,e)}function a(t,i){const h=d(i||{},u(t.stage,t.size),t.size,t.padding),n=l(i,t.allLayerViews);return t.stage.takeScreenshot(h,n,t.backgroundColor,i?.rotation)}function l(t={},i){if(!t.layers)return;const h=[];return t.layers.forEach(t=>{const n=i.find(i=>i.layer.id===t.id);n?.container&&h.push(n.container)}),h}function u(t,i){return Math.min(4,n(i,Math.min(4096,t.context.parameters.maxTextureSize)))}function d(t,i,h,n){t.ignorePadding&&(n={left:0,right:0,top:0,bottom:0});let e=null;null!=t.width&&null!=t.height?e=[t.width,t.height]:null==t.width&&null!=t.height?e=[t.height,t.height]:null!=t.width&&null==t.height?e=[t.width,t.width]:null==t.width&&null==t.height&&(e=null);const o=h[0]-(n.left+n.right),r=h[1]-(n.top+n.bottom);let a,l,u=t.area||{x:0,y:0,width:o,height:r};if(e){const t=o/r,i=e[0]/e[1];if(i>t){const t=u.width/i;u={x:u.x,y:Math.round(u.y+(u.height-t)/2),width:u.width,height:Math.round(t)}}else{const t=u.height*i;u={x:Math.round(u.x+(u.width-t)/2),y:u.y,width:Math.round(t),height:u.height}}}else e=[u.width,u.height];return e[0]>u.width?(a=Math.min(e[0]/u.width,i),l=e[0]/u.width/a):(a=1,l=e[0]/u.width),{cropArea:{x:Math.round((u.x+n.left)*a),y:Math.round((u.y+n.top)*a),width:Math.round(u.width*a),height:Math.round(u.height*a)},outputScale:l,resolutionScale:a}}export{d as createScreenshotRegion,r as takeRawScreenshot,o as takeScreenshot};
