"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.recordLikeDataToFlexContent = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../../feature-libraries/index.js");
const index_js_2 = require("../../util/index.js");
const index_js_3 = require("../core/index.js");
const createContext_js_1 = require("../createContext.js");
const unhydratedFlexTreeFromInsertable_js_1 = require("../unhydratedFlexTreeFromInsertable.js");
/**
 * Utilities shared by multiple node kinds.
 */
/**
 * Converts record-like data to a FlexContent representation for map/record schema.
 */
function recordLikeDataToFlexContent(fieldsIterator, schema) {
    const allowedChildTypes = (0, index_js_3.normalizeAllowedTypes)((0, index_js_3.unannotateImplicitAllowedTypes)(schema.info));
    const context = (0, createContext_js_1.getUnhydratedContext)(schema).flexContext;
    const transformedFields = new Map();
    for (const item of fieldsIterator) {
        const [key, value] = item;
        (0, internal_1.assert)(!transformedFields.has((0, index_js_2.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 = (0, unhydratedFlexTreeFromInsertable_js_1.unhydratedFlexTreeFromInsertableNode)(value, allowedChildTypes);
            const field = (0, index_js_3.createField)(context, index_js_1.FieldKinds.optional.identifier, (0, index_js_2.brand)(key), [child]);
            transformedFields.set((0, index_js_2.brand)(key), field);
        }
    }
    return [
        {
            type: (0, index_js_2.brand)(schema.identifier),
        },
        transformedFields,
    ];
}
exports.recordLikeDataToFlexContent = recordLikeDataToFlexContent;
//# sourceMappingURL=common.js.map