import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import { V_CONSOLE_DOM, EMPTY_LINE, BUILD_NAME_MAP, BUILD_LIST, COMMIT_LIST, AEGIS_PERFORMANCE_LIST, AEGIS_PERFORMANCE_KEY } from '../config.mjs';

var inputValue;
function updateInputValue(event) {
  var _a;
  inputValue = ((_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.value) || '';
}
function jumpToUrl() {
  if (!inputValue) return;
  location.href = inputValue;
}
function goBack() {
  var _a, _b;
  history.go(-1);
  (_b = (_a = window.vConsole) === null || _a === void 0 ? void 0 : _a.hide) === null || _b === void 0 ? void 0 : _b.call(_a);
}
function renderPlugin(callback) {
  var parseNumber = function parseNumber(num) {
    return +(+num).toFixed(2);
  };
  var html = "<div class=\"".concat(V_CONSOLE_DOM.WRAP, "\">");
  html += getPerformanceInfo().map(function (item) {
    var label = item.label,
      value = item.value;
    return "<div class=\"".concat(V_CONSOLE_DOM.LINE, "\">").concat(label, "\uFF1A").concat(parseNumber(value), "ms </div>");
  }).concat(EMPTY_LINE).concat(getVersionHtml()).concat(EMPTY_LINE).concat(getAegisPerformanceInfo()).join('\n');
  html += "\n<textarea id=\"".concat(V_CONSOLE_DOM.URL_INPUT_ID, "\" type=\"text\" placeholder=\"\u8BF7\u8F93\u5165\u8DF3\u8F6C\u8DEF\u5F84\"></textarea>\n<button id=\"").concat(V_CONSOLE_DOM.URL_JUMP_BUTTON, "\">\u8DF3\u8F6C</button>\n<button id=\"").concat(V_CONSOLE_DOM.GO_BACK_BUTTON, "\">\u8FD4\u56DE\u4E0A\u4E00\u9875</button>\n  ");
  html += '</div>';
  callback(html);
}
function initVersionPlugin() {
  var plugin = new VConsole.VConsolePlugin(V_CONSOLE_DOM.PLUGIN_VERSION_NAME, '其他信息');
  var callback = function callback(html) {
    var dom = document.getElementById("".concat(V_CONSOLE_DOM.PLUGIN_NAME_PREFIX).concat(V_CONSOLE_DOM.PLUGIN_VERSION_NAME));
    if (dom) {
      dom.innerHTML = html;
    }
  };
  plugin.on('renderTab', function (callback) {
    renderPlugin(callback);
  });
  plugin.on('showConsole', function () {
    renderPlugin(callback);
  });
  plugin.on('show', function () {
    renderPlugin(callback);
  });
  var btnList = [];
  btnList.push({
    name: '隐藏vConsole',
    global: false,
    onClick: function onClick() {
      var _a;
      (_a = window.vConsole) === null || _a === void 0 ? void 0 : _a.destroy();
    }
  });
  plugin.on('addTool', function (callback) {
    callback(btnList);
  });
  plugin.on('show', function () {
    var _a, _b, _c;
    (_a = document.getElementById(V_CONSOLE_DOM.URL_INPUT_ID)) === null || _a === void 0 ? void 0 : _a.addEventListener('input', updateInputValue);
    (_b = document.getElementById(V_CONSOLE_DOM.URL_JUMP_BUTTON)) === null || _b === void 0 ? void 0 : _b.addEventListener('click', jumpToUrl);
    (_c = document.getElementById(V_CONSOLE_DOM.GO_BACK_BUTTON)) === null || _c === void 0 ? void 0 : _c.addEventListener('click', goBack);
  });
  plugin.on('hide', function () {
    var _a, _b, _c;
    (_a = document.getElementById(V_CONSOLE_DOM.URL_INPUT_ID)) === null || _a === void 0 ? void 0 : _a.removeEventListener('input', updateInputValue);
    (_b = document.getElementById(V_CONSOLE_DOM.URL_JUMP_BUTTON)) === null || _b === void 0 ? void 0 : _b.removeEventListener('click', jumpToUrl);
    (_c = document.getElementById(V_CONSOLE_DOM.GO_BACK_BUTTON)) === null || _c === void 0 ? void 0 : _c.addEventListener('click', goBack);
  });
  return plugin;
}
function getPerformanceInfo() {
  var _a;
  var timing = performance.timing || {};
  var timeOrigin = ((_a = performance === null || performance === void 0 ? void 0 : performance.wx) === null || _a === void 0 ? void 0 : _a.timeOrigin) || timing.fetchStart;
  var dnsTime = timing.domainLookupEnd - timing.domainLookupStart;
  var tcpTime = timing.connectEnd - timing.connectStart;
  var backendTime = timing.responseStart - timing.requestStart;
  var respTime = timing.responseEnd - timing.responseStart;
  var domTime = timing.domContentLoadedEventStart - timing.responseEnd;
  var firstRenderTime = timing.domLoading - timeOrigin;
  var wholePageTime = timing.loadEventEnd - timeOrigin;
  var parseDomTime = timing.domComplete - timing.domInteractive;
  var list = [{
    value: dnsTime,
    label: 'DNS连接耗时'
  }, {
    value: tcpTime,
    label: 'TCP连接耗时'
  }, {
    value: backendTime,
    label: '后端响应时间'
  }, {
    value: respTime,
    label: 'HTML页面下载时间'
  }, {
    value: domTime,
    label: 'DOM时间'
  }, {
    value: parseDomTime,
    label: '解析DOM树耗时'
  }, {
    value: firstRenderTime,
    label: '首次加载耗时'
  }, {
    value: wholePageTime,
    label: '整页耗时'
  }];
  return list;
}
function getVersionHtml() {
  var buildInfo = window[BUILD_NAME_MAP.build] || {};
  var commitInfo = window[BUILD_NAME_MAP.commit] || {};
  var buildHtmlList = BUILD_LIST.map(function (item) {
    var key = item.key,
      label = item.label;
    return "<div class=\"".concat(V_CONSOLE_DOM.LINE, "\">").concat(label, ": ").concat(buildInfo[key] || '', "</div>");
  });
  var commitHtmlList = COMMIT_LIST.map(function (item) {
    var key = item.key,
      label = item.label;
    return "<div class=\"".concat(V_CONSOLE_DOM.LINE, "\">").concat(label, ": ").concat(commitInfo[key] || '', "</div>");
  });
  return [].concat(_toConsumableArray(buildHtmlList), [EMPTY_LINE], _toConsumableArray(commitHtmlList), [EMPTY_LINE, EMPTY_LINE]).join('\n');
}
function getAegisPerformanceInfo() {
  var perfInfo = window[AEGIS_PERFORMANCE_KEY] || {};
  var perfHtmlList = AEGIS_PERFORMANCE_LIST.map(function (item) {
    var _a;
    var key = item.key,
      label = item.label;
    return "<div class=\"".concat(V_CONSOLE_DOM.LINE, "\">").concat(label, ": ").concat((_a = perfInfo[key]) !== null && _a !== void 0 ? _a : '', "</div>");
  });
  return [EMPTY_LINE].concat(_toConsumableArray(perfHtmlList), [EMPTY_LINE]).join('\n');
}

export { initVersionPlugin };
