/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { getProxyForField, markContentType, prepareContentForInsert, } from "./proxies.js";
import { getFlexNode } from "./proxyBinding.js";
import { getSimpleNodeSchema } from "./schemaCaching.js";
import { NodeKind, type, } from "./schemaTypes.js";
import { cursorFromNodeData } from "./toMapTree.js";
import { TreeNodeValid } from "./types.js";
import { getFlexSchema } from "./toFlexSchema.js";
import { RawTreeNode, nodeContent, rawError } from "./rawNode.js";
const handler = {
    getPrototypeOf: () => {
        return Map.prototype;
    },
};
class CustomMapNodeBase extends TreeNodeValid {
    [Symbol.iterator]() {
        return this.entries();
    }
    delete(key) {
        const node = getFlexNode(this);
        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);
        const content = prepareContentForInsert(value, node.context.checkout.forest);
        const classSchema = getSimpleNodeSchema(node.schema);
        const cursor = cursorFromNodeData(content, classSchema.info, node.context.nodeKeyManager);
        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).
 */
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 new RawMapNode(flexSchema, copyContent(flexSchema.name, input));
        }
        static oneTimeSetup() {
            flexSchema = getFlexSchema(this);
        }
        get [type]() {
            return identifier;
        }
    }
    schema.constructorCached = undefined;
    schema.identifier = identifier;
    schema.info = info;
    schema.implicitlyConstructable = implicitlyConstructable;
    const schemaErased = schema;
    return schemaErased;
}
/**
 * The implementation of a map node created by {@link createRawNode}.
 */
export class RawMapNode extends RawTreeNode {
    get size() {
        return this[nodeContent].size;
    }
    has(key) {
        return this[nodeContent].has(key);
    }
    get(key) {
        return this[nodeContent].get(key);
    }
    keys() {
        return this[nodeContent].keys();
    }
    values() {
        throw rawError("Iterating map values");
    }
    entries() {
        throw rawError("Iterating map entries");
    }
    forEach(callbackFn, thisArg) {
        throw rawError("Iterating maps with forEach");
    }
    set(key, value) {
        throw rawError("Setting a map entry");
    }
    delete(key) {
        throw rawError("Deleting a map entry");
    }
    get asObject() {
        throw rawError("Converting a map to an object");
    }
    [Symbol.iterator]() {
        return this.entries();
    }
    boxedIterator() {
        throw rawError("Boxed iteration");
    }
}
function copyContent(typeName, content) {
    const copy = new Map(content);
    markContentType(typeName, copy);
    return copy;
}
//# sourceMappingURL=mapNode.js.map