"use strict";function _typeof(t){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},_typeof(t)}var _utils=require("../../../utils/utils"),_constant=require("../../../utils/constant"),_socket=require("../../../utils/socket"),_util=require("../util"),_theme=require("../../../utils/theme"),_utils2=require("./utils");function _createForOfIteratorHelper(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!r){if(Array.isArray(t)||(r=_unsupportedIterableToArray(t))||e&&t&&"number"==typeof t.length){r&&(t=r);var n=0,a=function(){};return{s:a,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,i=!0,s=!1;return{s:function(){r=r.call(t)},n:function(){var t=r.next();return i=t.done,t},e:function(t){s=!0,o=t},f:function(){try{i||null==r.return||r.return()}finally{if(s)throw o}}}}function _unsupportedIterableToArray(t,e){if(t){if("string"==typeof t)return _arrayLikeToArray(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_arrayLikeToArray(t,e):void 0}}function _arrayLikeToArray(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r=0;--a){var o=this.tryEntries[a],i=o.completion;if("root"===o.tryLoc)return n("end");if(o.tryLoc<=this.prev){var s=r.call(o,"catchLoc"),u=r.call(o,"finallyLoc");if(s&&u){if(this.prev=0;--n){var a=this.tryEntries[n];if(a.tryLoc<=this.prev&&r.call(a,"finallyLoc")&&this.prev=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),S(r),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var a=n.arg;S(r)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:b(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),d}},t}function asyncGeneratorStep(t,e,r,n,a,o,i){try{var s=t[o](i),u=s.value}catch(t){return void r(t)}s.done?e(u):Promise.resolve(u).then(n,a)}function _asyncToGenerator(t){return function(){var e=this,r=arguments;return new Promise(function(n,a){var o=t.apply(e,r);function i(t){asyncGeneratorStep(o,n,a,i,s,"next",t)}function s(t){asyncGeneratorStep(o,n,a,i,s,"throw",t)}i(void 0)})}}var teaEventMap={attend_lottery_success:_constant.EVENTS.PUBLIC.lottery.attend,lottery_result_modal:_constant.EVENTS.PUBLIC.lottery.result,lottery_win_info:_constant.EVENTS.PUBLIC.lottery.winInfo,open_lottery_modal:_constant.EVENTS.PUBLIC.lottery.click};Component({properties:{sdkInstance:{type:Object,value:null}},data:{visible:!1,type:"",data:null,useDarkTheme:!1,isDeadline:!1,undoLotteryMap:{},lotteryQueue:[],animationendAwardId:[],openResultAwardId:[],animationAwardId:0,status:0},lifetimes:{detached:function(){"function"==typeof this.unsub&&this.unsub(),this.unsub=null}},observers:{sdkInstance:function(t){var e=this;if(t){this.store=t.store,this.sdkInstance=t;var r=this;t.on(_constant.EVENTS.INNER.socket.init,function(t){r.socket=t,r.bindSocketEvents()});var n=1;"function"==typeof this.unsub&&this.unsub(),this.unsub=null,this.unsub=this.store.get({AwardInfo:function(t){var n=t||{},a=n.visible,o=n.currentId,i=n.isAuto;e.setData({visible:a}),i&&"detail"===e.data.type||(a&&o?r.openLottery(o):e.setData({type:""}))},liveInfo:function(t){e.setData({useDarkTheme:(0,_theme.isUseDarkTheme)(null==t?void 0:t.Basic)})},mainInfo:function(t){2===(t||{}).mode&&1===n&&r.setCheckedCheckIn()},userInfo:function(t){(t||{}).userId&&1===n&&r.setCheckedCheckIn()},imageTextInfo:function(t){((null==t?void 0:t.imageText)||[]).length&&1===n&&(n++,r.imageTextUpdateList())}})}}},methods:{createLotteryRequestId:function(){return this.lotteryRequestId=(this.lotteryRequestId||0)+1,this.lotteryRequestId},isLatestLotteryRequest:function(t,e){var r,n=(this.store.get("AwardInfo")||{}).visible,a=null===(r=this.data.data)||void 0===r?void 0:r.ID;return n&&a===e&&this.lotteryRequestId===t},setAwardInfo:function(t,e){var r=this.store.get("AwardInfo")||{},n=r.visible,a=r.currentId;n===t&&a===e||this.store.set("AwardInfo",{visible:t,currentId:e})},updateLotteryUserStatus:function(t){var e=(this.store.get("lotteriesUserStatus")||{}).lotteriesUserStatus,r=void 0===e?{}:e;(t||[]).forEach(function(t){if(r[t.lotteryId]){var e=r[t.lotteryId];[_util.LotteryStatus.WIN,_util.LotteryStatus.LOSE].includes(e)&&t.status!==_util.LotteryServerStatus.Delete||_util.LotteryStatus.ENROLLED===e&&_util.LotteryServerStatus.Ing===t.status||(e=(0,_util.getLotteryStatus)(t.status)),r[t.lotteryId]=e}else r[t.lotteryId]=(0,_util.getLotteryStatus)(t.status)}),this.store.set("lotteriesUserStatus",{lotteriesUserStatus:r})},updateLotteryUserStatusNoTrans:function(t,e){var r=(this.store.get("lotteriesUserStatus")||{}).lotteriesUserStatus,n=void 0===r?{}:r;n[t]=e,this.store.set("lotteriesUserStatus",{lotteriesUserStatus:n})},syncVisibleDetailOnLotteryStateChange:function(t,e){var r=this.store.get("AwardInfo")||{},n=r.visible,a=r.currentId;n&&a===t&&"detail"===this.data.type&&e!==_util.LotteryServerStatus.Ing&&this.setAwardInfo(!1,null)},updateOneLottery:function(t,e){var r=this,n=this.getCurrentLottery(t);if(n)if(n.Status!==e&&this.sdkInstance.reportLogger("lottery_status_change",{params:{awardId:t,oldStatus:n.Status,newStatus:e,evModule:"LOTTERY"}}),this.syncVisibleDetailOnLotteryStateChange(t,e),this.updateLotteryUserStatus([{lotteryId:t,status:e}]),n.Status!==_util.LotteryServerStatus.Opened&&e===_util.LotteryServerStatus.Opened)if(this.isLogin()){var a={ActivityID:this.store.get("mainInfo").activityId,AwardID:t};setTimeout(_asyncToGenerator(_regeneratorRuntime().mark(function e(){var n,o;return _regeneratorRuntime().wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,r.sdkInstance.request(_constant.REQUEST_URL_MAP.GET_AWARD_RECORD_INFO,{method:"POST",data:a});case 3:return n=e.sent,e.next=6,r.stopAnimation(t);case 6:n.AwardResult!==_util.AwardResult.WIN&&n.AwardResult!==_util.AwardResult.LOSE||(r.updateLotteryUserStatusNoTrans(t,n.AwardResult===_util.AwardResult.WIN?_util.LotteryStatus.WIN:_util.LotteryStatus.LOSE),null!==(o=r.data.openResultAwardId)&&void 0!==o&&o.includes(t)?r.sdkInstance.reportLogger("lottery_openResultModalCancel_repeat",{params:{AwardID:t,openResultAwardId:openResultAwardId,evModule:"LOTTERY"}}):(r.data.openResultAwardId.push(t),r.data.visible?r.data.lotteryQueue.push(t):r.setAwardInfo(!0,t))),r.reportTea("lottery_result_modal",{awardId:t,LoginTel:user.userTel,ifWin:4===n.AwardResult,evModule:"LOTTERY"}),callBack(),e.next=14;break;case 11:e.prev=11,e.t0=e.catch(0),r.stopAnimation(t);case 14:case"end":return e.stop()}},e,null,[[0,11]])})),5e3)}else this.stopAnimation(t);else if(n.Status!==_util.LotteryServerStatus.Opened&&n.Status!==_util.LotteryServerStatus.Opening&&e===_util.LotteryServerStatus.Opening){var o;if(null!==(o=this.data.animationendAwardId)&&void 0!==o&&o.includes(t))this.sdkInstance.reportLogger("lottery_animationCancel_repeat",{params:{AwardID:t,animationendAwardId:this.data.animationendAwardId,evModule:"LOTTERY"}});else{if(this.data.animationendAwardId.push(t),this.updateLotteryUserStatusNoTrans(t,_util.LotteryStatus.WAITING),"noAnimation"===n.AwardThemeUrl)return;setTimeout(function(){r.startAnimation(t)},5e3)}}else callBack()},startAnimation:function(t){var e=this,r=this.data.animationAwardId,n=this.getCurrentLottery(t);n?r||(this.setAwardInfo(!1,null),this.setData({animationAwardId:t,animationDowTime:(new Date).getTime(),AwardThemeUrl:n.AwardThemeUrl}),setTimeout(function(){e.data.animationAwardId===t&&(e.stopAnimation(t),e.sdkInstance.reportLogger("lottery_stopAnimation_manual",{params:{animationAwardId:r,evModule:"LOTTERY"}}))},1e4),this.sdkInstance.reportLogger("lottery_startAnimation_success",{params:_objectSpread(_objectSpread({},n),{},{evModule:"LOTTERY"})})):this.sdkInstance.reportLogger("lottery_startAnimation_failed",{params:{AwardID:t,evModule:"LOTTERY"}})},stopAnimation:function(t){var e=this;return _asyncToGenerator(_regeneratorRuntime().mark(function r(){var n,a,o,i;return _regeneratorRuntime().wrap(function(r){for(;;)switch(r.prev=r.next){case 0:if(n=e.data,a=n.animationAwardId,o=n.animationDowTime,i=(new Date).getTime()-(o||0),!(a&&i<3e3)){r.next=5;break}return r.next=5,(0,_utils.waitNumSeconds)(3e3-i);case 5:e.setData({animationAwardId:0}),e.sdkInstance.reportLogger("lottery_stopAnimation_success",{params:{AwardID:t,animationAwardId:a,evModule:"LOTTERY"}});case 7:case"end":return r.stop()}},r)}))()},onCountDown:function(){this.setData({isDeadline:!0,errMsg:""})},isLogin:function(){var t=arguments.length>0&&void 0!==arguments[0]&&arguments[0];return 2===this.store.get("mainInfo").mode||(t&&this.triggerEvent("commentCheck"),!1)},setCurrentData:function(t,e){try{var r=t.ID,n=this.data.undoLotteryMap[r],a=(this.store.get("lotteriesUserStatus")||{}).lotteriesUserStatus,o=e||(void 0===a?{}:a)[r]||(0,_util.getLotteryStatus)(t.Status),i=(0,_util.formatConditionDetails)(t.BarragePwd,t.AwardCondition,n,o===_util.LotteryStatus.ENROLLED),s=_objectSpread(_objectSpread(_objectSpread({},this.data.data),t),{},{formatDataList:i,hasCondition:!!i.length,showCountDown:t.AwardType===_util.AwardType.AutoOpen,UndoAwardCondition:n});s.status=o;var u=(0,_utils2.getDisplayAwardItemConfigs)(s,o),l=u[0]||{};s.displayAwardItemConfigs=u,s.displayAwardItemConfig=l,s.attendFailFlag=o===_util.LotteryStatus.OPEN&&!!n,s.attendedFlag=_util.AttendedStatus.includes(o),s.isTicket=l.AwardItemType===_util.AwardItemType.TICKET;var c=l.WinnerInfoType||s.WinnerInfoType||[];s.AwardLotteryTicketAddr=l.AwardLotteryTicketAddr||s.AwardLotteryTicketAddr,s.showReceiverName=null==c?void 0:c.includes(_util.EnumWinnerInfoType.ReceiverName),s.showAddress=c.includes(_util.EnumWinnerInfoType.Address),s.showMobile=c.includes(_util.EnumWinnerInfoType.Tel),s.showEmail=c.includes(_util.EnumWinnerInfoType.Email),s.showSubmit=s.showMobile||s.showAddress||s.showReceiverName||s.showEmail,s.ifAward=o===_util.LotteryStatus.WIN,s.fulfilled=!0,(s.showReceiverName&&!s.ReceiverName||s.showAddress&&!s.AwardAddress||s.showMobile&&!s.AwardTel||s.showEmail&&!s.AwardEmail)&&(s.fulfilled=!1),this.setData({data:s,status:s.status})}catch(t){console.error(t)}},getCurrentLottery:function(t){var e=(this.store.get("imageTextInfo")||{}).imageText,r=null;return null==e||e.forEach(function(e){null==e||e.Context.forEach(function(e){var n;e.ImageTextType!==_constant.ImageTextTypes.LOTTERY||(null===(n=e.AwardContext)||void 0===n?void 0:n.ID)!==t||(r=e.AwardContext)})}),r},openLottery:function(t){var e=this;return _asyncToGenerator(_regeneratorRuntime().mark(function r(){var n,a,o;return _regeneratorRuntime().wrap(function(r){for(;;)switch(r.prev=r.next){case 0:if(n=e.store.get("imageTextInfo")||{},a=n.imageText,o=e.getCurrentLottery(t)){r.next=6;break}return e.sdkInstance.reportLogger("mini_sdk_lottery_no_current_data",{imageText:a,lotteryId:t}),e.setAwardInfo(!1,null),r.abrupt("return");case 6:if(e.setCurrentData(o),e.reportTea("open_lottery_modal",{awardId:t}),![_util.LotteryServerStatus.BeforeOpen,_util.LotteryServerStatus.Opening].includes(o.Status)){r.next=11;break}return wx.showToast({title:"开奖中",icon:"none"}),r.abrupt("return");case 11:if(![_util.LotteryServerStatus.Opened].includes(o.Status)){r.next=14;break}return e.attendLottery(t),r.abrupt("return");case 14:if(![_util.LotteryServerStatus.Delete,_util.LotteryServerStatus.UnOpened,_util.LotteryServerStatus.Retract].includes(o.Status)){r.next=18;break}return wx.showToast({title:"该抽奖已结束",icon:"none"}),e.setAwardInfo(!1,null),r.abrupt("return");case 18:e.checkHasUnCompletedConditions(e.data.undoLotteryMap[t])&&e.attendLottery(t,!1,!0),e.setData({type:"detail"}),e.setAwardInfo(!0,t);case 21:case"end":return r.stop()}},r)}))()},checkHasUnCompletedConditions:function(t){return t&&Boolean(Object.keys(t).some(function(e){return t[e]===_util.ConditionCompleteFlag.NOT_COMPLETED}))},attendLotteryTrigger:function(t){this.attendLottery(t.detail.lotteryId,!0)},attendLottery:function(t){var e=arguments,r=this;return _asyncToGenerator(_regeneratorRuntime().mark(function n(){var a,o,i,s,u,l,c,d,f,p;return _regeneratorRuntime().wrap(function(n){for(;;)switch(n.prev=n.next){case 0:if(a=e.length>1&&void 0!==e[1]&&e[1],o=e.length>2&&void 0!==e[2]&&e[2],r.isLogin(!0)){n.next=5;break}return!a&&r.setAwardInfo(!1,null),n.abrupt("return");case 5:return i=r.store.get("mainInfo"),s=i.activityId,u=r.createLotteryRequestId(),n.prev=7,r.setData({isAttendLoading:!0}),n.next=11,r.sdkInstance.request(_constant.REQUEST_URL_MAP.GET_AWARD_RECORD_INFO,{method:"POST",data:{ActivityID:s,AwardID:t}});case 11:if(l=n.sent,r.isLatestLotteryRequest(u,t)){n.next=14;break}return n.abrupt("return");case 14:if(r.setData({isAttendLoading:!1}),!l){n.next=37;break}n.t0=l.AwardResult,n.next=n.t0===_util.AwardResult.ATTEND?19:n.t0===_util.AwardResult.LOSE?23:n.t0===_util.AwardResult.WIN?28:n.t0===_util.AwardResult.INIT?33:34;break;case 19:return r.updateLotteryUserStatusNoTrans(t,_util.LotteryStatus.ENROLLED),r.setAwardInfo(!1,null),wx.showToast({title:"已参与本次抽奖",icon:"none"}),n.abrupt("return");case 23:return r.updateLotteryUserStatusNoTrans(t,_util.LotteryStatus.LOSE),r.setCurrentData(l,_util.LotteryStatus.LOSE),r.setData({type:"result"}),r.setAwardInfo(!0,t),n.abrupt("return");case 28:return r.updateLotteryUserStatusNoTrans(t,_util.LotteryStatus.WIN),r.setCurrentData(l,_util.LotteryStatus.WIN),r.setData({type:"result"}),r.setAwardInfo(!0,t),n.abrupt("return");case 33:throw new Error("");case 34:return r.setAwardInfo(!1,null),wx.showToast({title:"请稍后再试",icon:"none"}),n.abrupt("return");case 37:n.next=55;break;case 39:if(n.prev=39,n.t1=n.catch(7),r.isLatestLotteryRequest(u,t)){n.next=43;break}return n.abrupt("return");case 43:if(r.setData({isAttendLoading:!1}),c=r.getCurrentLottery(t),_util.LotteryServerStatus.Opened!==c.Status){n.next=53;break}return r.updateLotteryUserStatusNoTrans(t,_util.LotteryStatus.UNATTEND),r.setData({type:"result"}),r.setAwardInfo(!0,t),r.sdkInstance.reportLogger("lottery_show_win_result",{params:{awardId:t,Status:_util.LotteryStatus.UNATTEND,evModule:"LOTTERY"}}),n.abrupt("return");case 53:p={ActivityID:s,AwardID:t,BarragePwd:(0,_utils.getBySplit)(null!==(d=c.BarragePwd)&&void 0!==d?d:""),RegisterType:null===(f=r.store.get("userInfo"))||void 0===f?void 0:f.typeRegister},r.sdkInstance.request(_constant.REQUEST_URL_MAP.ATTEND_LOTTERY,{method:"POST",data:p}).then(function(e){if(r.isLatestLotteryRequest(u,t)){if(r.setData({isAttendLoading:!1}),e.Status)return r.data.undoLotteryMap[t]=null,r.updateLotteryUserStatusNoTrans(t,_util.LotteryStatus.ENROLLED),o?r.setCurrentData(r.data.data):(r.setAwardInfo(!1,null),wx.showToast({title:"参与成功",icon:"none"})),void r.reportTea("attend_lottery_success",{awardId:t});if(e.UndoAwardCondition)throw r.data.undoLotteryMap[t]=e.UndoAwardCondition,r.setCurrentData(r.data.data),new Error("抽奖条件未满足");throw new Error(e.Message||"参与失败,请稍后再试")}}).catch(function(e){r.isLatestLotteryRequest(u,t)&&(r.setData({isAttendLoading:!1}),"抽奖条件未满足"!==e.message&&(r.setAwardInfo(!1,null),wx.showToast({title:e.message,icon:"none"}),r.sdkInstance.reportLogger("attend_lottery_failed",{params:{awardId:t,error:e.message,evModule:"LOTTERY"}})))});case 55:case"end":return n.stop()}},n,null,[[7,39]])}))()},imageTextUpdate:function(t){var e=t.AwardID,r=t.Status,n=t.TotalCount;r>0&&this.updateOneLottery(e,r,n);var a,o=this.store.get("imageTextInfo")||{},i=o.imageText,s=void 0===i?[]:i,u=_createForOfIteratorHelper(s);try{for(u.s();!(a=u.n()).done;){var l,c=_createForOfIteratorHelper(a.value.Context);try{for(c.s();!(l=c.n()).done;){var d=l.value;if((null==d?void 0:d.ImageTextType)===_constant.ImageTextTypes.LOTTERY){var f,p=d.AwardContext;if(p&&p.ID===e)return r>0&&(p.Status=r),n>0&&(p.TotalCount||(p.TotalCount=n),p.TotalCount0&&(o.push({lotteryId:y,status:h}),h===_util.LotteryServerStatus.Ing&&i.push(y)))}catch(t){c.e(t)}finally{c.f()}}}catch(t){s.e(t)}finally{s.f()}if(o.length&&t.updateLotteryUserStatus(o),!i.length){e.next=13;break}return e.next=12,t.syncUserStatus(i);case 12:t.store.set("imageTextInfo",_objectSpread(_objectSpread({},r),{},{imageText:a}));case 13:case"end":return e.stop()}},e)}))()},syncUserStatus:function(t){var e=this;return _asyncToGenerator(_regeneratorRuntime().mark(function r(){var n,a,o,i;return _regeneratorRuntime().wrap(function(r){for(;;)switch(r.prev=r.next){case 0:if(e.isLogin()){r.next=2;break}return r.abrupt("return");case 2:return n=[],r.next=5,Promise.all(t.map(function(){var t=_asyncToGenerator(_regeneratorRuntime().mark(function t(r){var a,o,i;return _regeneratorRuntime().wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return a=e.store.get("mainInfo"),o=a.activityId,t.prev=1,t.next=4,e.sdkInstance.request(_constant.REQUEST_URL_MAP.GET_AWARD_RECORD_INFO,{method:"POST",data:{ActivityID:o,AwardID:r}});case 4:(i=t.sent).AwardResult&&i.AwardResult===_util.AwardResult.ATTEND&&n.push(r),t.next=11;break;case 8:t.prev=8,t.t0=t.catch(1),console.error(t.t0);case 11:case"end":return t.stop()}},t,null,[[1,8]])}));return function(e){return t.apply(this,arguments)}}()));case 5:n.length>0&&(a=e.store.get("lotteriesUserStatus")||{},o=a.lotteriesUserStatus,i=void 0===o?{}:o,n.forEach(function(t){return i[t]=_util.LotteryStatus.ENROLLED}),e.store.set("lotteriesUserStatus",{lotteriesUserStatus:i}));case 6:case"end":return r.stop()}},r)}))()},bindSocketEvents:function(){var t,e=this;null===(t=this.socket)||void 0===t||t.on(_socket.WsMethod.imageTextLotteryOrCheckInUpdate,function(t){t.SendType!==_constant.ImageTextTypes.CHECKIN&&(e.sdkInstance.reportLogger("mini_sdk_lottery_update",{data:t}),e.imageTextUpdate(t))})},setCheckedCheckIn:function(){var t=this.store.get("mainInfo").activityId,e=(this.store.get("userInfo")||{}).userId,r=wx.getStorageSync("checkedId");if(e&&r&&JSON.parse(r)[t+"_"+e]){var n=JSON.parse(r)[t+"_"+e].slice(1);this.setData({checkedCheckIn:n})}},reportTea:function(t){var e,r,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};null===(e=this.sdkInstance)||void 0===e||null===(r=e.reportTea)||void 0===r||r.call(e,t,_objectSpread(_objectSpread({},n),{},{terminal:"miniprogram",evModule:"LOTTERY"})),teaEventMap[t]&&this.sdkInstance.emit(teaEventMap[t],_objectSpread({},n))}}});