"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.createImage=createImage,exports.downloadImage=downloadImage,exports.getCanvasRef=getCanvasRef,exports.toDataURL=toDataURL;var _global=require("../internals/global"),_useNativeAPI=require("./useNativeAPI"),_useDOM=require("./useDOM");function getCanvasRef(e,t){return(0,_useDOM.useRef)("#".concat(e),t).then(function(e){return e?e.node:Promise.reject({errMsg:"Canvas is not supported in the current environment."})})}function createImage(e,i){var r=e.imageWidth,s=e.imageHeight,u=e.imageUrl;return new Promise(function(e,t){var a,n,o;"function"==typeof i.createImage?(a=(0,_useNativeAPI.getSystemInfoSync)(["window"]).pixelRatio,n=i.getContext("2d"),(o=i.createImage()).onload=function(){n.drawImage(o,-r*a/2,-s*a/2,r*a,s*a),e(u)},o.onerror=function(){t({errMsg:"Image creation failed in canvas.createImage."})},o.src=u):t({errMsg:"Canvas.createImage is not a function."})})}function toDataURL(e,a){var n=e.width,o=e.height,t=e.type,i=void 0===t?"png":t,t=e.quality,r=void 0===t?1:t;return new Promise(function(t){var e;"function"==typeof a.toDataURL?t(a.toDataURL("image/".concat("jpg"===i||"jpeg"===i?"jpeg":i),r)):"function"==typeof _global.miniprogramThis.canvasToTempFilePath?(e=(0,_useNativeAPI.getSystemInfoSync)(["window"]).pixelRatio,_global.miniprogramThis.canvasToTempFilePath({destWidth:n*e,destHeight:o*e,canvas:a,fileType:"jpg"===i||"jpeg"===i?"jpg":"png",quality:r,success:function(e){return t(e.tempFilePath)},fail:function(){return t("")}})):t("")})}function downloadImage(e){return new Promise(function(t,a){/^http/.test(e)&&!/^http:\/\/tmp/.test(e)?_global.miniprogramThis.downloadFile({url:e,success:function(e){200===e.statusCode?t(e.tempFilePath):a({errMsg:e.errMsg})},fail:function(e){a(e)}}):t(e)})}