"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.createGroupOp = exports.createInsertOp = exports.createInsertSegmentOp = exports.createObliterateRangeOpSided = exports.createObliterateRangeOp = exports.createRemoveRangeOp = exports.createAdjustRangeOp = exports.createAnnotateRangeOp = exports.createAnnotateMarkerOp = void 0;
const ops_js_1 = require("./ops.js");
const sequencePlace_js_1 = require("./sequencePlace.js");
/**
 * Creates the op for annotating the markers with the provided properties
 * @param marker - The marker to annotate
 * @param props - The properties to annotate the marker with
 * @returns The annotate op
 *
 * @internal
 */
function createAnnotateMarkerOp(marker, props) {
    const id = marker.getId();
    if (!id) {
        return undefined;
    }
    return {
        props: { ...props },
        relativePos1: { id, before: true },
        relativePos2: { id },
        type: ops_js_1.MergeTreeDeltaType.ANNOTATE,
    };
}
exports.createAnnotateMarkerOp = createAnnotateMarkerOp;
/**
 * Creates the op for annotating the range with the provided properties
 * @param start - The inclusive start position of the range to annotate
 * @param end - The exclusive end position of the range to annotate
 * @param props - The properties to annotate the range with
 * @returns The annotate op
 *
 * @internal
 */
function createAnnotateRangeOp(start, end, props) {
    return {
        pos1: start,
        pos2: end,
        props: { ...props },
        type: ops_js_1.MergeTreeDeltaType.ANNOTATE,
    };
}
exports.createAnnotateRangeOp = createAnnotateRangeOp;
/**
 * Creates the op for annotating the range with the provided properties
 * @param start - The inclusive start position of the range to annotate
 * @param end - The exclusive end position of the range to annotate
 * @param props - The properties to annotate the range with
 * @returns The annotate op
 *
 * @internal
 */
function createAdjustRangeOp(start, end, adjust) {
    return {
        pos1: start,
        pos2: end,
        adjust: { ...adjust },
        type: ops_js_1.MergeTreeDeltaType.ANNOTATE,
    };
}
exports.createAdjustRangeOp = createAdjustRangeOp;
/**
 * Creates the op to remove a range
 *
 * @param start - The inclusive start of the range to remove
 * @param end - The exclusive end of the range to remove
 *
 * @internal
 */
function createRemoveRangeOp(start, end) {
    return {
        pos1: start,
        pos2: end,
        type: ops_js_1.MergeTreeDeltaType.REMOVE,
    };
}
exports.createRemoveRangeOp = createRemoveRangeOp;
/**
 * Creates the op to obliterate a range
 *
 * @param start - The inclusive start of the range to obliterate
 * @param end - The exclusive end of the range to obliterate
 *
 * @internal
 */
// eslint-disable-next-line import-x/no-deprecated
function createObliterateRangeOp(start, end) {
    return {
        pos1: start,
        pos2: end,
        type: ops_js_1.MergeTreeDeltaType.OBLITERATE,
    };
}
exports.createObliterateRangeOp = createObliterateRangeOp;
/**
 * Creates the op to obliterate a range
 *
 * @param start - The start of the range to obliterate.
 * If a number is provided, the range will start before that index.
 * @param end - The end of the range to obliterate.
 * If a number is provided, the range will end after that index -1.
 * This preserves the previous behavior of not expanding obliteration ranges at the endpoints
 * for uses which predate the availability of endpoint expansion.
 *
 * @internal
 */
function createObliterateRangeOpSided(start, end) {
    const startPlace = (0, sequencePlace_js_1.normalizePlace)(start);
    // If a number is provided, default to after the previous index.
    // This preserves the behavior of obliterate prior to the introduction of endpoint expansion.
    const endPlace = typeof end === "number"
        ? { pos: end - 1, side: sequencePlace_js_1.Side.After } // default to inclusive bounds
        : (0, sequencePlace_js_1.normalizePlace)(end);
    return {
        type: ops_js_1.MergeTreeDeltaType.OBLITERATE_SIDED,
        pos1: { pos: startPlace.pos, before: startPlace.side === sequencePlace_js_1.Side.Before },
        pos2: { pos: endPlace.pos, before: endPlace.side === sequencePlace_js_1.Side.Before },
    };
}
exports.createObliterateRangeOpSided = createObliterateRangeOpSided;
/**
 * Creates an op for inserting a segment at the specified position.
 *
 * @param pos - The position to insert the segment at
 * @param segment - The segment to insert
 *
 * @internal
 */
function createInsertSegmentOp(pos, segment) {
    return createInsertOp(pos, segment.toJSONObject());
}
exports.createInsertSegmentOp = createInsertSegmentOp;
/**
 * Creates the op for inserting a segment from its JSON representation at
 * the specified position.
 *
 * @internal
 */
function createInsertOp(pos, segSpec) {
    return {
        pos1: pos,
        seg: segSpec,
        type: ops_js_1.MergeTreeDeltaType.INSERT,
    };
}
exports.createInsertOp = createInsertOp;
/**
 * Creates a group op from the provided ops.
 *
 * @param ops - The ops to group
 *
 * @internal
 */
// eslint-disable-next-line import-x/no-deprecated
function createGroupOp(...ops) {
    return {
        ops,
        type: ops_js_1.MergeTreeDeltaType.GROUP,
    };
}
exports.createGroupOp = createGroupOp;
//# sourceMappingURL=opBuilder.js.map