'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

/**
 * 在屏幕右上角注册一个隐藏触摸热区，连点 5 次（3 秒内）弹出环境切换菜单。
 * 触发区域：屏幕宽度 80%~100%、高度 0%~15% 的矩形区域。
 */
function setupEnvSwitchHotspot(envSwitcher) {
  var _a, _b;
  var iwx = typeof wx !== 'undefined' ? wx : typeof globalThis !== 'undefined' ? globalThis.wx : null;
  if (!iwx) {
    console.warn('[initApi] 当前环境缺少 wx，跳过隐藏热区注册');
    return;
  }
  // 正式版（release）禁用调试热区，避免普通用户误触发切到测试环境
  if (typeof iwx.getAccountInfoSync === 'function') {
    try {
      var envVersion = (_b = (_a = iwx.getAccountInfoSync()) === null || _a === void 0 ? void 0 : _a.miniProgram) === null || _b === void 0 ? void 0 : _b.envVersion;
      if (envVersion === 'release') {
        console.log('[initApi] envVersion=release，跳过隐藏热区注册');
        return;
      }
      console.log("[initApi] envVersion=".concat(envVersion, "\uFF0C\u6CE8\u518C\u9690\u85CF\u70ED\u533A"));
    } catch (e) {
      console.warn('[initApi] getAccountInfoSync 失败，按非 release 处理继续注册', e);
    }
  }
  if (typeof iwx.onTouchStart !== 'function' || typeof iwx.getSystemInfoSync !== 'function') {
    console.warn('[initApi] 当前环境缺少 wx.onTouchStart / getSystemInfoSync，跳过隐藏热区注册');
    return;
  }
  var sysInfo;
  try {
    sysInfo = iwx.getSystemInfoSync();
  } catch (e) {
    console.warn('[initApi] getSystemInfoSync 失败，跳过隐藏热区注册', e);
    return;
  }
  var HOTSPOT_X_RATIO = 0.8; // 横向：80% 之后
  var HOTSPOT_Y_RATIO = 0.15; // 纵向：前 15%
  var REQUIRED_TAPS = 5;
  var TAP_WINDOW_MS = 3000;
  var tapTimestamps = [];
  var menuOpen = false;
  iwx.onTouchStart(function (e) {
    var _a;
    if (menuOpen) return;
    var touch = (_a = e === null || e === void 0 ? void 0 : e.touches) === null || _a === void 0 ? void 0 : _a[0];
    if (!touch) return;
    var inHotspot = touch.clientX >= sysInfo.windowWidth * HOTSPOT_X_RATIO && touch.clientY <= sysInfo.windowHeight * HOTSPOT_Y_RATIO;
    if (!inHotspot) return;
    var now = Date.now();
    tapTimestamps.push(now);
    // 只保留窗口期内的点击
    tapTimestamps = tapTimestamps.filter(function (t) {
      return now - t <= TAP_WINDOW_MS;
    });
    console.log("[initApi] \u9690\u85CF\u70ED\u533A\u70B9\u51FB ".concat(tapTimestamps.length, "/").concat(REQUIRED_TAPS));
    if (tapTimestamps.length >= REQUIRED_TAPS) {
      tapTimestamps = [];
      menuOpen = true;
      var current = envSwitcher.getEnv();
      iwx.showActionSheet({
        alertText: "\u5F53\u524D\u73AF\u5883\uFF1A".concat(current),
        itemList: ['切到 test 环境', '切到 prod 环境'],
        success: function success(res) {
          var target = res.tapIndex === 0 ? 'test' : 'prod';
          console.log("[initApi] \u7528\u6237\u9009\u62E9\u5207\u5230 ".concat(target));
          envSwitcher.setEnv(target); // 内部会自动 restartMiniProgram
        },
        fail: function fail(err) {
          console.log('[initApi] 用户取消或弹窗失败', err);
        },
        complete: function complete() {
          menuOpen = false;
        }
      });
    }
  });
  console.log("[initApi] \u5DF2\u6CE8\u518C\u9690\u85CF\u73AF\u5883\u5207\u6362\u70ED\u533A\uFF1A\u5C4F\u5E55\u53F3\u4E0A\u89D2 3 \u79D2\u5185\u8FDE\u70B9 ".concat(REQUIRED_TAPS, " \u6B21\u5524\u8D77\u83DC\u5355"));
}

exports.setupEnvSwitchHotspot = setupEnvSwitchHotspot;
