"use strict";var _thumbService=_interopRequireDefault(require("../../../services/thumb-service")),_thumbsItem=require("./thumbs-item"),_utils=require("../../../utils/utils"),_bottomIcon=require("../../../utils/icons/bottom-icon");function _interopRequireDefault(t){return t&&t.__esModule?t:{default:t}}var DEFAULT_THUMB_ICON_SRC="../../../assets/thumb.png";Component({properties:{mode:{type:String,value:"portrait"},sdkInstance:{type:Object,value:null,observer:function(t){this.bindSdkInstance(t)}}},data:{visible:!0,bottomIcon:{visible:!1},count:0,countStr:"0",thumbUpEffectUrls:[],iconSrc:DEFAULT_THUMB_ICON_SRC},lifetimes:{attached:function(){var t=_thumbService.default.totalCount;this.lastCount=t;var i=t;t>9999&&(i=(t/1e4).toFixed(1)+"w"),(0,_utils.setDataIfChanged)(this,{count:t,countStr:i.toString()}),this.updateConfig(_thumbService.default.config),this._updateCountBound=this.updateCount.bind(this),this._onConfigChange=this.onConfigChange.bind(this),_thumbService.default.on("onCountChange",this._updateCountBound),_thumbService.default.on("onConfigChange",this._onConfigChange)},ready:function(){this.initCanvas()},detached:function(){_thumbService.default.off("onCountChange",this._updateCountBound),_thumbService.default.off("onConfigChange",this._onConfigChange),this.unbindSdkInstance(),this.processingTimer&&(clearInterval(this.processingTimer),this.processingTimer=null)}},methods:{bindSdkInstance:function(t){var i,n=this;if(t&&this._bottomIconSdkInstance!==t){this.unbindSdkInstance(),this._bottomIconSdkInstance=t;var e=t.store;this._bottomIconUnsub=null==e||null===(i=e.get)||void 0===i?void 0:i.call(e,{liveInfo:function(){return n.syncHeartBottomIcon()},iconList:function(){return n.syncHeartBottomIcon()}}),this.syncHeartBottomIcon()}},unbindSdkInstance:function(){"function"==typeof this._bottomIconUnsub&&this._bottomIconUnsub(),this._bottomIconUnsub=null,this._bottomIconSdkInstance=null},syncHeartBottomIcon:function(){var t,i,n=this._bottomIconSdkInstance,e=null==n?void 0:n.store,s=(null==e||null===(t=e.get)||void 0===t?void 0:t.call(e,"liveInfo"))||{},o=null==e||null===(i=e.get)||void 0===i?void 0:i.call(e,"iconList"),a=(0,_bottomIcon.buildFixedBottomIconViewModel)(n,(0,_bottomIcon.buildHeartBottomIconSource)(s,o),this.data.mode)||{visible:!1};(0,_utils.setDataIfChanged)(this,{bottomIcon:a})},onConfigChange:function(t){this.updateConfig(t)},updateConfig:function(t){var i=this;if(t){var n={};void 0!==t.isThumbUpEnable&&(n.visible=!!t.isThumbUpEnable),void 0!==t.isThumbUpNumberEnable&&(n.showNumber=!!t.isThumbUpNumberEnable),n.iconSrc=t.thumbUpUrl||DEFAULT_THUMB_ICON_SRC,t.thumbUpEffectUrls&&(n.thumbUpEffectUrls=t.thumbUpEffectUrls),this.setData(n,function(){i.data.visible&&!i.canvas&&i.initCanvas()})}},updateCount:function(t){var i=t-(this.lastCount||0);if(this.isReady&&i>0&&i<50){var n=Math.min(i,10);this.addPendingAnimations(n)}this.lastCount=t,this.isReady||(this.isReady=!0);var e=t;t>9999&&(e=(t/1e4).toFixed(1)+"w"),(0,_utils.setDataIfChanged)(this,{count:t,countStr:e.toString()})},addPendingAnimations:function(t){this.pendingCount=(this.pendingCount||0)+t,this.processPendingAnimations()},processPendingAnimations:function(){var t=this;if(!this.processingTimer){var i=function(){t.pendingCount<=0?t.processingTimer&&(clearInterval(t.processingTimer),t.processingTimer=null):(t.startAnimation(),t.pendingCount--)};i(),this.pendingCount>0&&(this.processingTimer=setInterval(i,200))}},handleTap:function(){var t=this;_thumbService.default.addLike()&&(this.canvas||this.initCanvas(),this.calcCount=(this.calcCount||0)+1,this.calcTimer&&clearTimeout(this.calcTimer),this.calcTimer=setTimeout(function(){t.calcCount=0,t.calcTimer=null,t.setData({comboCount:0})},1e3),this.calcCount>=5&&this.setData({comboCount:this.calcCount}),wx.vibrateShort({type:"light"}).catch(function(){}))},initCanvas:function(){var t=this,i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;this.data.visible&&this.createSelectorQuery().select("#thumbCanvas").fields({node:!0,size:!0}).exec(function(n){if(n[0]){var e=n[0].node,s=n[0].width,o=n[0].height,a=wx.getSystemInfoSync().pixelRatio;e.width=s*a,e.height=o*a,t.ctx=e.getContext("2d"),t.ctx.scale(a,a),t.canvas=e,t.width=s,t.height=o,t.thumbs=[]}else t.data.visible&&i<5&&setTimeout(function(){t.initCanvas(i+1)},500)})},startAnimation:function(){if(this.canvas){var t=new _thumbsItem.ThumbsItem(this.width,this.height,this.canvas,this.data.thumbUpEffectUrls);this.thumbs.push(t),this.animating||(this.animating=!0,this.animate())}},animate:function(){var t=this;if(0===this.thumbs.length)return this.animating=!1,void(this.ctx&&this.ctx.clearRect(0,0,this.width,this.height));this.ctx&&(this.ctx.clearRect(0,0,this.width,this.height),this.thumbs=this.thumbs.filter(function(t){return t.update()}),this.thumbs.forEach(function(i){return i.draw(t.ctx)})),this.canvas.requestAnimationFrame(function(){return t.animate()})}}});