"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.MockNodeIdentifierManager = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const internal_2 = require("@fluidframework/id-compressor/internal");
const internal_3 = require("@fluidframework/telemetry-utils/internal");
const index_js_1 = require("../../util/index.js");
const nodeIdentifierManager_js_1 = require("./nodeIdentifierManager.js");
/**
 * Mock {@link NodeIdentifierManager} that generates deterministic {@link StableNodeIdentifier}s and {@link LocalNodeIdentifier}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 StableNodeIdentifier}s are not universally unique.
 */
class MockNodeIdentifierManager {
    constructor() {
        this.count = 0;
    }
    generateStableNodeIdentifier() {
        return (0, index_js_1.brand)(this.getId(this.count++));
    }
    generateLocalNodeIdentifier() {
        return this.localizeNodeIdentifier(this.generateStableNodeIdentifier());
    }
    localizeNodeIdentifier(key) {
        return (this.tryLocalizeNodeIdentifier(key) ?? (0, internal_1.fail)(0xb26 /* Identifier is not compressible */));
    }
    stabilizeNodeIdentifier(key) {
        return (0, index_js_1.brand)(this.getId((0, index_js_1.extractFromOpaque)(key)));
    }
    tryLocalizeNodeIdentifier(key) {
        if (!(0, nodeIdentifierManager_js_1.isStableNodeIdentifier)(key) || !key.startsWith("a110ca7e-add1-4000-8000-")) {
            return undefined;
        }
        const localNodeIdentifier = Number.parseInt(key.slice(24), 16);
        return localNodeIdentifier < this.count
            ? (0, index_js_1.brand)(localNodeIdentifier)
            : undefined;
    }
    getId(offset) {
        (0, internal_1.assert)(offset < 281_474_976_710_656, 0x6e8 /* UUID offset must be at most 16^12 */);
        if (offset >= 0 && offset < this.count) {
            return (0, internal_2.assertIsStableId)(`a110ca7e-add1-4000-8000-${Math.round(offset).toString(16).padStart(12, "0")}`);
        }
        else {
            throw new internal_3.UsageError("Local ID offset out of bounds");
        }
    }
}
exports.MockNodeIdentifierManager = MockNodeIdentifierManager;
//# sourceMappingURL=mockNodeIdentifierManager.js.map