"use strict";var _baseComponent=_interopRequireDefault(require("../helpers/baseComponent")),_classNames=_interopRequireDefault(require("../helpers/libs/classNames")),_styleToCssString=_interopRequireDefault(require("../helpers/libs/styleToCssString")),_useNativeAPI=require("../helpers/hooks/useNativeAPI"),_useCanvasAPI=require("../helpers/hooks/useCanvasAPI"),_index=_interopRequireDefault(require("./qr.js/index"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function _defineProperty(e,t,r){return(t=_toPropertyKey(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function _toPropertyKey(e){e=_toPrimitive(e,"string");return"symbol"==_typeof(e)?e:String(e)}function _toPrimitive(e,t){if("object"!=_typeof(e)||!e)return e;var r=e[Symbol.toPrimitive];if(void 0===r)return("string"===t?String:Number)(e);r=r.call(e,t||"default");if("object"!=_typeof(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}var utf16to8=function(e){for(var t=e.length,r="",o=0;o<t;o++){var a=e.charCodeAt(o);1<=a&&a<=127?r+=e.charAt(o):r=2047<a?(r=(r+=String.fromCharCode(224|a>>12&15))+String.fromCharCode(128|a>>6&63))+String.fromCharCode(128|a>>0&63):(r+=String.fromCharCode(192|a>>6&31))+String.fromCharCode(128|a>>0&63)}return r};(0,_baseComponent.default)({useExport:!0,properties:{prefixCls:{type:String,value:"wux-qrcode"},typeNumber:{type:Number,value:-1},errorCorrectLevel:{type:Number,value:2},width:{type:Number,value:200},height:{type:Number,value:200},whiteSpace:{type:Number,value:0},fgColor:{type:String,value:"black"},bgColor:{type:String,value:"white"},data:{type:String,value:""},showMenuByLongpress:{type:Boolean,value:!1},qrcodeStatus:{type:String,value:"activated"},qrcodeExpiredText:{type:String,value:"二维码过期"},qrcodeRefreshText:{type:String,value:"点击刷新"}},data:{wrapStyle:"",base64Url:""},observers:_defineProperty(_defineProperty({},"height, width",function(e,t){this.updateStyle(e,t)}),"prefixCls, typeNumber, errorCorrectLevel, width, height, whiteSpace, fgColor, bgColor, data",function(){this.setBase64Url.apply(this,arguments)}),computed:{classes:["prefixCls",function(e){return{wrap:(0,_classNames.default)(e),canvas:"".concat(e,"__canvas"),image:"".concat(e,"__image"),mask:"".concat(e,"__mask"),expired:"".concat(e,"__expired"),refresh:"".concat(e,"__refresh"),icon:"".concat(e,"__icon")}}]},methods:_defineProperty({updateStyle:function(e,t){e=(0,_styleToCssString.default)({height:"".concat(e,"px"),width:"".concat(t,"px")});this.setData({wrapStyle:e})},setBase64Url:function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];var o=t[0],a=t[1],n=t[2],i=t[3],u=t[4],s=t[5],l=t[6],c=t[7],f=t[8];this.createCanvasContext({prefixCls:o,typeNumber:a,errorCorrectLevel:n,width:i,height:u,whiteSpace:s,fgColor:l,bgColor:c,data:f})},createCanvasContext:function(t){var o=this,e=t.prefixCls,r=t.typeNumber,a=t.errorCorrectLevel,i=t.width,u=t.height,s=t.whiteSpace,l=t.fgColor,c=t.bgColor,n=t.data,f=(0,_index.default)(utf16to8(n),{typeNumber:r,errorCorrectLevel:a}).modules,h=(i-2*s)/f.length,p=(u-2*s)/f.length,y="".concat(e,"__canvas"),n=Promise.resolve();return n=(n=n.then(function(){return(0,_useCanvasAPI.getCanvasRef)(y,o).then(function(e){var n=(o.canvas=e).getContext("2d"),t=(0,_useNativeAPI.getSystemInfoSync)(["window"]).pixelRatio,r=u*t;return e.width=i*t,e.height=r,n.scale(t,t),n.fillStyle="#ffffff",n.fillRect(0,0,i,u),f.forEach(function(e,a){e.forEach(function(e,t){n.fillStyle=e?l:c;var e=Math.round(t*h)+s,r=Math.round(a*p)+s,t=Math.ceil((t+1)*h)-Math.floor(t*h),o=Math.ceil((a+1)*p)-Math.floor(a*p);n.fillRect(e,r,t,o)})}),(0,_useCanvasAPI.toDataURL)({width:i,height:u},e).then(function(e){return n.restore(),e})})})).then(function(e){e=e,t.base64Url!==e&&(o.setData({base64Url:e}),o.triggerEvent("load",{base64Url:e}))},function(e){o.triggerEvent("error",e)})},onTap:function(){this.triggerEvent("click")},onMaskClick:function(){"expired"===this.data.qrcodeStatus&&this.triggerEvent("refresh")}},"export",function(){var e=this;return{getCanvasNode:function(){return e.canvas},getBase64Url:function(){return e.data.base64Url}}}),ready:function(){var e=this.data,t=e.height,e=e.width;this.updateStyle(t,e),this.createCanvasContext(this.data)}});