"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 getTreeNodeForField_js_1 = require("../../getTreeNodeForField.js");
const schemaTypes_js_1 = require("../../schemaTypes.js");
const index_js_1 = require("../../core/index.js");
const unhydratedFlexTreeFromInsertable_js_1 = require("../../unhydratedFlexTreeFromInsertable.js");
const prepareForInsertion_js_1 = require("../../prepareForInsertion.js");
const index_js_2 = require("../../../util/index.js");
const treeNodeValid_js_1 = require("../../treeNodeValid.js");
const createContext_js_1 = require("../../createContext.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 treeNodeValid_js_1.TreeNodeValid {
    constructor(input) {
        super(input ?? []);
    }
    [Symbol.iterator]() {
        return this.entries();
    }
    get innerNode() {
        return (0, index_js_1.getOrCreateInnerNode)(this);
    }
    editor(key) {
        const field = this.innerNode.getBoxed((0, index_js_2.brand)(key));
        return field.editor;
    }
    delete(key) {
        const field = this.innerNode.getBoxed((0, index_js_2.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_2.brand)(key));
        return (0, getTreeNodeForField_js_1.tryGetTreeNodeForField)(field);
    }
    has(key) {
        return this.innerNode.tryGetField((0, index_js_2.brand)(key)) !== undefined;
    }
    keys() {
        const node = this.innerNode;
        return node.keys();
    }
    set(key, value) {
        const kernel = (0, index_js_1.getKernel)(this);
        const node = this.innerNode;
        const mapTree = (0, prepareForInsertion_js_1.prepareForInsertion)(value, (0, schemaTypes_js_1.createFieldSchema)(schemaTypes_js_1.FieldKind.Optional, kernel.schema.info), node.context);
        const field = node.getBoxed((0, index_js_2.brand)(key));
        this.editor(key).set(mapTree, field.length === 0);
        return this;
    }
    get size() {
        return (0, index_js_2.count)(this.innerNode.keys());
    }
    *values() {
        for (const [, value] of this.entries()) {
            yield value;
        }
    }
    forEach(callbackFn, thisArg) {
        for (const field of (0, index_js_1.getOrCreateInnerNode)(this)) {
            const node = (0, getTreeNodeForField_js_1.tryGetTreeNodeForField)(field);
            callbackFn.call(thisArg, node, field.key, this);
        }
    }
}
CustomMapNodeBase.kind = index_js_1.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, metadata, persistedMetadata) {
    const lazyChildTypes = new internal_1.Lazy(() => (0, schemaTypes_js_1.normalizeAllowedTypes)((0, schemaTypes_js_1.unannotateImplicitAllowedTypes)(info)));
    const lazyAnnotatedTypes = new internal_1.Lazy(() => [(0, schemaTypes_js_1.normalizeAnnotatedAllowedTypes)(info)]);
    const lazyAllowedTypesIdentifiers = new internal_1.Lazy(() => new Set([...lazyChildTypes.value].map((type) => type.identifier)));
    let unhydratedContext;
    class Schema extends CustomMapNodeBase {
        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 oneTimeSetup() {
            const schema = this;
            unhydratedContext = (0, createContext_js_1.getUnhydratedContext)(schema);
            return unhydratedContext;
        }
        static get childTypes() {
            return lazyChildTypes.value;
        }
        static get childAnnotatedAllowedTypes() {
            return lazyAnnotatedTypes.value;
        }
        // eslint-disable-next-line import/no-deprecated
        get [index_js_1.typeNameSymbol]() {
            return identifier;
        }
        get [index_js_1.typeSchemaSymbol]() {
            return Schema.constructorCached?.constructor;
        }
    }
    Schema.constructorCached = undefined;
    Schema.identifier = identifier;
    Schema.info = info;
    Schema.implicitlyConstructable = implicitlyConstructable;
    Schema.metadata = metadata ?? {};
    Schema.persistedMetadata = persistedMetadata;
    const schemaErased = Schema;
    return schemaErased;
}
exports.mapSchema = mapSchema;
//# sourceMappingURL=mapNode.js.map