/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert } from "@fluidframework/common-utils";
import { brand, extractFromOpaque } from "../../util";
import { symbolFromKey } from "./globalFieldKeySymbol";
export function isLocalKey(key) {
    return typeof key === "string";
}
/**
 * The empty key ("") is used for unnamed relationships, such as the indexer
 * of an explicit array node.
 *
 * This key is a hint that this field is the primary function of the node,
 * and in some abstractions the APIs for this field should be inlined onto the node.
 *
 * TODO:
 * This has to be a LocalFieldKey since different nodes will have different FieldStoredSchema for it.
 * This makes it prone to collisions and suggests
 * that this intention may be better conveyed by metadata on the ITreeSchema.
 * @alpha
 */
export const EmptyKey = brand("");
/**
 * GlobalFieldKey to use for the root of documents.
 * TODO: if we do want to standardize on a single value for this,
 * it likely should be namespaced or a UUID to avoid risk of collisions.
 * @alpha
 */
export const rootFieldKey = brand("rootFieldKey");
/**
 * @alpha
 */
export const rootFieldKeySymbol = symbolFromKey(rootFieldKey);
/**
 * @alpha
 */
export const rootField = keyAsDetachedField(rootFieldKeySymbol);
/**
 * Some code abstracts the root as a node with detached fields as its fields.
 * This maps detached field to field keys for thus use.
 *
 * @returns `field` as a {@link FieldKey} usable on a special root node serving as a parent of detached fields.
 * @alpha
 */
export function detachedFieldAsKey(field) {
    if (field === rootField) {
        return rootFieldKeySymbol;
    }
    return brand(extractFromOpaque(field));
}
/**
 * The inverse of {@link detachedFieldAsKey}.
 * Thus must only be used on {@link LocalFieldKey}s which were produced via {@link detachedFieldAsKey},
 * and with the same scope (ex: forest) as the detachedFieldAsKey was originally from.
 * @alpha
 */
export function keyAsDetachedField(key) {
    if (isLocalKey(key)) {
        assert(key !== rootFieldKey, 0x3be /* Root is field key must be a global field key */);
        return brand(key);
    }
    assert(key === rootFieldKeySymbol, 0x3bf /* Root is only allowed global field key as detached field */);
    return brand(rootFieldKey);
}
//# sourceMappingURL=types.js.map