"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.mapSchema = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const internal_2 = require("@fluidframework/telemetry-utils/internal");
const index_js_1 = require("../../../core/index.js");
const index_js_2 = require("../../../feature-libraries/index.js");
const index_js_3 = require("../../../util/index.js");
const index_js_4 = require("../../core/index.js");
const createContext_js_1 = require("../../createContext.js");
const fieldSchema_js_1 = require("../../fieldSchema.js");
const getTreeNodeForField_js_1 = require("../../getTreeNodeForField.js");
const prepareForInsertion_js_1 = require("../../prepareForInsertion.js");
const unhydratedFlexTreeFromInsertable_js_1 = require("../../unhydratedFlexTreeFromInsertable.js");
const common_js_1 = require("../common.js");
// TreeMapNode is invariant over schema type, so for this handler to work with all schema, the only possible type for the schema is `any`.
// This is not ideal, but no alternatives are possible.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handler = {
    getPrototypeOf: () => {
        return Map.prototype;
    },
};
class CustomMapNodeBase extends index_js_4.TreeNodeValid {
    constructor(input) {
        super(input ?? []);
    }
    [Symbol.iterator]() {
        return this.entries();
    }
    get innerNode() {
        return (0, index_js_4.getInnerNode)(this);
    }
    editor(key) {
        const field = this.innerNode.getBoxed((0, index_js_3.brand)(key));
        return field.editor;
    }
    delete(key) {
        const field = this.innerNode.getBoxed((0, index_js_3.brand)(key));
        this.editor(key).set(undefined, field.length === 0);
    }
    *entries() {
        const node = this.innerNode;
        for (const key of node.keys()) {
            yield [
                key,
                (0, getTreeNodeForField_js_1.tryGetTreeNodeForField)(node.getBoxed(key)),
            ];
        }
    }
    get(key) {
        const node = this.innerNode;
        const field = node.getBoxed((0, index_js_3.brand)(key));
        return (0, getTreeNodeForField_js_1.tryGetTreeNodeForField)(field);
    }
    has(key) {
        return this.innerNode.tryGetField((0, index_js_3.brand)(key)) !== undefined;
    }
    keys() {
        const node = this.innerNode;
        return node.keys();
    }
    set(key, value) {
        const kernel = (0, index_js_4.getKernel)(this);
        const node = this.innerNode;
        const innerSchema = this.innerNode.context.schema.nodeSchema.get((0, index_js_3.brand)(kernel.schema.identifier));
        (0, internal_1.assert)(innerSchema instanceof index_js_1.MapNodeStoredSchema, 0xc17 /* Expected MapNodeStoredSchema */);
        const mapTree = (0, prepareForInsertion_js_1.prepareForInsertion)(value, (0, fieldSchema_js_1.createFieldSchema)(fieldSchema_js_1.FieldKind.Optional, kernel.schema.info), node.context, innerSchema.mapFields);
        const field = node.getBoxed((0, index_js_3.brand)(key));
        this.editor(key).set(mapTree, field.length === 0);
        return this;
    }
    get size() {
        return (0, index_js_3.count)(this.innerNode.keys());
    }
    *values() {
        for (const [, value] of this.entries()) {
            yield value;
        }
    }
    forEach(callbackFn, thisArg) {
        for (const field of (0, index_js_4.getInnerNode)(this)) {
            const node = (0, getTreeNodeForField_js_1.tryGetTreeNodeForField)(field);
            callbackFn.call(thisArg, node, field.key, this);
        }
    }
}
CustomMapNodeBase.kind = index_js_4.NodeKind.Map;
/**
 * Define a {@link TreeNodeSchema} for a {@link (TreeArrayNode:interface)}.
 *
 * @param base - base schema type to extend.
 * @param useMapPrototype - should this type emulate a ES6 Map object (by faking its prototype with a proxy).
 * @param persistedMetadata - Optional persisted metadata for the object node schema.
 */
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
function mapSchema(identifier, info, implicitlyConstructable, useMapPrototype, nodeOptions = {}) {
    var _a;
    const normalizedTypes = (0, index_js_4.normalizeAllowedTypes)(info);
    const lazyAllowedTypesIdentifiers = new internal_1.Lazy(() => new Set(normalizedTypes.evaluate().map((type) => type.identifier)));
    const lazySimpleAllowedTypes = new internal_1.Lazy(() => {
        return index_js_4.AnnotatedAllowedTypesInternal.evaluateSimpleAllowedTypes(normalizedTypes);
    });
    let privateData;
    const persistedMetadata = nodeOptions.persistedMetadata;
    class Schema extends CustomMapNodeBase {
        constructor() {
            super(...arguments);
            this[_a] = "TreeMapNodeSchema";
        }
        static prepareInstance(instance, flexNode) {
            if (useMapPrototype) {
                return new Proxy(instance, handler);
            }
            return instance;
        }
        static buildRawNode(instance, input) {
            return (0, unhydratedFlexTreeFromInsertable_js_1.unhydratedFlexTreeFromInsertable)(input, this);
        }
        static get allowedTypesIdentifiers() {
            return lazyAllowedTypesIdentifiers.value;
        }
        static get simpleAllowedTypes() {
            return lazySimpleAllowedTypes.value;
        }
        static oneTimeSetup() {
            const schema = this;
            return (0, createContext_js_1.getTreeNodeSchemaInitializedData)(this, {
                shallowCompatibilityTest,
                toFlexContent: (data) => mapToFlexContent(data, schema),
            });
        }
        static get childTypes() {
            return normalizedTypes.evaluateSet();
        }
        // eslint-disable-next-line import-x/no-deprecated
        get [(_a = Symbol.toStringTag, index_js_4.typeNameSymbol)]() {
            return identifier;
        }
        get [index_js_4.typeSchemaSymbol]() {
            return Schema.constructorCached?.constructor;
        }
        static get [index_js_4.privateDataSymbol]() {
            return (privateData ??= (0, index_js_4.createTreeNodeSchemaPrivateData)(this, [normalizedTypes]));
        }
    }
    Schema.constructorCached = undefined;
    Schema.identifier = identifier;
    Schema.info = info;
    Schema.implicitlyConstructable = implicitlyConstructable;
    Schema.metadata = nodeOptions.metadata ?? {};
    Schema.persistedMetadata = persistedMetadata;
    const schemaErased = Schema;
    return schemaErased;
}
exports.mapSchema = mapSchema;
/**
 * {@link TreeNodeSchemaInitializedData.shallowCompatibilityTest} for Map nodes.
 */
function shallowCompatibilityTest(data) {
    if ((0, index_js_2.isTreeValue)(data)) {
        return index_js_4.CompatibilityLevel.None;
    }
    if ((0, index_js_3.isReadonlyArray)(data)) {
        // Arrays are iterable, so type checking does allow constructing a MapNode from an array if the array's type is key values pairs for the map.
        return index_js_4.CompatibilityLevel.Low;
    }
    if (Symbol.iterator in data) {
        return index_js_4.CompatibilityLevel.Normal;
    }
    // When not unioned with an ObjectNode, allow objects to be used to create maps.
    return index_js_4.CompatibilityLevel.Low;
}
/**
 * {@link TreeNodeSchemaInitializedData.toFlexContent} for Map nodes.
 *
 * Transforms data under a Map schema.
 * @param data - The tree data to be transformed. Must be an iterable or Record like object.
 * @param schema - The schema to comply with.
 */
function mapToFlexContent(data, schema) {
    if (!(typeof data === "object" && data !== null)) {
        throw new internal_2.UsageError(`Input data is incompatible with Map schema: ${data}`);
    }
    const fieldsIterator = (Symbol.iterator in data
        ? // Support iterables of key value pairs (including Map objects)
            data
        : // Support record objects for JSON style Map data
            Object.entries(data));
    return (0, common_js_1.recordLikeDataToFlexContent)(fieldsIterator, schema);
}
//# sourceMappingURL=mapNode.js.map