// Force strict mode and setup for ESM
"use strict";
import {
  SessionNotFoundError
} from "./chunk-GGQQA4JW.js";
import {
  init_esbuild_shims
} from "./chunk-A4BMJM77.js";
import {
  __name
} from "./chunk-J2S4EL5Y.js";

// packages/cli/src/serve/workspace-registry.ts
init_esbuild_shims();
function createWorkspaceRegistry(inputRuntimes) {
  if (inputRuntimes.length === 0) {
    throw new Error(
      "WorkspaceRegistry requires at least one workspace runtime."
    );
  }
  const primaryRuntimes = inputRuntimes.filter((runtime) => runtime.primary);
  if (primaryRuntimes.length !== 1) {
    throw new Error(
      "WorkspaceRegistry requires exactly one primary workspace runtime."
    );
  }
  const byCwd = /* @__PURE__ */ new Map();
  const byId = /* @__PURE__ */ new Map();
  for (const runtime of inputRuntimes) {
    if (byCwd.has(runtime.workspaceCwd)) {
      throw new Error(
        `Duplicate workspace runtime cwd ${JSON.stringify(
          runtime.workspaceCwd
        )}.`
      );
    }
    byCwd.set(runtime.workspaceCwd, runtime);
    if (byId.has(runtime.workspaceId)) {
      throw new Error(
        `Duplicate workspace runtime id ${JSON.stringify(
          runtime.workspaceId
        )}.`
      );
    }
    byId.set(runtime.workspaceId, runtime);
  }
  const runtimes = Object.freeze([...inputRuntimes]);
  const primary = primaryRuntimes[0];
  return {
    primary,
    list: /* @__PURE__ */ __name(() => runtimes, "list"),
    getByWorkspaceCwd: /* @__PURE__ */ __name((workspaceCwd) => byCwd.get(workspaceCwd), "getByWorkspaceCwd"),
    getByWorkspaceId: /* @__PURE__ */ __name((workspaceId) => byId.get(workspaceId), "getByWorkspaceId"),
    resolveWorkspaceCwd: /* @__PURE__ */ __name((workspaceCwd) => workspaceCwd === void 0 ? primary : byCwd.get(workspaceCwd), "resolveWorkspaceCwd"),
    resolveLiveSessionOwner: /* @__PURE__ */ __name((sessionId) => {
      const matches = [];
      for (const runtime of runtimes) {
        try {
          runtime.bridge.getSessionSummary(sessionId);
          matches.push(runtime);
        } catch (err) {
          if (err instanceof SessionNotFoundError) continue;
          throw err;
        }
      }
      if (matches.length === 0) return { kind: "not_found" };
      if (matches.length === 1) {
        return { kind: "found", runtime: matches[0] };
      }
      return { kind: "ambiguous", runtimes: matches };
    }, "resolveLiveSessionOwner")
  };
}
__name(createWorkspaceRegistry, "createWorkspaceRegistry");
function createSingleWorkspaceRegistry(runtime) {
  return createWorkspaceRegistry([runtime]);
}
__name(createSingleWorkspaceRegistry, "createSingleWorkspaceRegistry");

export {
  createWorkspaceRegistry,
  createSingleWorkspaceRegistry
};
/**
 * @license
 * Copyright 2026 Qwen Team
 * SPDX-License-Identifier: Apache-2.0
 */
