"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.MockNodeKeyManager = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const internal_2 = require("@fluidframework/id-compressor/internal");
const nodeKeyManager_js_1 = require("./nodeKeyManager.js");
const index_js_1 = require("../../util/index.js");
/**
 * Mock {@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.
 */
class MockNodeKeyManager {
    constructor() {
        this.count = 0;
    }
    generateStableNodeKey() {
        return (0, index_js_1.brand)(this.getId(this.count++));
    }
    generateLocalNodeKey() {
        return this.localizeNodeKey(this.generateStableNodeKey());
    }
    localizeNodeKey(key) {
        return this.tryLocalizeNodeKey(key) ?? (0, index_js_1.fail)("Key is not compressible");
    }
    stabilizeNodeKey(key) {
        return (0, index_js_1.brand)(this.getId((0, index_js_1.extractFromOpaque)(key)));
    }
    tryLocalizeNodeKey(key) {
        if (!(0, nodeKeyManager_js_1.isStableNodeKey)(key) || !key.startsWith("a110ca7e-add1-4000-8000-")) {
            return undefined;
        }
        const localNodeKey = Number.parseInt(key.substring(24), 16);
        return localNodeKey < this.count
            ? (0, index_js_1.brand)(localNodeKey)
            : undefined;
    }
    getId(offset) {
        (0, internal_1.assert)(offset >= 0, 0x6e7 /* UUID offset may not be negative */);
        (0, internal_1.assert)(offset < 281474976710656, 0x6e8 /* UUID offset must be at most 16^12 */);
        return (0, internal_2.assertIsStableId)(`a110ca7e-add1-4000-8000-${Math.round(offset).toString(16).padStart(12, "0")}`);
    }
}
exports.MockNodeKeyManager = MockNodeKeyManager;
//# sourceMappingURL=mockNodeKeyManager.js.map