/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { SimpleDependee } from "../dependency-tracking";
import { createEmitter } from "../../events";
import { storedEmptyFieldSchema, } from "./schema";
/**
 * StoredSchemaRepository for in memory use:
 * not hooked up to Fluid (does not create Fluid ops when editing).
 */
export class InMemoryStoredSchemaRepository extends SimpleDependee {
    /**
     * For now, the schema are just scored in maps.
     * There are a couple reasons we might not want this simple solution long term:
     * 1. We might want an easy/fast copy.
     * 2. We might want a way to reserve a large namespace of schema with the same schema.
     * The way mapFields has been structured mitigates the need for this, but it still might be useful.
     *
     * (ex: someone using data as field identifiers might want to
     * reserve all fields identifiers starting with "foo." to have a specific schema).
     * Combined with support for such namespaces in the allowed sets in the schema objects,
     * that might provide a decent alternative to mapFields (which is a bit odd).
     */
    constructor(data) {
        super("StoredSchemaRepository");
        this.events = createEmitter();
        this.data = cloneSchemaData(data ?? defaultSchemaData);
    }
    on(eventName, listener) {
        return this.events.on(eventName, listener);
    }
    get rootFieldSchema() {
        return this.data.rootFieldSchema;
    }
    get nodeSchema() {
        return this.data.nodeSchema;
    }
    update(newSchema) {
        this.events.emit("beforeSchemaChange", newSchema);
        this.data.rootFieldSchema = newSchema.rootFieldSchema;
        this.data.nodeSchema.clear();
        for (const [name, schema] of newSchema.nodeSchema) {
            this.data.nodeSchema.set(name, schema);
        }
        this.invalidateDependents();
        this.events.emit("afterSchemaChange", newSchema);
    }
}
export function schemaDataIsEmpty(data) {
    return data.nodeSchema.size === 0;
}
export const defaultSchemaData = {
    nodeSchema: new Map(),
    rootFieldSchema: storedEmptyFieldSchema,
};
export function cloneSchemaData(data) {
    return {
        nodeSchema: new Map(data?.nodeSchema ?? []),
        rootFieldSchema: data?.rootFieldSchema ?? storedEmptyFieldSchema,
    };
}
//# sourceMappingURL=storedSchemaRepository.js.map