import {
  isAgentGuiWorkbenchProvider,
  normalizeAgentGuiWorkbenchProvider
} from "./chunk-22L4VWUR.js";
import {
  agentGuiWorkbenchOpenSessionActivationType
} from "./chunk-6AZ2KF6R.js";

// workbench/launch.ts
var agentGuiWorkbenchTypeId = "agent-gui";
var agentGuiWorkbenchDockEntryPrefix = "agent-gui:";
var agentGuiWorkbenchInstanceSequence = 0;
function agentGuiWorkbenchDockEntryId(provider) {
  return provider === "codex" ? agentGuiWorkbenchTypeId : `${agentGuiWorkbenchDockEntryPrefix}${provider}`;
}
function agentGuiWorkbenchInstanceId(provider) {
  return `${agentGuiWorkbenchDockEntryPrefix}${provider}`;
}
function createAgentGuiWorkbenchInstanceId(input) {
  const prefix = agentGuiWorkbenchInstanceId(input.provider);
  const agentSessionId = input.agentSessionId?.trim();
  if (agentSessionId) {
    return `${prefix}:session:${encodeAgentGuiWorkbenchInstanceSegment(
      agentSessionId
    )}`;
  }
  agentGuiWorkbenchInstanceSequence += 1;
  return [
    prefix,
    "panel",
    `${Date.now().toString(36)}-${agentGuiWorkbenchInstanceSequence.toString(36)}`
  ].join(":");
}
function agentGuiWorkbenchProviderFromIdentifier(value) {
  const normalized = value?.trim();
  if (!normalized || normalized === agentGuiWorkbenchTypeId) {
    return normalized === agentGuiWorkbenchTypeId ? "codex" : null;
  }
  if (!normalized.startsWith(agentGuiWorkbenchDockEntryPrefix)) {
    return null;
  }
  const provider = normalized.slice(agentGuiWorkbenchDockEntryPrefix.length).split(":", 1)[0];
  return isAgentGuiWorkbenchProvider(provider) ? provider : null;
}
function agentGuiWorkbenchProviderFromLaunchRequest(request) {
  const payloadProvider = request.payload && typeof request.payload === "object" && !Array.isArray(request.payload) ? request.payload.provider : null;
  if (isAgentGuiWorkbenchProvider(payloadProvider)) {
    return payloadProvider;
  }
  return agentGuiWorkbenchProviderFromIdentifier(request.dockEntryId) ?? agentGuiWorkbenchProviderFromIdentifier(request.typeId) ?? "codex";
}
function createAgentGuiWorkbenchSessionLaunchRequest(input) {
  const provider = normalizeAgentGuiWorkbenchProvider(input.provider);
  return {
    dockEntryId: agentGuiWorkbenchDockEntryId(provider),
    payload: {
      ...input.agentSessionId ? { agentSessionId: input.agentSessionId } : {},
      provider
    },
    reason: "host",
    typeId: agentGuiWorkbenchTypeId
  };
}
function createAgentGuiWorkbenchLaunchDescriptor(request) {
  const provider = agentGuiWorkbenchProviderFromLaunchRequest(request);
  const targetAgentSessionId = agentSessionIdFromLaunchPayload(request.payload);
  const instanceId = createAgentGuiWorkbenchInstanceId({
    agentSessionId: targetAgentSessionId,
    provider
  });
  return {
    activation: targetAgentSessionId ? {
      payload: {
        agentSessionId: targetAgentSessionId
      },
      type: agentGuiWorkbenchOpenSessionActivationType
    } : null,
    dockEntryId: request.dockEntryId ?? agentGuiWorkbenchDockEntryId(provider),
    instanceId,
    provider,
    reuseDockEntryNode: Boolean(targetAgentSessionId),
    targetAgentSessionId
  };
}
function encodeAgentGuiWorkbenchInstanceSegment(value) {
  return encodeURIComponent(value.trim());
}
function agentSessionIdFromLaunchPayload(payload) {
  if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
    return null;
  }
  const agentSessionId = payload.agentSessionId;
  return typeof agentSessionId === "string" && agentSessionId.trim() ? agentSessionId.trim() : null;
}

export {
  agentGuiWorkbenchTypeId,
  agentGuiWorkbenchDockEntryId,
  agentGuiWorkbenchInstanceId,
  createAgentGuiWorkbenchInstanceId,
  agentGuiWorkbenchProviderFromIdentifier,
  agentGuiWorkbenchProviderFromLaunchRequest,
  createAgentGuiWorkbenchSessionLaunchRequest,
  createAgentGuiWorkbenchLaunchDescriptor
};
//# sourceMappingURL=chunk-3735XYFZ.js.map