import { inspect } from 'node:util';

// src/platform/core/string-helpers.ts
function stringifyUnknown(value) {
  if (value === void 0 || value === null) {
    return "";
  }
  if (typeof value === "string") {
    return value;
  }
  if (Buffer.isBuffer(value)) {
    return value.toString("utf8");
  }
  if (value instanceof Error) {
    return value.stack || value.message;
  }
  return inspect(value, { depth: 6, breakLength: 120 });
}
function getErrorSummary(error) {
  if (error instanceof Error) {
    return `${error.name}: ${error.message}`;
  }
  return stringifyUnknown(error) || "Unknown error";
}

// src/features/mcp/domain/server-record.ts
var SUPPORTED_HTTP_PROTOCOLS = /* @__PURE__ */ new Set(["http:", "https:"]);
function normalizeMcpServerName(value) {
  const normalized = value.trim();
  if (!normalized) {
    throw new Error("MCP \uC11C\uBC84 \uC774\uB984\uC744 \uC785\uB825\uD574 \uC8FC\uC138\uC694.");
  }
  if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(normalized)) {
    throw new Error("MCP \uC11C\uBC84 \uC774\uB984\uC740 \uC601\uBB38, \uC22B\uC790, \uC810(.), \uBC11\uC904(_), \uD558\uC774\uD508(-)\uB9CC \uC0AC\uC6A9\uD560 \uC218 \uC788\uC2B5\uB2C8\uB2E4.");
  }
  return normalized;
}
function normalizeMcpUrl(value) {
  const normalized = value.trim();
  if (!normalized) {
    throw new Error("MCP \uC11C\uBC84 \uC8FC\uC18C\uB97C \uC785\uB825\uD574 \uC8FC\uC138\uC694.");
  }
  let parsed;
  try {
    parsed = new URL(normalized);
  } catch (error) {
    throw new Error(`\uC720\uD6A8\uD55C MCP \uC11C\uBC84 URL\uC774 \uC544\uB2D9\uB2C8\uB2E4. ${getErrorSummary(error)}`);
  }
  if (!SUPPORTED_HTTP_PROTOCOLS.has(parsed.protocol)) {
    throw new Error(`\uD604\uC7AC\uB294 HTTP/HTTPS MCP \uC11C\uBC84\uB9CC \uC9C0\uC6D0\uD569\uB2C8\uB2E4. protocol=${parsed.protocol}`);
  }
  return parsed.toString();
}
function normalizeMcpCommand(value) {
  const normalized = value.trim();
  if (!normalized) {
    throw new Error("stdio MCP \uC11C\uBC84 command\uB97C \uC785\uB825\uD574 \uC8FC\uC138\uC694.");
  }
  return normalized;
}
function normalizeMcpArgs(values = []) {
  return values.map((value) => value.trim()).filter(Boolean);
}
function createMcpServerRecord(input, timestamps) {
  const name = normalizeMcpServerName(input.name);
  if (input.transport === "http") {
    return {
      createdAt: timestamps.createdAt,
      name,
      transport: "http",
      updatedAt: timestamps.updatedAt,
      url: normalizeMcpUrl(input.url)
    };
  }
  return {
    args: normalizeMcpArgs(input.args),
    command: normalizeMcpCommand(input.command),
    createdAt: timestamps.createdAt,
    name,
    transport: "stdio",
    updatedAt: timestamps.updatedAt
  };
}
function parsePersistedMcpServerRecord(value) {
  if (!value || typeof value !== "object") {
    return void 0;
  }
  const raw = value;
  if (typeof raw.name !== "string" || typeof raw.createdAt !== "string" || typeof raw.updatedAt !== "string") {
    return void 0;
  }
  if ((raw.transport === "http" || typeof raw.url === "string") && typeof raw.url === "string") {
    return createMcpServerRecord(
      {
        name: raw.name,
        transport: "http",
        url: raw.url
      },
      {
        createdAt: raw.createdAt,
        updatedAt: raw.updatedAt
      }
    );
  }
  if (raw.transport === "stdio" && typeof raw.command === "string") {
    return createMcpServerRecord(
      {
        args: Array.isArray(raw.args) ? raw.args.filter((entry) => typeof entry === "string") : [],
        command: raw.command,
        name: raw.name,
        transport: "stdio"
      },
      {
        createdAt: raw.createdAt,
        updatedAt: raw.updatedAt
      }
    );
  }
  return void 0;
}
function isStdioMcpServer(server) {
  return server.transport === "stdio";
}
function isHttpMcpServer(server) {
  return server.transport === "http";
}
function describeMcpServerRecord(server) {
  return isHttpMcpServer(server) ? server.url : [server.command, ...server.args].join(" ");
}
function getMcpServerTransportLabel(server) {
  return server.transport;
}
function getMcpServerTargetLabel(server) {
  return isHttpMcpServer(server) ? "url" : "command";
}
function getMcpServerHealthLabel(server) {
  return isHttpMcpServer(server) ? "Endpoint" : "Launcher";
}
function isSameMcpServerConfig(left, right) {
  if (left.name !== right.name || left.transport !== right.transport) {
    return false;
  }
  if (isHttpMcpServer(left) && isHttpMcpServer(right)) {
    return left.url === right.url;
  }
  if (isStdioMcpServer(left) && isStdioMcpServer(right)) {
    return left.command === right.command && left.args.join("\0") === right.args.join("\0");
  }
  return false;
}

export { createMcpServerRecord, describeMcpServerRecord, getMcpServerHealthLabel, getMcpServerTargetLabel, getMcpServerTransportLabel, isHttpMcpServer, isSameMcpServerConfig, isStdioMcpServer, normalizeMcpServerName, normalizeMcpUrl, parsePersistedMcpServerRecord };
