"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.unhydratedFlexTreeFromCursor = exports.createFromCursor = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../../core/index.js");
const index_js_2 = require("../core/index.js");
const index_js_3 = require("../../feature-libraries/index.js");
const createContext_js_1 = require("../createContext.js");
const index_js_4 = require("../node-kinds/index.js");
const toStoredSchema_js_1 = require("../toStoredSchema.js");
const customTree_js_1 = require("./customTree.js");
/**
 * Creates an unhydrated simple-tree field from a cursor in nodes mode.
 * @remarks
 * Does not support defaults.
 * Validates the field is in schema.
 */
function createFromCursor(schema, cursor) {
    const context = (0, createContext_js_1.getUnhydratedContext)(schema);
    const mapTrees = cursor === undefined ? [] : [unhydratedFlexTreeFromCursor(context, cursor)];
    const flexSchema = context.flexContext.schema;
    const schemaValidationPolicy = {
        policy: {
            ...index_js_3.defaultSchemaPolicy,
            allowUnknownOptionalFields: (0, index_js_4.createUnknownOptionalFieldPolicy)(schema),
        },
        schema: context.flexContext.schema,
    };
    const maybeError = (0, index_js_3.isFieldInSchema)(mapTrees, flexSchema.rootFieldSchema, schemaValidationPolicy);
    (0, index_js_3.inSchemaOrThrow)(maybeError);
    if (mapTrees.length === 0) {
        return undefined;
    }
    (0, internal_1.assert)(mapTrees.length === 1, 0xa11 /* unexpected field length */);
    // Length asserted above, so this is safe. This assert is done instead of checking for undefined after indexing to ensure a length greater than 1 also errors.
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    const mapTree = mapTrees[0];
    return (0, index_js_2.getOrCreateNodeFromInnerNode)(mapTree);
}
exports.createFromCursor = createFromCursor;
/**
 * Construct an {@link UnhydratedFlexTreeNode} from a cursor in Nodes mode.
 * @remarks
 * This does not validate the node is in schema.
 */
function unhydratedFlexTreeFromCursor(context, cursor) {
    (0, internal_1.assert)(cursor.mode === 0 /* CursorLocationType.Nodes */, 0xbb4 /* Expected nodes cursor */);
    const schema = context.schema.get(cursor.type) ?? (0, customTree_js_1.unknownTypeError)(cursor.type);
    const storedSchema = (0, toStoredSchema_js_1.getStoredSchema)(schema);
    const fields = new Map((0, index_js_1.mapCursorFields)(cursor, () => [
        cursor.getFieldKey(),
        (0, index_js_2.createField)(context.flexContext, storedSchema.getFieldSchema(cursor.getFieldKey()).kind, cursor.getFieldKey(), (0, index_js_1.mapCursorField)(cursor, () => unhydratedFlexTreeFromCursor(context, cursor))),
    ]));
    return new index_js_2.UnhydratedFlexTreeNode({ type: cursor.type, value: cursor.value }, fields, context);
}
exports.unhydratedFlexTreeFromCursor = unhydratedFlexTreeFromCursor;
//# sourceMappingURL=create.js.map