"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPropertyTrackingAndInsertionAttributionPolicyFactory = exports.createPropertyTrackingAttributionPolicyFactory = exports.createInsertOnlyAttributionPolicy = void 0;
const core_utils_1 = require("@fluidframework/core-utils");
const mergeTreeDeltaCallback_1 = require("./mergeTreeDeltaCallback.cjs");
const ops_1 = require("./ops.cjs");
const attributionCollection_1 = require("./attributionCollection.cjs");
function createAttributionPolicyFromCallbacks({ delta, maintenance, }) {
    let unsubscribe;
    return {
        // eslint-disable-next-line import/no-deprecated
        attach: (client) => {
            (0, core_utils_1.assert)(unsubscribe === undefined, 0x557 /* cannot attach to multiple clients at once */);
            const deltaSubscribed = (opArgs, deltaArgs) => delta(opArgs, deltaArgs, client);
            const maintenanceSubscribed = (args, opArgs) => maintenance(args, opArgs, client);
            client.on("delta", deltaSubscribed);
            client.on("maintenance", maintenanceSubscribed);
            unsubscribe = () => {
                client.off("delta", deltaSubscribed);
                client.off("maintenance", maintenanceSubscribed);
            };
        },
        detach: () => {
            unsubscribe?.();
            unsubscribe = undefined;
        },
        get isAttached() {
            return unsubscribe !== undefined;
        },
        serializer: attributionCollection_1.AttributionCollection,
    };
}
const ensureAttributionCollectionCallbacks = {
    delta: ({ op }, { deltaSegments }) => {
        if (op.type === ops_1.MergeTreeDeltaType.INSERT) {
            for (const { segment } of deltaSegments) {
                segment.attribution = new attributionCollection_1.AttributionCollection(segment.cachedLength);
            }
        }
    },
    maintenance: () => { },
};
const getAttributionKey = (
// eslint-disable-next-line import/no-deprecated
client, msg) => {
    if (msg) {
        return { type: "op", seq: msg.sequenceNumber };
    }
    const collabWindow = client.getCollabWindow();
    return collabWindow.collaborating ? { type: "local" } : { type: "detached", id: 0 };
};
const attributeInsertionOnSegments = (deltaSegments, key) => {
    for (const { segment } of deltaSegments) {
        if (segment.seq !== undefined) {
            segment.attribution?.update(undefined, new attributionCollection_1.AttributionCollection(segment.cachedLength, key));
        }
    }
};
const insertOnlyAttributionPolicyCallbacks = {
    delta: (opArgs, { deltaSegments, operation }, client) => {
        if (operation === ops_1.MergeTreeDeltaType.INSERT) {
            attributeInsertionOnSegments(deltaSegments, getAttributionKey(client, opArgs.sequencedMessage));
        }
    },
    maintenance: ({ deltaSegments, operation }, opArgs, client) => {
        if (operation === mergeTreeDeltaCallback_1.MergeTreeMaintenanceType.ACKNOWLEDGED &&
            opArgs?.op.type === ops_1.MergeTreeDeltaType.INSERT) {
            attributeInsertionOnSegments(deltaSegments, getAttributionKey(client, opArgs.sequencedMessage));
        }
    },
};
function createPropertyTrackingMergeTreeCallbacks(...propNames) {
    const toTrack = propNames.map((entry) => ({ propName: entry, channelName: entry }));
    const attributeAnnotateOnSegments = (deltaSegments, { op, sequencedMessage }, key) => {
        for (const { segment } of deltaSegments) {
            for (const { propName, channelName } of toTrack) {
                const shouldAttributeInsert = op.type === ops_1.MergeTreeDeltaType.INSERT &&
                    segment.properties?.[propName] !== undefined;
                const isLocal = sequencedMessage === undefined;
                const shouldAttributeAnnotate = op.type === ops_1.MergeTreeDeltaType.ANNOTATE &&
                    // Only attribute annotations which change the tracked property
                    op.props[propName] !== undefined &&
                    // Local changes to the tracked property always take effect
                    (isLocal ||
                        // Acked changes only take effect if there isn't a pending local change
                        (!isLocal && !segment.propertyManager?.hasPendingProperty(propName)));
                if (shouldAttributeInsert || shouldAttributeAnnotate) {
                    segment.attribution?.update(channelName, new attributionCollection_1.AttributionCollection(segment.cachedLength, key));
                }
            }
        }
    };
    return {
        delta: (opArgs, { deltaSegments }, client) => {
            const { op, sequencedMessage } = opArgs;
            if (op.type === ops_1.MergeTreeDeltaType.ANNOTATE || op.type === ops_1.MergeTreeDeltaType.INSERT) {
                attributeAnnotateOnSegments(deltaSegments, opArgs, getAttributionKey(client, sequencedMessage));
            }
        },
        maintenance: ({ deltaSegments, operation }, opArgs, client) => {
            if (operation === mergeTreeDeltaCallback_1.MergeTreeMaintenanceType.ACKNOWLEDGED && opArgs !== undefined) {
                attributeAnnotateOnSegments(deltaSegments, opArgs, getAttributionKey(client, opArgs.sequencedMessage));
            }
        },
    };
}
function combineMergeTreeCallbacks(callbacks) {
    return {
        delta: (...args) => callbacks.forEach(({ delta }) => delta(...args)),
        maintenance: (...args) => callbacks.forEach(({ maintenance }) => maintenance(...args)),
    };
}
/**
 * @returns An {@link AttributionPolicy} which tracks only insertion of content.
 * @internal
 */
function createInsertOnlyAttributionPolicy() {
    return createAttributionPolicyFromCallbacks(combineMergeTreeCallbacks([
        ensureAttributionCollectionCallbacks,
        insertOnlyAttributionPolicyCallbacks,
    ]));
}
exports.createInsertOnlyAttributionPolicy = createInsertOnlyAttributionPolicy;
/**
 * @param propNames - List of property names for which attribution should be tracked.
 * @returns A policy which only attributes annotation of the properties specified.
 * Keys for each property are stored under attribution channels of the same name--see example below.
 *
 * @example
 *
 * ```typescript
 * // Use this policy when creating your merge-tree:
 * const policy = createPropertyTrackingAttributionPolicyFactory("bold", "italic");
 * // ... later, you can get attribution keys for the last time the "bold" and "italic"
 * // properties were changed on a segment using `getAtOffset`:
 * const lastBoldedAttributionKey = segment.attribution?.getAtOffset(0, "bold");
 * const lastItalicizedAttributionKey = segment.attribution?.getAtOffset(0, "italic");
 * ```
 * @alpha
 */
function createPropertyTrackingAttributionPolicyFactory(...propNames) {
    return () => createAttributionPolicyFromCallbacks(combineMergeTreeCallbacks([
        ensureAttributionCollectionCallbacks,
        createPropertyTrackingMergeTreeCallbacks(...propNames),
    ]));
}
exports.createPropertyTrackingAttributionPolicyFactory = createPropertyTrackingAttributionPolicyFactory;
/**
 * Creates an attribution policy which tracks insertion as well as annotation of certain property names.
 * This combines the policies creatable using {@link createPropertyTrackingAttributionPolicyFactory} and
 * {@link createInsertOnlyAttributionPolicy}: see there for more details.
 * @alpha
 */
function createPropertyTrackingAndInsertionAttributionPolicyFactory(...propNames) {
    return () => createAttributionPolicyFromCallbacks(combineMergeTreeCallbacks([
        ensureAttributionCollectionCallbacks,
        insertOnlyAttributionPolicyCallbacks,
        createPropertyTrackingMergeTreeCallbacks(...propNames),
    ]));
}
exports.createPropertyTrackingAndInsertionAttributionPolicyFactory = createPropertyTrackingAndInsertionAttributionPolicyFactory;
//# sourceMappingURL=attributionPolicy.cjs.map