/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { cursorForMapTreeNode, getOrCreateMapTreeNode, getSchemaAndPolicy, isMapTreeNode, } from "../feature-libraries/index.js";
import { getProxyForField, markContentType, prepareContentForHydration, } from "./proxies.js";
import { getFlexNode } from "./proxyBinding.js";
import { getSimpleNodeSchema } from "./schemaCaching.js";
import { NodeKind, typeNameSymbol, } from "./schemaTypes.js";
import { mapTreeFromNodeData } from "./toMapTree.js";
import { TreeNodeValid } from "./types.js";
import { getFlexSchema } from "./toFlexSchema.js";
import { UsageError } from "@fluidframework/telemetry-utils/internal";
const handler = {
    getPrototypeOf: () => {
        return Map.prototype;
    },
};
class CustomMapNodeBase extends TreeNodeValid {
    [Symbol.iterator]() {
        return this.entries();
    }
    delete(key) {
        const node = getFlexNode(this);
        if (isMapTreeNode(node)) {
            throw new UsageError(`A map cannot be mutated before being inserted into the tree`);
        }
        node.delete(key);
    }
    *entries() {
        const node = getFlexNode(this);
        for (const key of node.keys()) {
            yield [key, getProxyForField(node.getBoxed(key))];
        }
    }
    get(key) {
        const node = getFlexNode(this);
        const field = node.getBoxed(key);
        return getProxyForField(field);
    }
    has(key) {
        const node = getFlexNode(this);
        return node.has(key);
    }
    keys() {
        const node = getFlexNode(this);
        return node.keys();
    }
    set(key, value) {
        const node = getFlexNode(this);
        if (isMapTreeNode(node)) {
            throw new UsageError(`A map cannot be mutated before being inserted into the tree`);
        }
        const classSchema = getSimpleNodeSchema(node.schema);
        const mapTree = mapTreeFromNodeData(value, classSchema.info, node.context.nodeKeyManager, getSchemaAndPolicy(node));
        prepareContentForHydration(mapTree, node.context.checkout.forest);
        const cursor = mapTree !== undefined ? cursorForMapTreeNode(mapTree) : undefined;
        node.set(key, cursor);
        return this;
    }
    get size() {
        return getFlexNode(this).size;
    }
    *values() {
        for (const [, value] of this.entries()) {
            yield value;
        }
    }
    forEach(callbackFn, thisArg) {
        for (const field of getFlexNode(this).boxedIterator()) {
            const node = getProxyForField(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;
    class schema extends CustomMapNodeBase {
        static prepareInstance(instance, flexNode) {
            if (useMapPrototype) {
                return new Proxy(instance, handler);
            }
            return instance;
        }
        static buildRawNode(instance, input) {
            return getOrCreateMapTreeNode(flexSchema, mapTreeFromNodeData(copyContent(flexSchema.name, input), this));
        }
        static oneTimeSetup() {
            flexSchema = getFlexSchema(this);
        }
        get [typeNameSymbol]() {
            return identifier;
        }
    }
    schema.constructorCached = undefined;
    schema.identifier = identifier;
    schema.info = info;
    schema.implicitlyConstructable = implicitlyConstructable;
    const schemaErased = schema;
    return schemaErased;
}
function copyContent(typeName, content) {
    const copy = new Map(content);
    markContentType(typeName, copy);
    return copy;
}
//# sourceMappingURL=mapNode.js.map