/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert } from "@fluidframework/core-utils/internal";
import { FieldKinds } from "../../feature-libraries/index.js";
import { brand } from "../../util/index.js";
import { createField, normalizeAllowedTypes, } from "../core/index.js";
import { getUnhydratedContext } from "../createContext.js";
import { unhydratedFlexTreeFromInsertableNode, } from "../unhydratedFlexTreeFromInsertable.js";
/**
 * Utilities shared by multiple node kinds.
 */
/**
 * Converts record-like data to a FlexContent representation for map/record schema.
 */
export function recordLikeDataToFlexContent(fieldsIterator, schema) {
    const allowedChildTypes = normalizeAllowedTypes(schema.info);
    const context = getUnhydratedContext(schema).flexContext;
    const transformedFields = new Map();
    for (const item of fieldsIterator) {
        const [key, value] = item;
        assert(!transformedFields.has(brand(key)), 0x84c /* Keys should not be duplicated */);
        // Omit undefined values - an entry with an undefined value is equivalent to one that has been removed or omitted
        if (value !== undefined) {
            const child = unhydratedFlexTreeFromInsertableNode(value, allowedChildTypes.evaluateSet());
            const field = createField(context, FieldKinds.optional.identifier, brand(key), [child]);
            transformedFields.set(brand(key), field);
        }
    }
    return [
        {
            type: brand(schema.identifier),
        },
        transformedFields,
    ];
}
//# sourceMappingURL=common.js.map