/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { UsageError } from "@fluidframework/telemetry-utils/internal";
import { cursorForMapTreeNode, FieldKinds, getOrCreateMapTreeNode, getSchemaAndPolicy, isMapTreeNode, } from "../feature-libraries/index.js";
import { getProxyForField, markContentType, prepareContentForHydration, } from "./proxies.js";
import { getFlexNode } from "./proxyBinding.js";
import { NodeKind, getStoredKey, getExplicitStoredKey, normalizeFieldSchema, typeNameSymbol, FieldKind, } from "./schemaTypes.js";
import { mapTreeFromNodeData } from "./toMapTree.js";
import { TreeNodeValid } from "./types.js";
import { fail } from "../util/index.js";
import { getFlexSchema } from "./toFlexSchema.js";
/**
 * Caches the mappings from view keys to stored keys for the provided object field schemas in {@link simpleKeyToFlexKeyCache}.
 */
function createFlexKeyMapping(fields) {
    const keyMap = new Map();
    for (const [viewKey, fieldSchema] of Object.entries(fields)) {
        const storedKey = getStoredKey(viewKey, fieldSchema);
        keyMap.set(viewKey, { storedKey, schema: normalizeFieldSchema(fieldSchema) });
    }
    return keyMap;
}
/**
 * @param allowAdditionalProperties - If true, setting of unexpected properties will be forwarded to the target object.
 * Otherwise setting of unexpected properties will error.
 * TODO: consider implementing this using `Object.preventExtension` instead.
 * @param customTargetObject - Target object of the proxy.
 * If not provided `{}` is used for the target.
 */
function createProxyHandler(flexKeyMap, allowAdditionalProperties) {
    // To satisfy 'deepEquals' level scrutiny, the target of the proxy must be an object with the same
    // prototype as an object literal '{}'.  This is because 'deepEquals' uses 'Object.getPrototypeOf'
    // as a way to quickly reject objects with different prototype chains.
    //
    // (Note that the prototype of an object literal appears as '[Object: null prototype] {}', not because
    // the prototype is null, but because the prototype object itself has a null prototype.)
    // TODO: Although the target is an object literal, it's still worthwhile to try experimenting with
    // a dispatch object to see if it improves performance.
    const handler = {
        get(target, viewKey, proxy) {
            const fieldInfo = flexKeyMap.get(viewKey);
            if (fieldInfo !== undefined) {
                const flexNode = getFlexNode(proxy);
                const field = flexNode.tryGetField(fieldInfo.storedKey);
                if (field !== undefined) {
                    return getProxyForField(field);
                }
                // Check if the user is trying to read an identifier field of an unhydrated node, but the identifier is not present.
                // This means the identifier is an "auto-generated identifier", because otherwise it would have been supplied by the user at construction time and would have been successfully read just above.
                // In this case, it is categorically impossible to provide an identifier (auto-generated identifiers can't be created until hydration/insertion time), so we emit an error.
                if (fieldInfo.schema.kind === FieldKind.Identifier && isMapTreeNode(flexNode)) {
                    throw new UsageError("An automatically generated node identifier may not be queried until the node is inserted into the tree");
                }
                return undefined;
            }
            // Pass the proxy as the receiver here, so that any methods on the prototype receive `proxy` as `this`.
            return Reflect.get(target, viewKey, proxy);
        },
        set(target, viewKey, value, proxy) {
            const fieldInfo = flexKeyMap.get(viewKey);
            if (fieldInfo === undefined) {
                return allowAdditionalProperties ? Reflect.set(target, viewKey, value) : false;
            }
            const flexNode = getFlexNode(proxy);
            if (isMapTreeNode(flexNode)) {
                throw new UsageError(`An object cannot be mutated before being inserted into the tree`);
            }
            setField(flexNode.getBoxed(fieldInfo.storedKey), fieldInfo.schema, value);
            return true;
        },
        has: (target, viewKey) => {
            return (flexKeyMap.has(viewKey) ||
                (allowAdditionalProperties ? Reflect.has(target, viewKey) : false));
        },
        ownKeys: (target) => {
            return [
                ...flexKeyMap.keys(),
                ...(allowAdditionalProperties ? Reflect.ownKeys(target) : []),
            ];
        },
        getOwnPropertyDescriptor: (target, viewKey) => {
            const fieldInfo = flexKeyMap.get(viewKey);
            if (fieldInfo === undefined) {
                return allowAdditionalProperties
                    ? Reflect.getOwnPropertyDescriptor(target, viewKey)
                    : undefined;
            }
            // For some reason, the getOwnPropertyDescriptor is not passed in the receiver, so use a weak map.
            // If a refactoring is done to associated flex tree data with the target not the proxy, this extra map could be removed,
            // and the design would be more compatible with proxyless nodes.
            const proxy = targetToProxy.get(target) ?? fail("missing proxy");
            const field = getFlexNode(proxy).tryGetField(fieldInfo.storedKey);
            const p = {
                value: field === undefined ? undefined : getProxyForField(field),
                writable: true,
                // Report empty fields as own properties so they shadow inherited properties (even when empty) to match TypeScript typing.
                // Make empty fields not enumerable so they get skipped when iterating over an object to better align with
                // JSON and deep equals with JSON compatible object (which can't have undefined fields).
                enumerable: field !== undefined,
                configurable: true, // Must be 'configurable' if property is absent from proxy target.
            };
            return p;
        },
    };
    return handler;
}
export function setField(field, simpleFieldSchema, value) {
    switch (field.schema.kind) {
        case FieldKinds.required:
        case FieldKinds.optional: {
            const typedField = field;
            const mapTree = mapTreeFromNodeData(value, simpleFieldSchema.allowedTypes, field.context.nodeKeyManager, getSchemaAndPolicy(field));
            prepareContentForHydration(mapTree, field.context.checkout.forest);
            typedField.content = mapTree !== undefined ? cursorForMapTreeNode(mapTree) : undefined;
            break;
        }
        default:
            fail("invalid FieldKind");
    }
}
class CustomObjectNodeBase extends TreeNodeValid {
}
CustomObjectNodeBase.kind = NodeKind.Object;
/**
 * Define a {@link TreeNodeSchema} for a {@link TreeObjectNode}.
 *
 * @param name - Unique identifier for this schema within this factory's scope.
 * @param fields - Schema for fields of the object node's schema. Defines what children can be placed under each key.
 */
export function objectSchema(identifier, info, implicitlyConstructable) {
    // Ensure no collisions between final set of view keys, and final set of stored keys (including those
    // implicitly derived from view keys)
    assertUniqueKeys(identifier, info);
    // Performance optimization: cache view key => stored key and schema.
    const flexKeyMap = createFlexKeyMapping(info);
    let handler;
    let customizable;
    let flexSchema;
    class CustomObjectNode extends CustomObjectNodeBase {
        static prepareInstance(instance, flexNode) {
            // Differentiate between the following cases:
            //
            // Case 1: Direct construction (POJO emulation)
            //
            //     const Foo = schemaFactory.object("Foo", {bar: schemaFactory.number});
            //
            //     assert.deepEqual(new Foo({ bar: 42 }), { bar: 42 },
            //		   "Prototype chain equivalent to POJO.");
            //
            // Case 2: Subclass construction (Customizable Object)
            //
            // 	   class Foo extends schemaFactory.object("Foo", {bar: schemaFactory.number}) {}
            //
            // 	   assert.notDeepEqual(new Foo({ bar: 42 }), { bar: 42 },
            // 	       "Subclass prototype chain differs from POJO.");
            //
            // In Case 1 (POJO emulation), the prototype chain match '{}' (proxyTarget = undefined)
            // In Case 2 (Customizable Object), the prototype chain include the user's subclass (proxyTarget = this)
            const proxyTarget = customizable ? instance : {};
            const proxy = new Proxy(proxyTarget, handler);
            targetToProxy.set(proxyTarget, proxy);
            return proxy;
        }
        static buildRawNode(instance, input) {
            return getOrCreateMapTreeNode(flexSchema, mapTreeFromNodeData(copyContent(flexSchema.name, input), this));
        }
        static oneTimeSetup() {
            // One time initialization that required knowing the most derived type (from this.constructor) and thus has to be lazy.
            customizable = this !== CustomObjectNode;
            handler = createProxyHandler(flexKeyMap, customizable);
            flexSchema = getFlexSchema(this);
            // First run, do extra validation.
            // TODO: provide a way for TreeConfiguration to trigger this same validation to ensure it gets run early.
            // Scan for shadowing inherited members which won't work, but stop scan early to allow shadowing built in (which seems to work ok).
            {
                let prototype = this.prototype;
                // There isn't a clear cleaner way to author this loop.
                while (prototype !== CustomObjectNode.prototype) {
                    for (const [key] of flexKeyMap) {
                        if (
                        // constructor is a special case, since one is built in on the derived type, and shadowing it works fine since we only use it before fields are applied.
                        key !== "constructor" &&
                            Reflect.getOwnPropertyDescriptor(prototype, key) !== undefined) {
                            throw new UsageError(`Schema ${identifier} defines an inherited property "${key.toString()}" which shadows a field. Since fields are exposed as own properties, this shadowing will not work, and is an error.`);
                        }
                    }
                    // Since this stops at CustomObjectNode, it should never see a null prototype, so this case is safe.
                    // Additionally, if the prototype chain is ever messed up such that CustomObjectNode is not in it,
                    // the null that would show up here does at least ensure this code throws instead of hanging.
                    prototype = Reflect.getPrototypeOf(prototype);
                }
            }
        }
        get [typeNameSymbol]() {
            return identifier;
        }
    }
    CustomObjectNode.fields = new Map([...flexKeyMap].map(([key, value]) => [key, value.schema]));
    CustomObjectNode.constructorCached = undefined;
    CustomObjectNode.identifier = identifier;
    CustomObjectNode.info = info;
    CustomObjectNode.implicitlyConstructable = implicitlyConstructable;
    return CustomObjectNode;
}
const targetToProxy = new WeakMap();
/**
 * Ensures that the set of view keys in the schema is unique.
 * Also ensure that the final set of stored keys (including those implicitly derived from view keys) is unique.
 * @throws Throws a `UsageError` if either of the key uniqueness invariants is violated.
 */
function assertUniqueKeys(schemaName, fields) {
    // Verify that there are no duplicates among the explicitly specified stored keys.
    const explicitStoredKeys = new Set();
    for (const schema of Object.values(fields)) {
        const storedKey = getExplicitStoredKey(schema);
        if (storedKey === undefined) {
            continue;
        }
        if (explicitStoredKeys.has(storedKey)) {
            throw new UsageError(`Duplicate stored key "${storedKey}" in schema "${schemaName}". Stored keys must be unique within an object schema.`);
        }
        explicitStoredKeys.add(storedKey);
    }
    // Verify that there are no duplicates among the derived
    // (including those implicitly derived from view keys) stored keys.
    const derivedStoredKeys = new Set();
    for (const [viewKey, schema] of Object.entries(fields)) {
        const storedKey = getStoredKey(viewKey, schema);
        if (derivedStoredKeys.has(storedKey)) {
            throw new UsageError(`Stored key "${storedKey}" in schema "${schemaName}" conflicts with a property key of the same name, which is not overridden by a stored key. The final set of stored keys in an object schema must be unique.`);
        }
        derivedStoredKeys.add(storedKey);
    }
}
function copyContent(typeName, content) {
    const copy = { ...content };
    markContentType(typeName, copy);
    return copy;
}
export const ObjectNodeSchema = {
    // instanceof-based narrowing support for Javascript and TypeScript 5.3 or newer.
    [Symbol.hasInstance](value) {
        return isObjectNodeSchema(value);
    },
};
export function isObjectNodeSchema(schema) {
    return schema.kind === NodeKind.Object;
}
//# sourceMappingURL=objectNode.js.map