"use strict";
const common_vendor = require("../common/vendor.js");
const utils_common = require("../utils/common.js");
const utils_sseParse = require("../utils/sseParse.js");
getApp();
const promistHandle = (res2) => {
  if (typeof res2.data.data == "string" && res2.data.data.constructor == String) {
    res2.data = JSON.parse(utils_common.desDecrypt(res2.data.data, getApp().globalData.apiSecretKey));
  }
  let resp = "";
  if (res2.data.RespCode == "10000" || res2.data.resultCode == "0") {
    resp = [];
    if (res2.data.Data instanceof Array || res2.data.Data instanceof Object) {
      resp = res2.data.Data;
    } else if (res2.data.WxPayConfigKey || res2.data.resultCode == "0" || res2.data.configKey || res2.data.result || res2.data.clientId) {
      resp = res2.data;
    }
  } else {
    if (res2.data.RespCode == 503) {
      jumpErrorPage();
    }
    if (res2.data.RespCode != "401") {
      resp = res2.data.RespMessage || res2.data.errMsg;
    }
  }
  return resp;
};
const catchPromise = (resp, callBack, showModal = true) => {
  if (resp === "") {
    return false;
  }
  if (typeof resp != "object") {
    if (!showModal) {
      return false;
    }
    utils_common.showModal(resp);
    return false;
  } else {
    if (callBack) {
      return callBack();
    }
  }
};
const promistHandleNew = (res2) => {
  if (typeof res2.data.data == "string" && res2.data.data.constructor == String) {
    res2.data = JSON.parse(utils_common.desDecrypt(res2.data.data, getApp().globalData.apiSecretKey));
  }
  let resp = "";
  if (res2.data.RespCode == "10000" || res2.data.resultCode == "0") {
    resp = [];
    if (res2.data.Data instanceof Array || res2.data.Data instanceof Object) {
      resp = res2.data.Data;
    } else if (res2.data.WxPayConfigKey || res2.data.resultCode == "0" || res2.data.configKey || res2.data.result || res2.data.clientId) {
      resp = res2.data;
    }
  } else {
    if (res2.data.RespCode == 503) {
      jumpErrorPage();
    }
    if (res2.data.RespCode != "401") {
      resp = res2.data.RespMessage || res2.data.errMsg;
    }
  }
  return {
    resp,
    resData: res2.data
  };
};
const catchPromiseNew = (data, callBack, option = {
  showModal: true,
  showModalCallBack: () => {
  }
}) => {
  if (data.resp === "") {
    return {
      resp: false,
      resData: data.resData
    };
  }
  if (typeof data.resp != "object") {
    if (!option.showModal || data.resData.RespCode == 503) {
      return {
        resp: false,
        resData: data.resData
      };
    }
    utils_common.showModal(data.resp, option.showModalCallBack);
    return {
      resp: false,
      resData: data.resData
    };
  } else {
    if (callBack) {
      return callBack();
    }
  }
};
const jumpErrorPage = () => {
  const [currentPage] = getCurrentPages();
  const maintainPage = "pages/st1/business/errorPage/maintain/maintain";
  const offlinePage = "pages/st1/business/errorPage/offline/offline";
  const isMaintainPage = currentPage.route.indexOf(maintainPage) > 0;
  const isOfflinePage = currentPage.route.indexOf(offlinePage) > 0;
  let jumpPage = "";
  if (res.data.RespCode == "maintain" && !isMaintainPage) {
    jumpPage = maintainPage;
  }
  if (res.data.RespCode == "offline" && !isOfflinePage) {
    jumpPage = offlinePage;
  }
  jumpPage && common_vendor.index.reLaunch({
    url: `/${jumpPage}`
  });
};
function arrayBufferToString(arrayBuffer) {
  let binary = "";
  const bytes = new Uint8Array(arrayBuffer);
  const len = bytes.byteLength;
  for (let i = 0; i < len; i++) {
    binary += String.fromCharCode(bytes[i]);
  }
  return decodeURIComponent(escape(binary));
}
const sseChunkDataHandle = (onChunkReceived) => {
  let ssePaser = utils_sseParse.createSSEParser(onChunkReceived);
  return (res2) => {
    try {
      let data = arrayBufferToString(res2.data);
      common_vendor.index.__f__("log", "at service/handle.js:173", "==>", data);
      ssePaser.parse(data);
    } catch (e) {
      common_vendor.index.__f__("error", "at service/handle.js:176", e);
    }
  };
};
const handle = {
  promistHandle,
  catchPromise,
  promistHandleNew,
  catchPromiseNew,
  sseChunkDataHandle
};
exports.handle = handle;
//# sourceMappingURL=../../.sourcemap/mp-alipay/service/handle.js.map
