// src/server-connection.ts
import { Option, Schema } from "effect";
var DEFAULT_EXECUTOR_SERVER_ORIGIN = "http://127.0.0.1:4000";
var DEFAULT_EXECUTOR_SERVER_USERNAME = "executor";
var stripTrailingSlash = (value) => value.replace(/\/+$/, "");
var displayNameFromOrigin = (origin) => origin.replace(/^https?:\/\//, "").replace(/\/+$/, "");
var normalizeExecutorServerOrigin = (raw) => {
  const trimmed = stripTrailingSlash(raw.trim());
  if (!trimmed) return DEFAULT_EXECUTOR_SERVER_ORIGIN;
  const parsed = new URL(/^https?:\/\//.test(trimmed) ? trimmed : `http://${trimmed}`);
  if (parsed.pathname === "/api") {
    parsed.pathname = "/";
  }
  parsed.search = "";
  parsed.hash = "";
  return stripTrailingSlash(parsed.toString());
};
var apiBaseUrlForServerOrigin = (origin) => `${origin}/api`;
var originFromApiBaseUrl = (raw) => {
  const parsed = new URL(raw);
  if (parsed.pathname.endsWith("/api")) {
    parsed.pathname = parsed.pathname.slice(0, -"/api".length) || "/";
  }
  parsed.search = "";
  parsed.hash = "";
  return normalizeExecutorServerOrigin(parsed.toString());
};
var normalizeExecutorServerConnection = (input = {}) => {
  const origin = normalizeExecutorServerOrigin(
    input.origin ?? (input.apiBaseUrl ? originFromApiBaseUrl(input.apiBaseUrl) : DEFAULT_EXECUTOR_SERVER_ORIGIN)
  );
  const apiBaseUrl = stripTrailingSlash(input.apiBaseUrl ?? apiBaseUrlForServerOrigin(origin));
  const kind = input.kind ?? "http";
  return {
    kind,
    key: input.key ?? `${kind}:${origin}`,
    origin,
    apiBaseUrl,
    displayName: input.displayName ?? displayNameFromOrigin(origin),
    ...input.auth ? { auth: input.auth } : {}
  };
};
var encodeBasicCredentials = (credentials) => {
  if (typeof globalThis.btoa === "function") {
    return globalThis.btoa(credentials);
  }
  const buffer = globalThis.Buffer;
  if (buffer) {
    return buffer.from(credentials).toString("base64");
  }
  return null;
};
var getExecutorServerAuthorizationHeader = (connection) => {
  const auth = connection.auth;
  if (!auth) return null;
  if (auth.kind === "bearer") return `Bearer ${auth.token}`;
  const encoded = encodeBasicCredentials(
    `${auth.username ?? DEFAULT_EXECUTOR_SERVER_USERNAME}:${auth.password}`
  );
  return encoded ? `Basic ${encoded}` : null;
};
var ExecutorServerAuthJson = Schema.Union([
  Schema.Struct({
    kind: Schema.Literal("basic"),
    username: Schema.optional(Schema.String),
    password: Schema.String
  }),
  Schema.Struct({
    kind: Schema.Literal("bearer"),
    token: Schema.String
  })
]);
var ExecutorServerConnectionJson = Schema.Struct({
  kind: Schema.optional(Schema.Literals(["http", "desktop-sidecar"])),
  key: Schema.optional(Schema.String),
  origin: Schema.String,
  apiBaseUrl: Schema.optional(Schema.String),
  displayName: Schema.optional(Schema.String),
  auth: Schema.optional(ExecutorServerAuthJson)
});
var ExecutorLocalServerManifestJson = Schema.Struct({
  version: Schema.Literal(1),
  kind: Schema.Literals(["cli-daemon", "desktop-sidecar", "foreground"]),
  pid: Schema.Number,
  startedAt: Schema.String,
  dataDir: Schema.String,
  scopeDir: Schema.NullOr(Schema.String),
  connection: ExecutorServerConnectionJson,
  owner: Schema.Struct({
    client: Schema.Literals(["cli", "desktop"]),
    version: Schema.NullOr(Schema.String),
    executablePath: Schema.NullOr(Schema.String)
  })
});
var decodeUnknownJsonOption = Schema.decodeUnknownOption(Schema.UnknownFromJsonString);
var decodeExecutorLocalServerManifestJson = Schema.decodeUnknownOption(
  ExecutorLocalServerManifestJson
);
var canNormalizeServerOrigin = (origin) => {
  const trimmed = stripTrailingSlash(origin.trim());
  if (!trimmed) return true;
  return URL.canParse(/^https?:\/\//.test(trimmed) ? trimmed : `http://${trimmed}`);
};
var parseExecutorLocalServerManifest = (raw) => {
  const json = decodeUnknownJsonOption(raw);
  if (Option.isNone(json)) return null;
  const decoded = decodeExecutorLocalServerManifestJson(json.value);
  if (Option.isNone(decoded)) return null;
  const parsed = decoded.value;
  if (!Number.isInteger(parsed.pid) || parsed.pid <= 0 || !canNormalizeServerOrigin(parsed.connection.origin)) {
    return null;
  }
  const connection = normalizeExecutorServerConnection(parsed.connection);
  return {
    version: 1,
    kind: parsed.kind,
    pid: parsed.pid,
    startedAt: parsed.startedAt,
    dataDir: parsed.dataDir,
    scopeDir: parsed.scopeDir,
    connection,
    owner: {
      client: parsed.owner.client,
      version: parsed.owner.version,
      executablePath: parsed.owner.executablePath
    }
  };
};
var serializeExecutorLocalServerManifest = (manifest) => `${JSON.stringify(manifest, null, 2)}
`;

// src/oauth-popup-types.ts
var OAUTH_POPUP_MESSAGE_TYPE = "executor:oauth-result";
var isOAuthPopupResult = (value) => typeof value === "object" && value !== null && value.type === OAUTH_POPUP_MESSAGE_TYPE;

// src/api-errors.ts
import { Schema as Schema2 } from "effect";
var InternalError = class extends Schema2.TaggedErrorClass()(
  "InternalError",
  {
    /** Opaque correlation id for backend lookup (Sentry event id, log line, etc.). */
    traceId: Schema2.String
  },
  { httpApiStatus: 500 }
) {
};

export {
  DEFAULT_EXECUTOR_SERVER_ORIGIN,
  DEFAULT_EXECUTOR_SERVER_USERNAME,
  normalizeExecutorServerOrigin,
  apiBaseUrlForServerOrigin,
  originFromApiBaseUrl,
  normalizeExecutorServerConnection,
  getExecutorServerAuthorizationHeader,
  parseExecutorLocalServerManifest,
  serializeExecutorLocalServerManifest,
  OAUTH_POPUP_MESSAGE_TYPE,
  isOAuthPopupResult,
  InternalError
};
//# sourceMappingURL=chunk-SK3PCJHJ.js.map