"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.baseModel = exports.summarizerOperationGenerator = void 0;
const stochastic_test_utils_1 = require("@fluid-private/stochastic-test-utils");
const internal_1 = require("@fluidframework/test-runtime-utils/internal");
const defaultConfig = {
    weights: {
        reconnect: 1,
        newSummarizer: 1,
        summaryNack: 1,
        submitOp: 1,
    },
};
function summarizerOperationGenerator(options) {
    const reconnect = async (_state) => ({
        type: "reconnect",
    });
    const newSummarizer = async (_state) => ({
        type: "newSummarizer",
    });
    const summaryNack = async (_state) => ({
        type: "summaryNack",
    });
    const submitOp = async (_state) => ({
        type: "submitOp",
    });
    const usableWeights = options.weights ?? defaultConfig.weights;
    return (0, stochastic_test_utils_1.createWeightedAsyncGenerator)([
        [reconnect, usableWeights.reconnect],
        [newSummarizer, usableWeights.newSummarizer],
        [summaryNack, usableWeights.summaryNack],
        [submitOp, usableWeights.submitOp],
    ]);
}
exports.summarizerOperationGenerator = summarizerOperationGenerator;
exports.baseModel = {
    reducer: makeReducer(),
};
function makeReducer() {
    const wrapper = (baseReducer) => async (state, operation) => {
        await baseReducer(state, operation);
        state.containerRuntimeFactory.processAllMessages();
    };
    const createNewSummarizer = async (state) => {
        const oldRuntime = state.containerRuntime;
        oldRuntime.disposeFn();
        state.containerRuntime = state.containerRuntimeFactory.createContainerRuntime(new internal_1.MockFluidDataStoreRuntime());
        await state.containerRuntime.initializeWithStashedOps(oldRuntime);
    };
    const reducer = (0, stochastic_test_utils_1.combineReducersAsync)({
        reconnect: async (state, _op) => {
            state.containerRuntime.connected = false;
            state.containerRuntime.connected = true;
            await createNewSummarizer(state);
        },
        newSummarizer: async (state, _op) => {
            await createNewSummarizer(state);
        },
        summaryNack: async (state, _op) => {
            state.containerRuntime.prepareSummaryNack();
            await state.containerRuntime.summarize();
        },
        submitOp: async (state, _op) => {
            // Send arbitrary runtime op
            state.containerRuntime.submit({}, {});
        },
    });
    return wrapper(reducer);
}
//# sourceMappingURL=fuzzUtils.js.map