/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { UnhydratedContext, getOrCreateMapTreeNode, getSchemaAndPolicy, } from "../feature-libraries/index.js";
import { getTreeNodeForField, prepareContentForHydration } from "./proxies.js";
import { getOrCreateInnerNode } from "./proxyBinding.js";
import { createFieldSchema, FieldKind, } from "./schemaTypes.js";
import { getKernel, NodeKind, 
// eslint-disable-next-line import/no-deprecated
typeNameSymbol, typeSchemaSymbol, } from "./core/index.js";
import { mapTreeFromNodeData, } from "./toMapTree.js";
import { getFlexSchema, toFlexSchema } from "./toFlexSchema.js";
import { brand, count } from "../util/index.js";
import { TreeNodeValid } from "./treeNodeValid.js";
const handler = {
    getPrototypeOf: () => {
        return Map.prototype;
    },
};
class CustomMapNodeBase extends TreeNodeValid {
    [Symbol.iterator]() {
        return this.entries();
    }
    get innerNode() {
        return getOrCreateInnerNode(this);
    }
    editor(key) {
        const field = this.innerNode.getBoxed(brand(key));
        return field.editor;
    }
    delete(key) {
        const field = this.innerNode.getBoxed(brand(key));
        this.editor(key).set(undefined, field.length === 0);
    }
    *entries() {
        const node = this.innerNode;
        for (const key of node.keys()) {
            yield [
                key,
                getTreeNodeForField(node.getBoxed(key)),
            ];
        }
    }
    get(key) {
        const node = this.innerNode;
        const field = node.getBoxed(brand(key));
        return getTreeNodeForField(field);
    }
    has(key) {
        return this.innerNode.tryGetField(brand(key)) !== undefined;
    }
    keys() {
        const node = this.innerNode;
        return node.keys();
    }
    set(key, value) {
        const kernel = getKernel(this);
        const node = this.innerNode;
        const mapTree = mapTreeFromNodeData(value, createFieldSchema(FieldKind.Optional, kernel.schema.info), node.context.isHydrated() ? node.context.nodeKeyManager : undefined, getSchemaAndPolicy(node));
        const field = node.getBoxed(brand(key));
        if (node.context.isHydrated()) {
            prepareContentForHydration(mapTree, node.context.checkout.forest);
        }
        this.editor(key).set(mapTree, field.length === 0);
        return this;
    }
    get size() {
        return count(this.innerNode.keys());
    }
    *values() {
        for (const [, value] of this.entries()) {
            yield value;
        }
    }
    forEach(callbackFn, thisArg) {
        for (const field of getOrCreateInnerNode(this).boxedIterator()) {
            const node = getTreeNodeForField(field);
            callbackFn.call(thisArg, node, field.key, this);
        }
    }
}
CustomMapNodeBase.kind = 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).
 */
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function mapSchema(identifier, info, implicitlyConstructable, useMapPrototype) {
    let flexSchema;
    let unhydratedContext;
    class Schema extends CustomMapNodeBase {
        static prepareInstance(instance, flexNode) {
            if (useMapPrototype) {
                return new Proxy(instance, handler);
            }
            return instance;
        }
        static buildRawNode(instance, input) {
            return getOrCreateMapTreeNode(unhydratedContext, flexSchema, mapTreeFromNodeData(input, this));
        }
        static oneTimeSetup() {
            const schema = this;
            flexSchema = getFlexSchema(schema);
            unhydratedContext = new UnhydratedContext(toFlexSchema(schema));
        }
        // eslint-disable-next-line import/no-deprecated
        get [typeNameSymbol]() {
            return identifier;
        }
        get [typeSchemaSymbol]() {
            return Schema.constructorCached?.constructor;
        }
    }
    Schema.constructorCached = undefined;
    Schema.identifier = identifier;
    Schema.info = info;
    Schema.implicitlyConstructable = implicitlyConstructable;
    const schemaErased = Schema;
    return schemaErased;
}
//# sourceMappingURL=mapNode.js.map