/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { channelsTreeName } from "@fluidframework/runtime-definitions/internal";
/** Path for nodes in a tree with escaped special characters */
export class EscapedPath {
    constructor(path) {
        this.path = path;
    }
    static create(path) {
        return new EscapedPath(encodeURIComponent(path));
    }
    static createAndConcat(pathParts) {
        let ret = EscapedPath.create(pathParts[0] ?? "");
        for (let i = 1; i < pathParts.length; i++) {
            ret = ret.concat(EscapedPath.create(pathParts[i]));
        }
        return ret;
    }
    toString() {
        return this.path;
    }
    concat(path) {
        return new EscapedPath(`${this.path}/${path.path}`);
    }
}
/** Information about a summary relevant to a specific node in the tree */
export class SummaryNode {
    /** Creates an instance that is valid for the root with specific basePath and localPath */
    static createForRoot(referenceSequenceNumber) {
        return new SummaryNode({
            referenceSequenceNumber,
            basePath: undefined,
            localPath: EscapedPath.create(""), // root hard-coded to ""
        });
    }
    /** Summary reference sequence number, i.e. last sequence number seen when it was created */
    get referenceSequenceNumber() {
        return this.summary.referenceSequenceNumber;
    }
    /** Full path to parent node, or undefined if this is the root */
    get basePath() {
        return this.summary.basePath;
    }
    /** Relative path to this node from its parent node */
    get localPath() {
        return this.summary.localPath;
    }
    /** Relative path from this node to its node innermost base summary */
    get additionalPath() {
        return this.summary.additionalPath;
    }
    set additionalPath(additionalPath) {
        this.summary.additionalPath = additionalPath;
    }
    constructor(summary) {
        this.summary = summary;
    }
    /** Gets the full path to this node, to be used when sending a handle */
    get fullPath() {
        return this.basePath?.concat(this.localPath) ?? this.localPath;
    }
    /**
     * Gets the full path to this node's innermost base summary.
     * The children nodes can use this as their basePath to determine their path.
     */
    get fullPathForChildren() {
        return this.additionalPath !== undefined
            ? this.fullPath.concat(this.additionalPath)
            : this.fullPath;
    }
    /**
     * Creates a new node within the same summary for a child of this node.
     * @param id - id of the child node
     */
    createForChild(id) {
        return new SummaryNode({
            referenceSequenceNumber: this.referenceSequenceNumber,
            basePath: this.fullPathForChildren,
            localPath: EscapedPath.create(id),
        });
    }
}
/**
 * Checks if the summary contains .channels subtree where the children subtrees
 * would be located if exists.
 * @param baseSummary - summary to check
 */
export function parseSummaryForSubtrees(baseSummary) {
    // New versions of snapshots have child nodes isolated in .channels subtree
    const channelsSubtree = baseSummary.trees[channelsTreeName];
    if (channelsSubtree !== undefined) {
        return {
            childrenTree: channelsSubtree,
            childrenPathPart: channelsTreeName,
        };
    }
    return {
        childrenTree: baseSummary,
        childrenPathPart: undefined,
    };
}
//# sourceMappingURL=summarizerNodeUtils.js.map