/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert, fail } from "@fluidframework/core-utils/internal";
import { assertIsStableId } from "@fluidframework/id-compressor/internal";
import { UsageError } from "@fluidframework/telemetry-utils/internal";
import { brand, extractFromOpaque } from "../../util/index.js";
import { isStableNodeIdentifier, } from "./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.
 */
export class MockNodeIdentifierManager {
    constructor() {
        this.count = 0;
    }
    generateStableNodeIdentifier() {
        return brand(this.getId(this.count++));
    }
    generateLocalNodeIdentifier() {
        return this.localizeNodeIdentifier(this.generateStableNodeIdentifier());
    }
    localizeNodeIdentifier(key) {
        return (this.tryLocalizeNodeIdentifier(key) ?? fail(0xb26 /* Identifier is not compressible */));
    }
    stabilizeNodeIdentifier(key) {
        return brand(this.getId(extractFromOpaque(key)));
    }
    tryLocalizeNodeIdentifier(key) {
        if (!isStableNodeIdentifier(key) || !key.startsWith("a110ca7e-add1-4000-8000-")) {
            return undefined;
        }
        const localNodeIdentifier = Number.parseInt(key.substring(24), 16);
        return localNodeIdentifier < this.count
            ? brand(localNodeIdentifier)
            : undefined;
    }
    getId(offset) {
        assert(offset < 281_474_976_710_656, 0x6e8 /* UUID offset must be at most 16^12 */);
        if (offset >= 0 && offset < this.count) {
            return assertIsStableId(`a110ca7e-add1-4000-8000-${Math.round(offset).toString(16).padStart(12, "0")}`);
        }
        else {
            throw new UsageError("Local ID offset out of bounds");
        }
    }
}
//# sourceMappingURL=mockNodeIdentifierManager.js.map