"use strict";var _utils=require("../../../utils/utils"),_constant=require("../../../utils/constant"),_voteService=require("../../../services/vote-service"),_vote=require("../../../utils/vote"),_theme=require("../../../utils/theme");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)}function _regeneratorRuntime(){_regeneratorRuntime=function(){return t};var t={},e=Object.prototype,r=e.hasOwnProperty,n=Object.defineProperty||function(t,e,r){t[e]=r.value},o="function"==typeof Symbol?Symbol:{},i=o.iterator||"@@iterator",a=o.asyncIterator||"@@asyncIterator",u=o.toStringTag||"@@toStringTag";function s(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{s({},"")}catch(t){s=function(t,e,r){return t[e]=r}}function l(t,e,r,o){var i=e&&e.prototype instanceof f?e:f,a=Object.create(i.prototype),u=new x(o||[]);return n(a,"_invoke",{value:_(t,r,u)}),a}function c(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=l;var d={};function f(){}function v(){}function h(){}var p={};s(p,i,function(){return this});var y=Object.getPrototypeOf,m=y&&y(y(O([])));m&&m!==e&&r.call(m,i)&&(p=m);var g=h.prototype=f.prototype=Object.create(p);function b(t){["next","throw","return"].forEach(function(e){s(t,e,function(t){return this._invoke(e,t)})})}function w(t,e){function o(n,i,a,u){var s=c(t[n],t,i);if("throw"!==s.type){var l=s.arg,d=l.value;return d&&"object"==_typeof(d)&&r.call(d,"__await")?e.resolve(d.__await).then(function(t){o("next",t,a,u)},function(t){o("throw",t,a,u)}):e.resolve(d).then(function(t){l.value=t,a(l)},function(t){return o("throw",t,a,u)})}u(s.arg)}var i;n(this,"_invoke",{value:function(t,r){function n(){return new e(function(e,n){o(t,r,e,n)})}return i=i?i.then(n,n):n()}})}function _(t,e,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var u=S(a,r);if(u){if(u===d)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var s=c(t,e,r);if("normal"===s.type){if(n=r.done?"completed":"suspendedYield",s.arg===d)continue;return{value:s.arg,done:r.done}}"throw"===s.type&&(n="completed",r.method="throw",r.arg=s.arg)}}}function S(t,e){var r=e.method,n=t.iterator[r];if(void 0===n)return e.delegate=null,"throw"===r&&t.iterator.return&&(e.method="return",e.arg=void 0,S(t,e),"throw"===e.method)||"return"!==r&&(e.method="throw",e.arg=new TypeError("The iterator does not provide a '"+r+"' method")),d;var o=c(n,t.iterator,e.arg);if("throw"===o.type)return e.method="throw",e.arg=o.arg,e.delegate=null,d;var i=o.arg;return i?i.done?(e[t.resultName]=i.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,d):i:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,d)}function V(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function I(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function x(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(V,this),this.reset(!0)}function O(t){if(t){var e=t[i];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,o=function e(){for(;++n=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var u=r.call(i,"catchLoc"),s=r.call(i,"finallyLoc");if(u&&s){if(this.prev=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),I(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 o=n.arg;I(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:O(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),d}},t}function asyncGeneratorStep(t,e,r,n,o,i,a){try{var u=t[i](a),s=u.value}catch(t){return void r(t)}u.done?e(s):Promise.resolve(s).then(n,o)}function _asyncToGenerator(t){return function(){var e=this,r=arguments;return new Promise(function(n,o){var i=t.apply(e,r);function a(t){asyncGeneratorStep(i,n,o,a,u,"next",t)}function u(t){asyncGeneratorStep(i,n,o,a,u,"throw",t)}a(void 0)})}}function ownKeys(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),r.push.apply(r,n)}return r}function _objectSpread(t){for(var e=1;e0&&void 0!==arguments[0]&&arguments[0];return 2===((null===(t=this.store)||void 0===t?void 0:t.get("mainInfo"))||{}).mode||(e&&this.triggerEvent("commentCheck",{from:"vote"}),!1)},getVoteId:function(t,e){return firstValid(null==t?void 0:t.VoteId,e)},normalizeVote:function(t,e){var r=(null==t?void 0:t.UserChose)||(null==t?void 0:t.UserChoice)||[],n=(r||[]).map(function(t){return String(t)}),o=((null==t?void 0:t.Options)||(null==t?void 0:t.VoteOptions)||[]).map(function(t,e){var r=firstValid(null==t?void 0:t.OptionSite,null==t?void 0:t.OptionId,null==t?void 0:t.OptionID,null==t?void 0:t.Id,null==t?void 0:t.ID,String(e+1));return _objectSpread(_objectSpread({},t),{},{OptionSite:r,OptionName:(0,_utils.getBySplit)((null==t?void 0:t.OptionName)||(null==t?void 0:t.Name)||""),checked:n.includes(String(r))})}),i=isMultipleVote(t);return _objectSpread(_objectSpread({},t),{},{VoteId:this.getVoteId(t,e),Title:(0,_utils.getBySplit)((null==t?void 0:t.Title)||""),Options:o,ChoiceType:i?"Multiple":"Single",IsMultiple:i,TotalCount:(null==t?void 0:t.TotalCount)||(null==t?void 0:t.TotalUserCount)||(null==t?void 0:t.JoinCount)||0,UserChose:r,Status:(null==t?void 0:t.Status)||"Export"})},findVoteFromImageText:function(t){for(var e,r=(null===(e=this.store)||void 0===e?void 0:e.get("imageTextInfo.imageText"))||[],n=0;n1&&void 0!==e[1]?e[1]:"manual",r.setData({loading:!0,errMsg:"",data:null,formValue:[],isVoted:!1,isShowResult:!1,isVoteClosed:!1}),null===(o=r.selectComponent("#vote-drawer"))||void 0===o||o.showFrame(),(a=r.findVoteFromImageText(t))&&(u=r.normalizeVote(a,t),s=!!(u.UserChose||[]).length,l=(0,_vote.isVoteEnded)(u),r.setData({data:u,formValue:u.UserChose||[],isVoted:s,isShowResult:s||l||"Before"===u.ResultView,isVoteClosed:l})),n.prev=5,c=r.store.get("liveInfo.ActivityId")||r.store.get("mainInfo.activityId"),n.next=9,(0,_voteService.getVoteDetail)({sdkInstance:r.sdkInstance,ActivityId:c,VoteId:t,triggerShow:i});case 9:d=n.sent,f=r.normalizeVote(_objectSpread(_objectSpread({},a||{}),d||{}),t),v=!!(f.UserChose||[]).length,h=(0,_vote.isVoteEnded)(f),r.setData({data:f,formValue:f.UserChose||[],isVoted:v,isShowResult:v||h||"Before"===f.ResultView,isVoteClosed:h,loading:!1}),n.next=20;break;case 16:n.prev=16,n.t0=n.catch(5),null===(p=r.sdkInstance)||void 0===p||null===(y=p.reportTea)||void 0===y||y.call(p,"mini_sdk_vote_get_fail",{voteId:t,error:n.t0}),r.setData({loading:!1,errMsg:a?"":"投票详情获取失败"});case 20:case"end":return n.stop()}},n,null,[[5,16]])}))()},closeVoteDrawer:function(){var t;null===(t=this.selectComponent("#vote-drawer"))||void 0===t||t.hideFrame()},onVoteDrawerCancel:function(){var t;null===(t=this.store)||void 0===t||t.set("VoteInfo",_objectSpread(_objectSpread({},this.store.get("VoteInfo")||{}),{},{visible:!1,currentId:null}))},updateOptionChecked:function(t){var e=this.data.data||{},r=t.map(function(t){return String(t)});this.setData({formValue:t,errMsg:"",data:_objectSpread(_objectSpread({},e),{},{Options:(e.Options||[]).map(function(t){return _objectSpread(_objectSpread({},t),{},{checked:r.includes(String(t.OptionSite))})})})})},showVoteToast:function(t){wx.showToast({title:t,icon:"none"})},valueChange:function(t){var e=Array.isArray(t.detail.value)?t.detail.value:[t.detail.value];this.updateOptionChecked(e)},formSubmit:function(){var t=this;return _asyncToGenerator(_regeneratorRuntime().mark(function e(){var r,n,o,i,a,u,s,l,c,d,f,v;return _regeneratorRuntime().wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.data.data||{},t.isLogin(!0)){e.next=3;break}return e.abrupt("return");case 3:if(t.data.formValue.length){e.next=6;break}return t.showVoteToast("请选择投票选项"),e.abrupt("return");case 6:if(!t.data.isVoteClosed&&"Deadline"!==r.Status&&"Delete"!==r.Status){e.next=9;break}return t.showVoteToast("投票已截止"),e.abrupt("return");case 9:if(n=r.VoteId){e.next=14;break}return t.showVoteToast("投票信息异常,请稍后重试"),null===(o=t.sdkInstance)||void 0===o||null===(i=o.reportTea)||void 0===i||i.call(o,"mini_sdk_vote_submit_invalid",{reason:"empty_vote_id"}),e.abrupt("return");case 14:return t.setData({submitting:!0,errMsg:""}),e.prev=15,s=t.store.get("liveInfo.ActivityId")||t.store.get("mainInfo.activityId"),l=t.store.get("userInfo.typeRegister"),e.next=20,(0,_voteService.submitVote)({sdkInstance:t.sdkInstance,ActivityId:s,VoteId:n,OptionType:t.data.formValue,RegisterType:l});case 20:c=e.sent,null===(a=t.sdkInstance)||void 0===a||null===(u=a.markVoteSubmitted)||void 0===u||u.call(a,n),d=t.normalizeVote(_objectSpread(_objectSpread(_objectSpread({},r),c||{}),{},{UserChose:(null==c?void 0:c.UserChose)||(null==c?void 0:c.UserChoice)||t.data.formValue}),n),t.setData({data:d,formValue:d.UserChose||t.data.formValue,isVoted:!!(d.UserChose||t.data.formValue||[]).length,isShowResult:!0,isVoteClosed:(0,_vote.isVoteEnded)(d),submitting:!1}),wx.showToast({title:"投票成功",icon:"success"}),e.next=32;break;case 27:e.prev=27,e.t0=e.catch(15),null===(f=t.sdkInstance)||void 0===f||null===(v=f.reportTea)||void 0===v||v.call(f,"mini_sdk_vote_submit_fail",{voteId:n,error:e.t0}),t.setData({submitting:!1}),t.showVoteToast((null===e.t0||void 0===e.t0?void 0:e.t0.message)||"提交失败,请稍后重试");case 32:case"end":return e.stop()}},e,null,[[15,27]])}))()}}});