/**
 * Runtime store — Zustand vanilla store for the GoodVibes platform runtime.
 *
 * Uses `createStore` from `zustand/vanilla` (NOT the React `create` hook)
 * because this runtime uses Zustand directly with no React renderer.
 */
import { createStore } from 'zustand/vanilla';
import { createInitialRuntimeState } from './state.js';
import { patchControlPlaneDomain, syncSessionStatePatch, transitionAgentDomainRecord, transitionTaskDomainRecord, updateAgentState, updateAutomationDomainFromJob, updateAutomationDomainFromRun, updateAutomationDomainFromSource, updateCommunicationState, updateConversationState, updateControlPlaneDomainFromClient, updateDeliveryDomainFromAttempt, updateIntegrationDomainFromRecord, updateMcpState, updateOrchestrationState, updatePermissionState, updatePluginState, updateRouteFailureState, updateRoutesDomainFromBinding, updateSessionState, updateSurfaceDomainFromRecord, updateTaskDomainFromRecord, updateTaskState, updateTransportState, updateWatcherDomainFromRecord, } from './helpers/reducers.js';
export function createRuntimeStore() {
    return createStore(() => createInitialRuntimeState());
}
function mutateRuntimeStore(store, updater) {
    store.setState(updater);
}
export function createDomainDispatch(store) {
    return {
        dispatchTurnEvent(event) {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                conversation: updateConversationState(state.conversation, event),
            }));
        },
        dispatchToolEvent(event) {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                conversation: updateConversationState(state.conversation, event),
            }));
        },
        dispatchPermissionEvent(event) {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                permissions: updatePermissionState(state.permissions, event),
            }));
        },
        dispatchTaskEvent(event) {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                tasks: updateTaskState(state.tasks, event),
            }));
        },
        dispatchAgentEvent(event) {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                agents: updateAgentState(state.agents, event),
            }));
        },
        dispatchOrchestrationEvent(event) {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                orchestration: updateOrchestrationState(state.orchestration, event),
            }));
        },
        dispatchCommunicationEvent(event) {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                communication: updateCommunicationState(state.communication, event),
            }));
        },
        dispatchPluginEvent(event) {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                plugins: updatePluginState(state.plugins, event),
            }));
        },
        dispatchMcpEvent(event) {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                mcp: updateMcpState(state.mcp, event),
            }));
        },
        dispatchTransportEvent(event) {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                ...updateTransportState(state.acp, state.daemon, event),
            }));
        },
        dispatchCompactionEvent(event) {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                session: updateSessionState(state.session, event),
            }));
        },
        syncSessionState(patch, source = 'domain-dispatch') {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                session: syncSessionStatePatch(state.session, patch, source),
            }));
        },
        syncRuntimeTask(task, source = 'domain-dispatch') {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                tasks: updateTaskDomainFromRecord(state.tasks, task, source),
            }));
        },
        transitionRuntimeTask(taskId, status, patch, source = 'domain-dispatch') {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                tasks: transitionTaskDomainRecord(state.tasks, taskId, status, patch, source),
            }));
        },
        transitionRuntimeAgent(agentId, status, patch, source = 'domain-dispatch') {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                agents: transitionAgentDomainRecord(state.agents, agentId, status, patch, source),
            }));
        },
        syncIntegration(record, source = 'domain-dispatch') {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                integrations: updateIntegrationDomainFromRecord(state.integrations, record, source),
            }));
        },
        syncAutomationSource(record, source = 'domain-dispatch') {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                automation: updateAutomationDomainFromSource(state.automation, record, source),
            }));
        },
        syncAutomationJob(record, source = 'domain-dispatch') {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                automation: updateAutomationDomainFromJob(state.automation, record, source),
            }));
        },
        syncAutomationRun(record, source = 'domain-dispatch') {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                automation: updateAutomationDomainFromRun(state.automation, record, source),
            }));
        },
        syncRouteBinding(record, source = 'domain-dispatch') {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                routes: updateRoutesDomainFromBinding(state.routes, record, source),
            }));
        },
        recordRouteBindingFailure(surfaceKind, externalId, source = 'domain-dispatch') {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                routes: updateRouteFailureState(state.routes, surfaceKind, externalId, source),
            }));
        },
        syncControlPlaneClient(record, source = 'domain-dispatch') {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                controlPlane: updateControlPlaneDomainFromClient(state.controlPlane, record, source),
            }));
        },
        syncControlPlaneState(patch, source = 'domain-dispatch') {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                controlPlane: patchControlPlaneDomain(state.controlPlane, patch, source),
            }));
        },
        syncDeliveryAttempt(record, source = 'domain-dispatch') {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                deliveries: updateDeliveryDomainFromAttempt(state.deliveries, record, source),
            }));
        },
        syncSurface(record, source = 'domain-dispatch') {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                surfaces: updateSurfaceDomainFromRecord(state.surfaces, record, source),
            }));
        },
        syncWatcher(record, source = 'domain-dispatch') {
            mutateRuntimeStore(store, (state) => ({
                ...state,
                watchers: updateWatcherDomainFromRecord(state.watchers, record, source),
            }));
        },
    };
}
export { createInitialRuntimeState } from './state.js';
export * from './selectors/index.js';
export * from './domains/index.js';
