"use strict";var _constant=require("../../../utils/constant"),_utils=require("../../../utils/utils"),_theme=require("../../../utils/theme"),_coupon=require("../../../utils/coupon"),_featureProcess=require("../../../utils/feature-process");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,n=e.hasOwnProperty,o=Object.defineProperty||function(t,e,n){t[e]=n.value},r="function"==typeof Symbol?Symbol:{},i=r.iterator||"@@iterator",u=r.asyncIterator||"@@asyncIterator",a=r.toStringTag||"@@toStringTag";function c(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{c({},"")}catch(t){c=function(t,e,n){return t[e]=n}}function s(t,e,n,r){var i=e&&e.prototype instanceof f?e:f,u=Object.create(i.prototype),a=new T(r||[]);return o(u,"_invoke",{value:S(t,n,a)}),u}function p(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=s;var l={};function f(){}function d(){}function h(){}var v={};c(v,i,function(){return this});var y=Object.getPrototypeOf,m=y&&y(y(I([])));m&&m!==e&&n.call(m,i)&&(v=m);var g=h.prototype=f.prototype=Object.create(v);function b(t){["next","throw","return"].forEach(function(e){c(t,e,function(t){return this._invoke(e,t)})})}function _(t,e){function r(o,i,u,a){var c=p(t[o],t,i);if("throw"!==c.type){var s=c.arg,l=s.value;return l&&"object"==_typeof(l)&&n.call(l,"__await")?e.resolve(l.__await).then(function(t){r("next",t,u,a)},function(t){r("throw",t,u,a)}):e.resolve(l).then(function(t){s.value=t,u(s)},function(t){return r("throw",t,u,a)})}a(c.arg)}var i;o(this,"_invoke",{value:function(t,n){function o(){return new e(function(e,o){r(t,n,e,o)})}return i=i?i.then(o,o):o()}})}function S(t,e,n){var o="suspendedStart";return function(r,i){if("executing"===o)throw new Error("Generator is already running");if("completed"===o){if("throw"===r)throw i;return{value:void 0,done:!0}}for(n.method=r,n.arg=i;;){var u=n.delegate;if(u){var a=w(u,n);if(a){if(a===l)continue;return a}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===o)throw o="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o="executing";var c=p(t,e,n);if("normal"===c.type){if(o=n.done?"completed":"suspendedYield",c.arg===l)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(o="completed",n.method="throw",n.arg=c.arg)}}}function w(t,e){var n=e.method,o=t.iterator[n];if(void 0===o)return e.delegate=null,"throw"===n&&t.iterator.return&&(e.method="return",e.arg=void 0,w(t,e),"throw"===e.method)||"return"!==n&&(e.method="throw",e.arg=new TypeError("The iterator does not provide a '"+n+"' method")),l;var r=p(o,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,l;var i=r.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,l):i:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function C(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 k(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function T(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(C,this),this.reset(!0)}function I(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 o=-1,r=function e(){for(;++o=0;--r){var i=this.tryEntries[r],u=i.completion;if("root"===i.tryLoc)return o("end");if(i.tryLoc<=this.prev){var a=n.call(i,"catchLoc"),c=n.call(i,"finallyLoc");if(a&&c){if(this.prev=0;--o){var r=this.tryEntries[o];if(r.tryLoc<=this.prev&&n.call(r,"finallyLoc")&&this.prev=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),k(n),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var o=n.completion;if("throw"===o.type){var r=o.arg;k(n)}return r}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:I(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},t}function asyncGeneratorStep(t,e,n,o,r,i,u){try{var a=t[i](u),c=a.value}catch(t){return void n(t)}a.done?e(c):Promise.resolve(c).then(o,r)}function _asyncToGenerator(t){return function(){var e=this,n=arguments;return new Promise(function(o,r){var i=t.apply(e,n);function u(t){asyncGeneratorStep(i,o,r,u,a,"next",t)}function a(t){asyncGeneratorStep(i,o,r,u,a,"throw",t)}u(void 0)})}}function _createForOfIteratorHelper(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!n){if(Array.isArray(t)||(n=_unsupportedIterableToArray(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var o=0,r=function(){};return{s:r,n:function(){return o>=t.length?{done:!0}:{done:!1,value:t[o++]}},e:function(t){throw t},f:r}}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 i,u=!0,a=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return u=t.done,t},e:function(t){a=!0,i=t},f:function(){try{u||null==n.return||n.return()}finally{if(a)throw i}}}}function _unsupportedIterableToArray(t,e){if(t){if("string"==typeof t)return _arrayLikeToArray(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?_arrayLikeToArray(t,e):void 0}}function _arrayLikeToArray(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,o=new Array(e);n1&&void 0!==arguments[1]?arguments[1]:{};null===(e=this.sdkInstance)||void 0===e||null===(n=e.reportTea)||void 0===n||n.call(e,t,_objectSpread(_objectSpread({},o),{},{terminal:"miniprogram",evModule:"coupon"}))},_hasAnyCoupons:function(){var t,e=_createForOfIteratorHelper(this.store.get("imageTextInfo.imageText")||[]);try{for(e.s();!(t=e.n()).done;){var n=t.value;if(n&&!n.IsDelete){var o,r=_createForOfIteratorHelper(n.Context||[]);try{for(r.s();!(o=r.n()).done;){var i=o.value;if(null!=i&&i.CouponContext)return!0}}catch(t){r.e(t)}finally{r.f()}}}}catch(t){e.e(t)}finally{e.f()}return!1},_getCouponList:function(){var t=this,e=this.store.get("imageTextInfo.imageText")||[],n=[],o=this.store.get("userInfo")||{},r=o.levelId,i=o.userId,u=(this.store.get("liveInfo")||{}).ActivityId,a=this.store.get("coupon.liveStatus"),c=this.store.get("coupon.watchTime");return e.forEach(function(e){e&&!e.IsDelete&&(e.Context||[]).forEach(function(e){var o;if((null==e?void 0:e.ImageTextType)===_constant.ImageTextTypes.COUPON||null!=e&&e.CouponContext){var s=null==e?void 0:e.CouponContext;if(s){var p=s.Coupon||s,l=i&&u&&(0,_coupon.isCouponPicked)(u,i,p.Id||s.Id)?1:0,f=(0,_coupon.calcCouponStatus)(p,l),d=f===_coupon.CouponStatusType.Pickup?f:null!==(o=s.couponStatus)&&void 0!==o?o:f,h=(0,_utils.getBySplit)(p.Name||"卡券"),v=(0,_utils.getBySplit)(p.Description||""),y=p.Pic||DefaultCouponUrl,m="";if(void 0!==p.Count&&void 0!==p.PickupCount&&-1!==p.Count){var g=Math.max(0,Number(p.Count)-Number(p.PickupCount));m="剩余 ".concat(g,"/").concat(p.Count)}var b=p.EndTime?"有效期至 ".concat(formatTime(p.EndTime)):"",_=t._getBtnState(d),S=(0,_coupon.isCouponLevelEligible)(null!=r?r:"",s),w=(0,_coupon.isCouponWatchEligible)(s,{liveStatus:a,watchTime:c,activityId:u});S&&n.push(_objectSpread(_objectSpread({},s),{},{_display:{title:h,desc:v,imageUrl:y,countText:m,timeText:b,btnText:_.btnText,btnDisabled:_.btnDisabled,isReceived:_.isReceived,couponStatus:d,levelEligible:S,watchEligible:w}}))}}})}),n.sort(function(t,e){return(e.SendTime||0)-(t.SendTime||0)})},_getBtnState:function(t){return t===_coupon.CouponStatusType.Sending?{btnText:"立即领取",btnDisabled:!1,isReceived:!1}:t===_coupon.CouponStatusType.SendCompleted?{btnText:"已领完",btnDisabled:!0,isReceived:!1}:t===_coupon.CouponStatusType.Pickup?{btnText:"已领取",btnDisabled:!0,isReceived:!0}:t===_coupon.CouponStatusType.Received?{btnText:_coupon.CouponReceivedLimitText,btnDisabled:!0,isReceived:!1}:t===_coupon.CouponStatusType.End?{btnText:"已结束",btnDisabled:!0,isReceived:!1}:{btnText:"立即领取",btnDisabled:!1,isReceived:!1}},openListDrawer:function(){var t,e;if(!(0,_featureProcess.lockFeatureProcess)(this.sdkInstance,_featureProcess.LockFeatureMap.Coupon)){var n=this._getCouponList();this.setData({couponList:n});var o=this.selectComponent("#coupon-list-drawer");o&&o.showFrame(),this.sdkInstance&&null!==(t=this.sdkInstance.EVENTS)&&void 0!==t&&null!==(e=t.coupon)&&void 0!==e&&e.getList&&this.sdkInstance.emit(this.sdkInstance.EVENTS.coupon.getList,{})}},closeListDrawer:function(){this.setData({})},onViewProducts:function(){var t=this.selectComponent("#coupon-list-drawer");t&&t.hideFrame&&t.hideFrame()},onCouponItemClick:function(t){if(!(0,_featureProcess.lockFeatureProcess)(this.sdkInstance,_featureProcess.LockFeatureMap.Coupon)){var e=t.currentTarget.dataset.id;this.sdkInstance.setShowCouponId(e,"click")}},onPickupFromList:function(t){var e=this;return _asyncToGenerator(_regeneratorRuntime().mark(function n(){var o,r,i,u,a;return _regeneratorRuntime().wrap(function(n){for(;;)switch(n.prev=n.next){case 0:if(!(0,_featureProcess.lockFeatureProcess)(e.sdkInstance,_featureProcess.LockFeatureMap.Coupon)){n.next=2;break}return n.abrupt("return");case 2:if(o=t.currentTarget.dataset.id,r=e.data.couponList.find(function(t){return t.Id==o||t.Coupon&&t.Coupon.Id==o})){n.next=6;break}return n.abrupt("return");case 6:if(i=r._display||{},u=i.couponStatus,a=i.watchEligible,u!==_coupon.CouponStatusType.End&&u!==_coupon.CouponStatusType.SendCompleted&&a){n.next=10;break}return e.sdkInstance.setShowCouponId(o,"click"),n.abrupt("return");case 10:return n.next=12,e._doPickup(r);case 12:e.setData({couponList:e._getCouponList()});case 13:case"end":return n.stop()}},n)}))()},_handleCouponPickupReceivedLimit:function(t){(0,_coupon.updateCouponContextStatusInStore)(this.store,t.Id,_coupon.CouponStatusType.Received),this.sdkInstance.setCouponPickupStatus(_coupon.CouponStatusType.Received),wx.showToast({title:_coupon.CouponReceivedLimitText,icon:"none"})},_doPickup:function(t){var e=this;return _asyncToGenerator(_regeneratorRuntime().mark(function n(){var o,r,i,u,a,c,s,p,l,f,d,h,v;return _regeneratorRuntime().wrap(function(n){for(;;)switch(n.prev=n.next){case 0:if(o=t.Coupon||t,r=e.store.get("liveInfo")||{},i=r.ActivityId,u=e.store.get("userInfo")||{},a=u.userId,c=u.levelId,i&&a){n.next=6;break}return n.abrupt("return");case 6:if(e._reportCouponTea("pick_up_coupon",{couponId:o.Id,isLogin:!0}),e.isLogin(!0)){n.next=9;break}return n.abrupt("return");case 9:if((0,_coupon.isCouponLevelEligible)(null!=c?c:"",t,function(t,n){return e._reportCouponTea(t,n)})){n.next=13;break}return wx.showToast({title:"当前等级不可领取",icon:"none"}),n.abrupt("return");case 13:if(s=e.store.get("coupon.liveStatus"),p=e.store.get("coupon.watchTime"),(0,_coupon.isCouponWatchEligible)(t,{liveStatus:s,watchTime:p,activityId:i},function(t,n){return e._reportCouponTea(t,n)})){n.next=19;break}return wx.showToast({title:"观看后可领取",icon:"none"}),n.abrupt("return");case 19:return n.next=21,e.sdkInstance.request(_constant.REQUEST_URL_MAP.COUPON_PICKUP,{method:"POST",data:{ActivityID:i,Id:o.Id}});case 21:if(l=n.sent,e._reportCouponTea("pick_up_coupon_status",{couponId:o.Id,status:null==l?void 0:l.Status,res:l}),f=null==l?void 0:l.Status,d=_coupon.CouponStatusType.Pickup,![_coupon.CouponPickupStatus.Success,_coupon.CouponPickupStatus.Pickup,_coupon.CouponPickupStatus.SendCompleted].includes(f)){n.next=29;break}f===_coupon.CouponPickupStatus.SendCompleted?d=_coupon.CouponStatusType.SendCompleted:(0,_coupon.markCouponPicked)(i,a,o.Id),n.next=48;break;case 29:if(f!==_coupon.CouponPickupStatus.Ended){n.next=38;break}if(!(null!=l&&l.EndTime&&o.SendTime&&l.EndTime0&&void 0!==arguments[0]&&arguments[0];return 2===(this.store.get("mainInfo")||{}).mode||(t&&this.triggerEvent("commentCheck"),!1)}}});