"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
var _stringify = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/json/stringify"));
const fetch = require('node-fetch');
let idePort = 49160;
const getBaseUrl = () => `http://127.0.0.1:${idePort}`;

/**
 * 设置IDE端口号
 * @param {number} port - IDE服务端口号
 */
function setIdePort(port) {
  if (typeof port === 'number' && port > 0) {
    idePort = port;
  } else {
    console.warn('设置IDE端口失败');
  }
}

/**
 * 发送请求到IDE
 * @param {Object} obj -  请求数据
 * @param {string} type - 请求类型 build / release / debug / trans
 * @returns {Promise<Object>} 响应数据
 */
async function fetchInfo(obj, type) {
  const url = `${getBaseUrl()}/info`;
  const data = {
    obj,
    type
  };
  try {
    const response = await fetch(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: (0, _stringify.default)(data)
    });
    return await response.json();
  } catch (error) {
    console.error('Error sending request to IDE:', error);
    throw error;
  }
}
module.exports = {
  fetchInfo,
  setIdePort
};