"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 index_js_1 = require("../feature-libraries/index.js");
const proxies_js_1 = require("./proxies.js");
const proxyBinding_js_1 = require("./proxyBinding.js");
const schemaCaching_js_1 = require("./schemaCaching.js");
const schemaTypes_js_1 = require("./schemaTypes.js");
const toMapTree_js_1 = require("./toMapTree.js");
const types_js_1 = require("./types.js");
const toFlexSchema_js_1 = require("./toFlexSchema.js");
const internal_1 = require("@fluidframework/telemetry-utils/internal");
const handler = {
    getPrototypeOf: () => {
        return Map.prototype;
    },
};
class CustomMapNodeBase extends types_js_1.TreeNodeValid {
    [Symbol.iterator]() {
        return this.entries();
    }
    delete(key) {
        const node = (0, proxyBinding_js_1.getFlexNode)(this);
        if ((0, index_js_1.isMapTreeNode)(node)) {
            throw new internal_1.UsageError(`A map cannot be mutated before being inserted into the tree`);
        }
        node.delete(key);
    }
    *entries() {
        const node = (0, proxyBinding_js_1.getFlexNode)(this);
        for (const key of node.keys()) {
            yield [key, (0, proxies_js_1.getProxyForField)(node.getBoxed(key))];
        }
    }
    get(key) {
        const node = (0, proxyBinding_js_1.getFlexNode)(this);
        const field = node.getBoxed(key);
        return (0, proxies_js_1.getProxyForField)(field);
    }
    has(key) {
        const node = (0, proxyBinding_js_1.getFlexNode)(this);
        return node.has(key);
    }
    keys() {
        const node = (0, proxyBinding_js_1.getFlexNode)(this);
        return node.keys();
    }
    set(key, value) {
        const node = (0, proxyBinding_js_1.getFlexNode)(this);
        if ((0, index_js_1.isMapTreeNode)(node)) {
            throw new internal_1.UsageError(`A map cannot be mutated before being inserted into the tree`);
        }
        const classSchema = (0, schemaCaching_js_1.getSimpleNodeSchema)(node.schema);
        const mapTree = (0, toMapTree_js_1.mapTreeFromNodeData)(value, classSchema.info, node.context.nodeKeyManager, (0, index_js_1.getSchemaAndPolicy)(node));
        (0, proxies_js_1.prepareContentForHydration)(mapTree, node.context.checkout.forest);
        const cursor = mapTree !== undefined ? (0, index_js_1.cursorForMapTreeNode)(mapTree) : undefined;
        node.set(key, cursor);
        return this;
    }
    get size() {
        return (0, proxyBinding_js_1.getFlexNode)(this).size;
    }
    *values() {
        for (const [, value] of this.entries()) {
            yield value;
        }
    }
    forEach(callbackFn, thisArg) {
        for (const field of (0, proxyBinding_js_1.getFlexNode)(this).boxedIterator()) {
            const node = (0, proxies_js_1.getProxyForField)(field);
            callbackFn.call(thisArg, node, field.key, this);
        }
    }
}
CustomMapNodeBase.kind = schemaTypes_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).
 */
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
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 (0, index_js_1.getOrCreateMapTreeNode)(flexSchema, (0, toMapTree_js_1.mapTreeFromNodeData)(copyContent(flexSchema.name, input), this));
        }
        static oneTimeSetup() {
            flexSchema = (0, toFlexSchema_js_1.getFlexSchema)(this);
        }
        get [schemaTypes_js_1.typeNameSymbol]() {
            return identifier;
        }
    }
    schema.constructorCached = undefined;
    schema.identifier = identifier;
    schema.info = info;
    schema.implicitlyConstructable = implicitlyConstructable;
    const schemaErased = schema;
    return schemaErased;
}
exports.mapSchema = mapSchema;
function copyContent(typeName, content) {
    const copy = new Map(content);
    (0, proxies_js_1.markContentType)(typeName, copy);
    return copy;
}
//# sourceMappingURL=mapNode.js.map