"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateSummaryHeuristicConfiguration = exports.formCreateSummarizerFn = exports.DefaultSummaryConfiguration = exports.isSummariesDisabled = exports.summarizerRequestUrl = void 0;
const internal_1 = require("@fluidframework/container-definitions/internal");
const internal_2 = require("@fluidframework/driver-definitions/internal");
const internal_3 = require("@fluidframework/runtime-utils/internal");
const internal_4 = require("@fluidframework/telemetry-utils/internal");
const summarizerTypes_js_1 = require("./summarizerTypes.js");
exports.summarizerRequestUrl = "_summarizer";
function isSummariesDisabled(config) {
    return config.state === "disabled";
}
exports.isSummariesDisabled = isSummariesDisabled;
/**
 * @legacy
 * @alpha
 */
exports.DefaultSummaryConfiguration = {
    state: "enabled",
    minIdleTime: 0,
    maxIdleTime: 30 * 1000, // 30 secs.
    maxTime: 60 * 1000, // 1 min.
    maxOps: 100, // Summarize if 100 weighted ops received since last snapshot.
    minOpsForLastSummaryAttempt: 10,
    maxAckWaitTime: 3 * 60 * 1000, // 3 mins.
    maxOpsSinceLastSummary: 7000,
    initialSummarizerDelayMs: 5 * 1000, // 5 secs.
    nonRuntimeOpWeight: 0.1,
    runtimeOpWeight: 1,
    nonRuntimeHeuristicThreshold: 20,
};
/**
 * Returns a function that will create and retrieve a Summarizer.
 */
function formCreateSummarizerFn(loader) {
    return async () => {
        const request = {
            headers: {
                [internal_1.LoaderHeader.cache]: false,
                [internal_1.LoaderHeader.clientDetails]: {
                    capabilities: { interactive: false },
                    type: summarizerTypes_js_1.summarizerClientType,
                },
                [internal_2.DriverHeader.summarizingClient]: true,
                [internal_1.LoaderHeader.reconnect]: false,
            },
            url: `/${exports.summarizerRequestUrl}`,
        };
        const resolvedContainer = await loader.resolve(request);
        let fluidObject;
        // Older containers may not have the "getEntryPoint" API
        // ! This check will need to stay until LTS of loader moves past 2.0.0-internal.7.0.0
        if (resolvedContainer.getEntryPoint === undefined) {
            // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-explicit-any
            const response = (await resolvedContainer.request({
                url: `/${exports.summarizerRequestUrl}`,
            }));
            if (response.status !== 200 || response.mimeType !== "fluid/object") {
                throw (0, internal_3.responseToException)(response, request);
            }
            // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
            fluidObject = response.value;
        }
        else {
            fluidObject = await resolvedContainer.getEntryPoint();
        }
        if (fluidObject?.ISummarizer === undefined) {
            throw new internal_4.UsageError("Fluid object does not implement ISummarizer");
        }
        return fluidObject.ISummarizer;
    };
}
exports.formCreateSummarizerFn = formCreateSummarizerFn;
function validateSummaryHeuristicConfiguration(configuration) {
    // eslint-disable-next-line no-restricted-syntax
    for (const prop in configuration) {
        if (typeof configuration[prop] === "number" && configuration[prop] < 0) {
            throw new internal_4.UsageError(`Summary heuristic configuration property "${prop}" cannot be less than 0`);
        }
    }
    if (configuration.minIdleTime > configuration.maxIdleTime) {
        throw new internal_4.UsageError(`"minIdleTime" [${configuration.minIdleTime}] cannot be greater than "maxIdleTime" [${configuration.maxIdleTime}]`);
    }
}
exports.validateSummaryHeuristicConfiguration = validateSummaryHeuristicConfiguration;
//# sourceMappingURL=summaryHelpers.js.map