import {
  agentGuiWorkbenchProviders,
  normalizeAgentGuiWorkbenchProvider
} from "./chunk-22L4VWUR.js";

// workbench/state.ts
function createDefaultAgentGuiWorkbenchNodeState(provider = "codex") {
  return {
    composerOverrides: null,
    composerOverridesByProvider: null,
    conversationCount: null,
    conversationRailCollapsed: false,
    conversationRailWidthPx: null,
    lastActiveAgentSessionId: null,
    lastActiveConversationTitle: null,
    provider
  };
}
function normalizeAgentGuiWorkbenchState(state) {
  if (!isRecord(state)) {
    return createDefaultAgentGuiWorkbenchState();
  }
  return {
    composerOverrides: normalizeAgentGuiWorkbenchComposerOverrides(
      state.composerOverrides
    ),
    composerOverridesByProvider: normalizeAgentGuiWorkbenchComposerOverridesByProvider(
      state.composerOverridesByProvider
    ),
    conversationRailCollapsed: state.conversationRailCollapsed === true,
    conversationRailWidthPx: normalizeOptionalPositiveNumber(
      state.conversationRailWidthPx
    ),
    lastActiveAgentSessionId: typeof state.lastActiveAgentSessionId === "string" ? state.lastActiveAgentSessionId : null,
    lastActiveConversationTitle: typeof state.lastActiveConversationTitle === "string" ? state.lastActiveConversationTitle : null
  };
}
function projectAgentGuiWorkbenchState(state) {
  return {
    composerOverrides: normalizeAgentGuiWorkbenchComposerOverrides(
      state.composerOverrides
    ),
    composerOverridesByProvider: normalizeAgentGuiWorkbenchComposerOverridesByProvider(
      state.composerOverridesByProvider
    ),
    conversationRailCollapsed: state.conversationRailCollapsed === true,
    conversationRailWidthPx: normalizeOptionalPositiveNumber(
      state.conversationRailWidthPx
    ),
    lastActiveAgentSessionId: state.lastActiveAgentSessionId ?? null,
    lastActiveConversationTitle: state.lastActiveConversationTitle ?? null
  };
}
function areAgentGuiWorkbenchStatesEqual(left, right) {
  return composerOverridesEqual(left.composerOverrides, right.composerOverrides) && composerOverridesByProviderEqual(
    left.composerOverridesByProvider,
    right.composerOverridesByProvider
  ) && left.conversationRailCollapsed === right.conversationRailCollapsed && left.conversationRailWidthPx === right.conversationRailWidthPx && left.lastActiveAgentSessionId === right.lastActiveAgentSessionId && left.lastActiveConversationTitle === right.lastActiveConversationTitle;
}
function normalizeAgentGuiWorkbenchNodeState(state, fallbackProvider = "codex") {
  const provider = normalizeAgentGuiWorkbenchProvider(
    state?.provider,
    fallbackProvider
  );
  return {
    ...createDefaultAgentGuiWorkbenchNodeState(provider),
    composerOverrides: normalizeAgentGuiWorkbenchComposerOverrides(
      state?.composerOverrides
    ),
    composerOverridesByProvider: normalizeAgentGuiWorkbenchComposerOverridesByProvider(
      state?.composerOverridesByProvider
    ),
    conversationCount: normalizeOptionalNonNegativeNumber(
      state?.conversationCount
    ),
    conversationRailCollapsed: state?.conversationRailCollapsed === true,
    conversationRailWidthPx: normalizeOptionalPositiveNumber(
      state?.conversationRailWidthPx
    ),
    lastActiveAgentSessionId: typeof state?.lastActiveAgentSessionId === "string" ? state.lastActiveAgentSessionId : null,
    lastActiveConversationTitle: typeof state?.lastActiveConversationTitle === "string" ? state.lastActiveConversationTitle : null,
    provider
  };
}
function areAgentGuiWorkbenchNodeStatesEqual(left, right) {
  return composerOverridesEqual(left.composerOverrides, right.composerOverrides) && composerOverridesByProviderEqual(
    left.composerOverridesByProvider,
    right.composerOverridesByProvider
  ) && left.conversationCount === right.conversationCount && left.conversationRailCollapsed === right.conversationRailCollapsed && left.conversationRailWidthPx === right.conversationRailWidthPx && left.lastActiveAgentSessionId === right.lastActiveAgentSessionId && left.lastActiveConversationTitle === right.lastActiveConversationTitle && left.provider === right.provider;
}
function agentGuiWorkbenchProviderFromInstanceId(instanceId) {
  const normalized = instanceId?.trim();
  if (!normalized || normalized === "agent-gui") {
    return "codex";
  }
  const [, provider] = normalized.split(":", 3);
  return normalizeAgentGuiWorkbenchProvider(provider);
}
function createAgentGuiWorkbenchNodeStateSource(input) {
  const typeId = input.typeId ?? "agent-gui";
  const nodeStateByInstanceId = /* @__PURE__ */ new Map();
  const listeners = /* @__PURE__ */ new Set();
  const notify = () => {
    for (const listener of listeners) {
      listener();
    }
  };
  return {
    externalStateSource: {
      getNodeState(request) {
        if (request.typeId !== typeId) {
          return null;
        }
        const state = nodeStateByInstanceId.get(request.instanceId);
        return state ? { ...state } : null;
      },
      getSnapshotNodeState(request) {
        if (request.typeId !== typeId) {
          return null;
        }
        const state = nodeStateByInstanceId.get(request.instanceId);
        return state ? { ...state } : null;
      },
      getWorkspaceState() {
        return {
          workspaceId: input.workspaceId
        };
      },
      subscribe(listener) {
        listeners.add(listener);
        return () => {
          listeners.delete(listener);
        };
      }
    },
    readNodeState(request) {
      if (request.typeId !== typeId) {
        return null;
      }
      const state = nodeStateByInstanceId.get(request.instanceId);
      return state ? { ...state } : null;
    },
    writeNodeState(request) {
      if (request.typeId !== typeId) {
        return;
      }
      nodeStateByInstanceId.set(request.instanceId, {
        ...normalizeAgentGuiWorkbenchState(request.state)
      });
      notify();
    }
  };
}
function createDefaultAgentGuiWorkbenchState() {
  return {
    composerOverrides: null,
    composerOverridesByProvider: null,
    conversationRailCollapsed: false,
    conversationRailWidthPx: null,
    lastActiveAgentSessionId: null,
    lastActiveConversationTitle: null
  };
}
function normalizeAgentGuiWorkbenchComposerOverrides(value) {
  if (!isRecord(value)) {
    return null;
  }
  const composerOverrides = {};
  if (typeof value.model === "string" && value.model.trim()) {
    composerOverrides.model = value.model.trim();
  }
  if (typeof value.reasoningEffort === "string" && value.reasoningEffort.trim()) {
    composerOverrides.reasoningEffort = value.reasoningEffort.trim();
  }
  if (typeof value.permissionModeId === "string" && value.permissionModeId.trim()) {
    composerOverrides.permissionModeId = value.permissionModeId.trim();
  }
  if (typeof value.planMode === "boolean") {
    composerOverrides.planMode = value.planMode;
  }
  return Object.keys(composerOverrides).length > 0 ? composerOverrides : null;
}
function normalizeAgentGuiWorkbenchComposerOverridesByProvider(value) {
  if (!isRecord(value)) {
    return null;
  }
  const result = {};
  for (const provider of agentGuiWorkbenchProviders) {
    const overrides = normalizeAgentGuiWorkbenchComposerOverrides(
      value[provider]
    );
    if (overrides) {
      result[provider] = overrides;
    }
  }
  return Object.keys(result).length > 0 ? result : null;
}
function normalizeOptionalPositiveNumber(value) {
  return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.round(value) : null;
}
function normalizeOptionalNonNegativeNumber(value) {
  return typeof value === "number" && Number.isFinite(value) && value >= 0 ? Math.round(value) : null;
}
function composerOverridesEqual(left, right) {
  return (left?.model ?? null) === (right?.model ?? null) && (left?.permissionModeId ?? null) === (right?.permissionModeId ?? null) && (left?.planMode ?? null) === (right?.planMode ?? null) && (left?.reasoningEffort ?? null) === (right?.reasoningEffort ?? null);
}
function composerOverridesByProviderEqual(left, right) {
  for (const provider of agentGuiWorkbenchProviders) {
    if (!composerOverridesEqual(left?.[provider], right?.[provider])) {
      return false;
    }
  }
  return true;
}
function isRecord(value) {
  return typeof value === "object" && value !== null;
}

export {
  createDefaultAgentGuiWorkbenchNodeState,
  normalizeAgentGuiWorkbenchState,
  projectAgentGuiWorkbenchState,
  areAgentGuiWorkbenchStatesEqual,
  normalizeAgentGuiWorkbenchNodeState,
  areAgentGuiWorkbenchNodeStatesEqual,
  agentGuiWorkbenchProviderFromInstanceId,
  createAgentGuiWorkbenchNodeStateSource
};
//# sourceMappingURL=chunk-GEXHKJK5.js.map