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