"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.createMockNodeKeyManager = exports.createNodeKeyManager = void 0;
const core_utils_1 = require("@fluidframework/core-utils");
const container_runtime_1 = require("@fluidframework/container-runtime");
const util_1 = require("../../util");
/**
 * 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, core_utils_1.assert)(idCompressor !== undefined, 0x6e4 /* Runtime IdCompressor must be available to generate local node keys */);
            return (0, util_1.brand)(idCompressor.generateCompressedId());
        },
        localizeNodeKey: (key) => {
            (0, core_utils_1.assert)(idCompressor !== undefined, 0x6e5 /* Runtime IdCompressor must be available to convert node keys */);
            return (0, util_1.brand)(idCompressor.recompress(key));
        },
        stabilizeNodeKey: (key) => {
            (0, core_utils_1.assert)(idCompressor !== undefined, 0x6e6 /* Runtime IdCompressor must be available to convert node keys */);
            return (0, util_1.brand)(
            // TODO: The assert below is required for type safety but is maybe slow
            (0, container_runtime_1.assertIsStableId)(idCompressor.decompress((0, util_1.extractFromOpaque)(key))));
        },
    };
}
exports.createNodeKeyManager = createNodeKeyManager;
/**
 * Create a {@link NodeKeyManager} that generates deterministic {@link StableNodeKey}s and {@link LocalNodeKey}s.
 * @remarks This is useful for test environments because it will always yield the same keys in the same order.
 * It should not be used for production environments for the same reason; the {@link StableNodeKey}s are not universally unique.
 */
function createMockNodeKeyManager() {
    return new MockNodeKeyManager();
}
exports.createMockNodeKeyManager = createMockNodeKeyManager;
class MockNodeKeyManager {
    constructor() {
        this.count = 0;
    }
    generateStableNodeKey() {
        return (0, util_1.brand)(this.createMockStableId(this.count++));
    }
    generateLocalNodeKey() {
        return this.localizeNodeKey(this.generateStableNodeKey());
    }
    localizeNodeKey(key) {
        return (0, util_1.brand)(Number.parseInt(key.substring(30), 16));
    }
    stabilizeNodeKey(key) {
        return (0, util_1.brand)(this.createMockStableId((0, util_1.extractFromOpaque)(key)));
    }
    createMockStableId(offset) {
        (0, core_utils_1.assert)(offset >= 0, 0x6e7 /* UUID offset may not be negative */);
        (0, core_utils_1.assert)(offset < 281474976710656, 0x6e8 /* UUID offset must be at most 16^12 */);
        return (0, container_runtime_1.assertIsStableId)(`a110ca7e-add1-4000-8000-${Math.round(offset).toString(16).padStart(12, "0")}`);
    }
}
//# sourceMappingURL=nodeKeyManager.js.map