"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.isStableNodeKey = exports.createNodeKeyManager = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const internal_2 = require("@fluidframework/id-compressor/internal");
const index_js_1 = require("../../util/index.js");
/**
 * Creates a {@link NodeKeyManager} from the given {@link IIdCompressor}.
 * @param idCompressor - the compressor to use for key generation, compression, and decompression.
 * If undefined, then attempts to generate or convert keys will throw an error.
 */
function createNodeKeyManager(idCompressor) {
    return {
        generateLocalNodeKey: () => {
            (0, internal_1.assert)(idCompressor !== undefined, 0x6e4 /* Runtime IdCompressor must be available to generate local node keys */);
            return (0, index_js_1.brand)(idCompressor.generateCompressedId());
        },
        localizeNodeKey: (key) => {
            (0, internal_1.assert)(idCompressor !== undefined, 0x6e5 /* Runtime IdCompressor must be available to convert node keys */);
            return (0, index_js_1.brand)(idCompressor.recompress(key));
        },
        stabilizeNodeKey: (key) => {
            (0, internal_1.assert)(idCompressor !== undefined, 0x6e6 /* Runtime IdCompressor must be available to convert node keys */);
            return (0, index_js_1.brand)(
            // TODO: The assert below is required for type safety but is maybe slow
            (0, internal_2.assertIsStableId)(idCompressor.decompress((0, index_js_1.extractFromOpaque)(key))));
        },
        tryLocalizeNodeKey: (key) => {
            (0, internal_1.assert)(idCompressor !== undefined, 0x6e9 /* Runtime IdCompressor must be available to convert node keys */);
            if (isStableNodeKey(key)) {
                const compressedKey = idCompressor.tryRecompress(key);
                if (compressedKey !== undefined) {
                    return (0, index_js_1.brand)(compressedKey);
                }
            }
        },
    };
}
exports.createNodeKeyManager = createNodeKeyManager;
function isStableNodeKey(key) {
    return (0, internal_2.isStableId)(key);
}
exports.isStableNodeKey = isStableNodeKey;
//# sourceMappingURL=nodeKeyManager.js.map