import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
import _createClass from '@babel/runtime/helpers/createClass';
import { initEnv } from '../../env/env.mjs';
import { callJsBrowserAdapter, addMsdkNativeCallbackListener, sendToMsdkNative } from '../../msdk/msdk.mjs';
import { LocationFlag } from '../location-interface.mjs';
import '@babel/runtime/helpers/typeof';
import '../../loader/little-loader.mjs';

// import { GAME_HLDDZ, GAME_GP } from '../../tools/game';
// import ConfigInfo from '../../config';
var MsdkLocation = /*#__PURE__*/function () {
  function MsdkLocation() {
    _classCallCheck(this, MsdkLocation);
  }
  return _createClass(MsdkLocation, [{
    key: "getLocation",
    value: function getLocation(options) {
      return new Promise(function (resolve, reject) {
        if (initEnv().isMsdk && options.useMsdkLocation
        // && (ConfigInfo.instance.getTipGid() === GAME_HLDDZ || ConfigInfo.instance.getTipGid() === GAME_GP)
        ) {
          callJsBrowserAdapter().then(function () {
            addMsdkNativeCallbackListener(function (data) {
              console.log('addMsdkNativeCallbackListener', data);
              var response;
              try {
                response = JSON.parse(data);
              } catch (_a) {}
              if ((response === null || response === void 0 ? void 0 : response.cmd) === 'reqLocation') {
                if ((response === null || response === void 0 ? void 0 : response.ret) === 0) {
                  var location = {
                    lat: response.latitude || response.lat,
                    lng: response.longitude || response.lng
                  };
                  resolve({
                    location: location,
                    flag: LocationFlag.LocationSuccess
                  });
                } else {
                  reject({
                    flag: response.ret,
                    errorMsg: response.errorMsg
                  });
                }
              }
            });
            var requestLocationData = '';
            // if (ConfigInfo.instance.getTipGid() === GAME_HLDDZ) {
            if (options.msdkLocationReqStr) {
              requestLocationData = options.msdkLocationReqStr; // 'fakelink://&Operation=ReqLocation';
            } else {
              var reqData = {
                cmd: 'reqLocation'
              };
              requestLocationData = JSON.stringify(reqData);
            }
            sendToMsdkNative(requestLocationData);
          })["catch"](function () {
            console.log('addMsdkNativeCallbackListener error');
            reject({
              flag: LocationFlag.LocationFailed
            });
          });
        } else {
          reject({
            flag: LocationFlag.LocationFailed
          });
        }
      });
    }
  }]);
}();

export { MsdkLocation as default };
