"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=exports.WsMethod=void 0;var _fws=require("../lib/fws");function _regeneratorRuntime(){_regeneratorRuntime=function(){return e};var e={},t=Object.prototype,r=t.hasOwnProperty,n=Object.defineProperty||function(e,t,r){e[t]=r.value},o="function"==typeof Symbol?Symbol:{},i=o.iterator||"@@iterator",a=o.asyncIterator||"@@asyncIterator",s=o.toStringTag||"@@toStringTag";function c(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{c({},"")}catch(e){c=function(e,t,r){return e[t]=r}}function u(e,t,r,o){var i=t&&t.prototype instanceof f?t:f,a=Object.create(i.prototype),s=new T(o||[]);return n(a,"_invoke",{value:S(e,r,s)}),a}function l(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}e.wrap=u;var d={};function f(){}function h(){}function y(){}var p={};c(p,i,function(){return this});var v=Object.getPrototypeOf,g=v&&v(v(P([])));g&&g!==t&&r.call(g,i)&&(p=g);var m=y.prototype=f.prototype=Object.create(p);function _(e){["next","throw","return"].forEach(function(t){c(e,t,function(e){return this._invoke(t,e)})})}function b(e,t){function o(n,i,a,s){var c=l(e[n],e,i);if("throw"!==c.type){var u=c.arg,d=u.value;return d&&"object"==_typeof(d)&&r.call(d,"__await")?t.resolve(d.__await).then(function(e){o("next",e,a,s)},function(e){o("throw",e,a,s)}):t.resolve(d).then(function(e){u.value=e,a(u)},function(e){return o("throw",e,a,s)})}s(c.arg)}var i;n(this,"_invoke",{value:function(e,r){function n(){return new t(function(t,n){o(e,r,t,n)})}return i=i?i.then(n,n):n()}})}function S(e,t,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 s=w(a,r);if(s){if(s===d)continue;return s}}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 c=l(e,t,r);if("normal"===c.type){if(n=r.done?"completed":"suspendedYield",c.arg===d)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n="completed",r.method="throw",r.arg=c.arg)}}}function w(e,t){var r=t.method,n=e.iterator[r];if(void 0===n)return t.delegate=null,"throw"===r&&e.iterator.return&&(t.method="return",t.arg=void 0,w(e,t),"throw"===t.method)||"return"!==r&&(t.method="throw",t.arg=new TypeError("The iterator does not provide a '"+r+"' method")),d;var o=l(n,e.iterator,t.arg);if("throw"===o.type)return t.method="throw",t.arg=o.arg,t.delegate=null,d;var i=o.arg;return i?i.done?(t[e.resultName]=i.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,d):i:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,d)}function k(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function E(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function T(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(k,this),this.reset(!0)}function P(e){if(e){var t=e[i];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var n=-1,o=function t(){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 s=r.call(i,"catchLoc"),c=r.call(i,"finallyLoc");if(s&&c){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),E(r),d}},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;E(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:P(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=void 0),d}},e}function asyncGeneratorStep(e,t,r,n,o,i,a){try{var s=e[i](a),c=s.value}catch(e){return void r(e)}s.done?t(c):Promise.resolve(c).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 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;t1&&void 0!==arguments[1]?arguments[1]:void 0,n=normalizeMethodKey(e);if(n&&this.methodHandler.has(n))if(r){var o=null!==(t=this.methodHandler.get(n))&&void 0!==t?t:[],i=o.indexOf(r);i>-1&&o.splice(i,1),this.methodHandler.set(n,o)}else this.methodHandler.delete(n);else console.log("不存在callback")}},{key:"startHeartCheck",value:function(){this.heartStop(),this.heartStart()}},{key:"heartStart",value:function(){var e=this;clearTimeout(this.pingTimeoutId),this.pingTimeoutId=setTimeout(function(){e.socketTask.send({data:e.PINGPONGMESSAGE}),clearTimeout(e.pongTimeoutId),e.pongTimeoutId=setTimeout(function(){e.heartTimeout()},e.pongTimeoutDelay)},this.pingTimeoutDelay)}},{key:"heartStop",value:function(){clearTimeout(this.pingTimeoutId),this.pingTimeoutId=null,clearTimeout(this.pongTimeoutId),this.pongTimeoutId=null}},{key:"heartTimeout",value:function(){if(console.warn("心跳超时"),++this.timeoutCount>=this.timeoutLimit)return console.warn("try 心跳超时,超过限制次数"),void this.createSocket();this.startHeartCheck()}},{key:"send",value:function(e,t){var r=this;if(this.socketTask&&!this.isDestroyed){var n=getEnv(),o={SeqID:1,LogID:Date.now(),service:"staging"===n.envName?20282:1,method:null!=e?e:0,payload_encoding:"text",payload_type:"tts",payload:JSON.stringify(t)},i=(0,_fws.encodeFrame)(o),a=i.buffer.slice(i.byteOffset,i.byteLength+i.byteOffset);this.socketTask.send({data:a,fail:function(e){r.report("mini_sdk_ws_send_fail",{error:e}),console.log("WS send Fail",e)},success:function(){r.report("mini_sdk_ws_send_success",{content:o}),console.log("WS send Success")}})}}},{key:"destroy",value:function(){var e,t,r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"manual";this.report("mini_sdk_ws_destroy",{reason:r}),this.isDestroyed=!0,this.heartStop();try{var n,o;null===(n=this.socketTask)||void 0===n||null===(o=n.close)||void 0===o||o.call(n)}catch(e){}null===(e=this.methodHandler)||void 0===e||null===(t=e.clear)||void 0===t||t.call(e),this.socketTask=null}}]),e}(),readArrayBuffer=function(e){var t=(0,_fws.decodeFrame)(new Uint8Array(e));try{var r;if(isGzJsonPayload(t)){var n=normalizeDecodedGzJsonPayload(t.payload);if(void 0!==n)return t.textPayload=n,t.payloadValue=n,t.eventPayload=n,t;if(null==pakoLib||!pakoLib.ungzip)return t.payloadParseError="missing gzip decoder",t.eventPayload={payloadType:t.payloadType,payloadEncoding:t.payloadEncoding,payload:t.payload},t;var o=t.payload instanceof Uint8Array?t.payload:new Uint8Array(t.payload),i=JSON.parse(pakoLib.ungzip(o,{to:"string"}));return t.textPayload=i,t.payloadValue=i,t.eventPayload=i,t}var a=(0,_fws.readU8a)(t.payload),s=safeParsePayloadText(a);return t.textPayload=s,t.eventPayload=s,Array.isArray(s)&&null!==(r=s[0])&&void 0!==r&&r.SendMsg?t.payloadValue=safeParsePayloadText(s[0].SendMsg):t.payloadValue=s,t}catch(e){return t.payloadParseError=e,t.eventPayload=void 0!==t.textPayload?t.textPayload:{payloadType:t.payloadType,payloadEncoding:t.payloadEncoding,payload:t.payload},t}},urlAddQueryDeal=function(e,t){var r="",n=e;return"object"===_typeof(t)&&Object.keys(t).length&&(Object.keys(t).forEach(function(e){r+="&".concat(e,"=").concat(t[e])}),n=e.includes("?")?"".concat(e).concat(r):"".concat(e,"?").concat(r.substring(1))),n},_default=WS;exports.default=_default;