'use strict';

var net = require('net');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var net__default = /*#__PURE__*/_interopDefault(net);

// src/features/talk-to-figma/infrastructure/port-health.ts
var TALK_TO_FIGMA_PORT_TIMEOUT_MS = 1500;
var LOCALHOST_CANDIDATES = ["127.0.0.1", "::1"];
function probeSingleHost(host, port) {
  return new Promise((resolve) => {
    const socket = net__default.default.createConnection({
      host,
      port
    });
    const finish = (result) => {
      socket.removeAllListeners();
      socket.destroy();
      resolve(result);
    };
    socket.setTimeout(TALK_TO_FIGMA_PORT_TIMEOUT_MS);
    socket.once("connect", () => {
      finish({
        detail: `${host}:${port} \uC5F0\uACB0 \uC131\uACF5`,
        host,
        ok: true,
        port
      });
    });
    socket.once("timeout", () => {
      finish({
        detail: `${host}:${port} \uC5F0\uACB0 \uD0C0\uC784\uC544\uC6C3`,
        host,
        ok: false,
        port
      });
    });
    socket.once("error", (error) => {
      finish({
        detail: `${host}:${port} \uC5F0\uACB0 \uC2E4\uD328 - ${error.message}`,
        host,
        ok: false,
        port
      });
    });
  });
}
async function probeTalkToFigmaPort(port) {
  const failures = [];
  for (const host of LOCALHOST_CANDIDATES) {
    const result = await probeSingleHost(host, port);
    if (result.ok) {
      return result;
    }
    failures.push(result.detail);
  }
  return {
    detail: failures.join(" | "),
    ok: false,
    port
  };
}

exports.probeTalkToFigmaPort = probeTalkToFigmaPort;
