import { loadJS } from '../loader/loader.mjs';
import { WX_JS_SDK } from './config.mjs';
import '../loader/little-loader.mjs';

function configWx(_ref) {
  var apiList = _ref.apiList,
    openTagList = _ref.openTagList,
    getWxSignaturePromise = _ref.getWxSignaturePromise;
  return new Promise(function (resolve, reject) {
    loadJS(WX_JS_SDK).then(function () {
      getWxCfg({
        apiList: apiList,
        openTagList: openTagList,
        getWxSignaturePromise: getWxSignaturePromise
      }).then(function (wx) {
        resolve(wx);
      })["catch"](function (error) {
        console.log('configWx error : ', error);
        reject(error);
      });
    });
  });
}
/**
 * 请求微信鉴权接口
 * @param {*} apiList api列表
 * @param {*} openTagList openTag列表
 * @returns {Promise} 微信鉴权结果
 */
function getWxCfg(_ref2) {
  var apiList = _ref2.apiList,
    openTagList = _ref2.openTagList,
    getWxSignaturePromise = _ref2.getWxSignaturePromise;
  return new Promise(function (resolve, reject) {
    getWxSignaturePromise().then(function () {
      var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var _a;
      (_a = window === null || window === void 0 ? void 0 : window.wx) === null || _a === void 0 ? void 0 : _a.config({
        beta: true,
        debug: false,
        appId: data.wxappid,
        timestamp: data.timestamp,
        nonceStr: data.noncestr,
        signature: data.signature,
        jsApiList: apiList,
        openTagList: openTagList
      });
      window.wx.ready(function () {
        resolve(window.wx);
      });
      window.wx.error(function (err) {
        console.warn('wx config error : ', err);
        reject(err);
      });
    })["catch"](function (error) {
      reject(error);
      console.log('get_share_cfg error : ', error);
    });
  });
}

export { configWx };
