"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.initReservationModule=exports.getReservationService=exports.destroyReservationService=exports.default=void 0;var _events=_interopRequireDefault(require("../utils/events")),_constant=require("../utils/constant");function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function ownKeys(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),r.push.apply(r,n)}return r}function _objectSpread(e){for(var t=1;t=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var s=r.call(i,"catchLoc"),u=r.call(i,"finallyLoc");if(s&&u){if(this.prev=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),S(r),v}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var o=n.arg;S(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:T(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=void 0),v}},e}function asyncGeneratorStep(e,t,r,n,o,i,a){try{var s=e[i](a),u=s.value}catch(e){return void r(e)}s.done?t(u):Promise.resolve(u).then(n,o)}function _asyncToGenerator(e){return function(){var t=this,r=arguments;return new Promise(function(n,o){var i=e.apply(t,r);function a(e){asyncGeneratorStep(i,n,o,a,s,"next",e)}function s(e){asyncGeneratorStep(i,n,o,a,s,"throw",e)}a(void 0)})}}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var r=0;r0&&void 0!==arguments[0]?arguments[0]:{};_classCallCheck(this,e),this.updateContext(t)}var t,r,n,o;return _createClass(e,[{key:"updateContext",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.store,r=e.request,n=e.reportTea,o=e.emitEvent,i=e.getDeviceId;t&&(this.store=t),r&&(this.request=r),n&&(this.reportTea=n),o&&(this.emitEvent=o),i&&(this.getDeviceId=i)}},{key:"isReady",value:function(){return!!(this.store&&this.request&&this.reportTea&&this.emitEvent)}},{key:"init",value:function(e){var t,r;if(this.isReady()){var n=(e||{}).Basic;if(n){var o=1===n.IsReservationEnable,i=null!==(t=n.IsReservationSmsEnable)&&void 0!==t?t:0,a=null!==(r=n.AutoReservationWindowEnable)&&void 0!==r?r:0;this.store.set("reservation.isReservationEnable",o),this.store.set("reservation.isReservationSmsEnable",i),this.store.set("reservation.autoReservationWindowEnable",a),o||this.store.set("reservation.isReserved",!1),o&&n.Status===_constant.LIVE_STATUS.预告&&this.getReservationStatus()}}}},{key:"getReservationStatus",value:(o=_asyncToGenerator(_regeneratorRuntime().mark(function e(){var t,r,n,o,i,a,s,u=this;return _regeneratorRuntime().wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(this.isReady()){e.next=2;break}return e.abrupt("return",!1);case 2:if(t=this.store.get("liveInfo")||{},r=t.ActivityId,n=t.Basic,o=this.store.get("userInfo")||{},i=function(){return u.store.set("reservation.isReserved",!1),u.store.set("reservation.statusLoaded",!0),!1},1===(null==n?void 0:n.IsReservationEnable)&&(null==n?void 0:n.Status)===_constant.LIVE_STATUS.预告&&o.userId){e.next=9;break}return e.abrupt("return",i());case 9:return e.next=11,this.request(_constant.REQUEST_URL_MAP.RESERVATION_STATUS_GET,{method:"GET",data:{ActivityId:r}}).catch(function(e){console.log("getReservationStatus error",e)});case 11:if(a=e.sent){e.next=14;break}return e.abrupt("return",i());case 14:return s=(null==a?void 0:a.IsReserved)||!1,this.store.set("reservation.isReserved",s),this.store.set("reservation.statusLoaded",!0),e.abrupt("return",s);case 18:case"end":return e.stop()}},e,this)})),function(){return o.apply(this,arguments)})},{key:"submitReservationLogin",value:(n=_asyncToGenerator(_regeneratorRuntime().mark(function e(t){var r,n,o,i,a,s,u;return _regeneratorRuntime().wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(this.isReady()){e.next=2;break}return e.abrupt("return");case 2:return r=this.store.get("liveInfo")||{},n=this.store.get("userInfo")||{},o=r.ActivityId,i={ActivityId:o,ReservationType:1,RegisterType:Number(n.typeRegister||0)},a=!1,e.next=9,this.request(_constant.REQUEST_URL_MAP.RESERVATION_LOGIN,{method:"GET",data:i}).catch(function(e){var t=getReservationErrorMessage(e);"请勿重复预约"===t||t===_constant.MESSAGE_ERROR.REPEAT_RESERVATION?a=!0:wx.showToast({title:"预约失败,请重试",icon:"none"})});case 9:((s=e.sent)&&s.Validity||a)&&(a?wx.showToast({title:"已预约过此直播间",icon:"none"}):(wx.showToast({title:"预约成功",icon:"success"}),this.store.set("reservation.isReserved",!0),this.emitEvent(_events.default.PUBLIC.reservation.attend,{tel:""})),this.reportTea("appointment_success",{isRepeat:a,uid:null===(u=this.getDeviceId)||void 0===u?void 0:u.call(this),aid:o}),t&&t(!0));case 11:case"end":return e.stop()}},e,this)})),function(e){return n.apply(this,arguments)})},{key:"sendVerifyCode",value:(r=_asyncToGenerator(_regeneratorRuntime().mark(function e(t){var r,n,o,i=this;return _regeneratorRuntime().wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(this.isReady()){e.next=2;break}return e.abrupt("return");case 2:return r=this.store.get("liveInfo")||{},n=r.ActivityId,e.next=6,this.request(_constant.REQUEST_URL_MAP.VERIFY_CODE_SEND,{method:"GET",data:{ActivityId:n,Tel:t}}).catch(function(e){var t=getReservationErrorMessage(e);return t===_constant.MESSAGE_ERROR.NOTIFY_TOO_FREQUENCY?{error:"发送验证码太频繁"}:(i.reportTea("captcha_send_fail",{error:t,evModule:"RESERVATION"}),{error:"请求失败,请重试"})});case 6:return o=e.sent,e.abrupt("return",o);case 8:case"end":return e.stop()}},e,this)})),function(e){return r.apply(this,arguments)})},{key:"submitSmsReservation",value:(t=_asyncToGenerator(_regeneratorRuntime().mark(function e(t,r){var n,o,i,a,s,u,c;return _regeneratorRuntime().wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(this.isReady()){e.next=2;break}return e.abrupt("return",{success:!1,errMsg:"请求失败,请重试"});case 2:return n=this.store.get("liveInfo")||{},o=this.store.get("userInfo")||{},i=n.ActivityId,a={ActivityId:i,Tel:t.tel,VerifyCode:t.verifyCode,ReservationType:1,RegisterType:Number(o.typeRegister||0)},s=!1,e.next=9,this.request(_constant.REQUEST_URL_MAP.VERIFY_CODE_CHECK,{method:"GET",data:a}).catch(function(e){var t=getReservationErrorMessage(e);return t===_constant.MESSAGE_ERROR.NOTIFY_TOO_FREQUENCY?{error:"发送验证码太频繁"}:t===_constant.MESSAGE_ERROR.VERIFICATION_CODE_ERROR?{error:"验证码错误,请重新输入"}:t===_constant.MESSAGE_ERROR.VERIFICATION_CODE_EXPIRATION?{error:"验证码超时,请重新获取"}:t===_constant.MESSAGE_ERROR.REPEAT_RESERVATION?(s=!0,{}):t===_constant.MESSAGE_ERROR.WHITE_LIST_PHONE?{error:"请输入白名单内手机号"}:{error:"请求失败,请重试"}});case 9:if(null==(u=e.sent)||!u.error){e.next=12;break}return e.abrupt("return",{success:!1,errMsg:u.error});case 12:if(!(u&&u.Validity||s)){e.next=17;break}return s?wx.showToast({title:"该手机号预约过此直播间",icon:"none"}):(wx.showToast({title:"预约成功",icon:"success"}),this.store.set("reservation.isReserved",!0),this.emitEvent(_events.default.PUBLIC.reservation.attend,{tel:t.tel})),this.reportTea("appointment_success",{isRepeat:s,uid:null===(c=this.getDeviceId)||void 0===c?void 0:c.call(this),aid:i,evModule:"RESERVATION"}),r&&r(!0),e.abrupt("return",{success:!0});case 17:return e.abrupt("return",{success:!1,errMsg:"请求失败,请重试"});case 18:case"end":return e.stop()}},e,this)})),function(e,r){return t.apply(this,arguments)})},{key:"showReservationModal",value:function(){var e;this.isReady()&&(null!==(e=(this.store.get("mainInfo")||{}).options)&&void 0!==e&&e.disableReservationCell||(this.store.set("reservation.modalVisible",!0),this.reportTea("appointment_click",{evModule:"RESERVATION"})))}},{key:"hideReservationModal",value:function(){this.isReady()&&this.store.set("reservation.modalVisible",!1)}}]),e}(),getReservationService=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=getSdkInstanceId(e);if(!r)return null;var n=_objectSpread(_objectSpread({},getContextFromSdk(e)),t),o=serviceMap.get(r);return o?(o.updateContext(n),o):(o=new ReservationService(n),serviceMap.set(r,o),o)};exports.getReservationService=getReservationService;var initReservationModule=function(e,t){var r=getReservationService(e);return null==r||r.init(t),r};exports.initReservationModule=initReservationModule;var destroyReservationService=function(e){var t=getSdkInstanceId(e);t&&serviceMap.delete(t)};exports.destroyReservationService=destroyReservationService;var _default=ReservationService;exports.default=_default;