/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert } from "@fluidframework/core-utils/internal";
import { assertIsStableId, isStableId } from "@fluidframework/id-compressor/internal";
import { brand, extractFromOpaque } from "../../util/index.js";
/**
 * Creates a {@link NodeIdentifierManager} from the given {@link IIdCompressor}.
 * @param idCompressor - the compressor to use for identifier generation, compression, and decompression.
 * If undefined, then attempts to generate or convert identifiers will throw an error.
 */
export function createNodeIdentifierManager(idCompressor) {
    return new DefaultNodeIdentifierManager(idCompressor);
}
class DefaultNodeIdentifierManager {
    constructor(idCompressor) {
        this.idCompressor = idCompressor;
    }
    generateLocalNodeIdentifier() {
        assert(this.idCompressor !== undefined, 0x6e4 /* Runtime IdCompressor must be available to generate local node identifiers */);
        return brand(this.idCompressor.generateCompressedId());
    }
    localizeNodeIdentifier(identifier) {
        assert(this.idCompressor !== undefined, 0x6e5 /* Runtime IdCompressor must be available to convert node identifiers */);
        return brand(this.idCompressor.recompress(identifier));
    }
    stabilizeNodeIdentifier(identifier) {
        assert(this.idCompressor !== undefined, 0x6e6 /* Runtime IdCompressor must be available to convert node identifiers */);
        return brand(
        // TODO: The assert below is required for type safety but is maybe slow
        assertIsStableId(this.idCompressor.decompress(extractFromOpaque(identifier))));
    }
    tryLocalizeNodeIdentifier(identifier) {
        assert(this.idCompressor !== undefined, 0x6e9 /* Runtime IdCompressor must be available to convert node identifiers */);
        if (isStableNodeIdentifier(identifier)) {
            const compressedIdentifier = this.idCompressor.tryRecompress(identifier);
            if (compressedIdentifier !== undefined) {
                return brand(compressedIdentifier);
            }
        }
    }
}
export function isStableNodeIdentifier(identifier) {
    return isStableId(identifier);
}
//# sourceMappingURL=nodeIdentifierManager.js.map