"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.schemaDataIsEmpty = exports.InMemoryStoredSchemaRepository = void 0;
const dependency_tracking_1 = require("../dependency-tracking");
const events_1 = require("../../events");
const schema_1 = require("./schema");
/**
 * StoredSchemaRepository for in memory use:
 * not hooked up to Fluid (does not create Fluid ops when editing).
 */
class InMemoryStoredSchemaRepository extends dependency_tracking_1.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(policy, data) {
        super("StoredSchemaRepository");
        this.policy = policy;
        this.events = (0, events_1.createEmitter)();
        this.data = {
            treeSchema: new Map(data?.treeSchema ?? []),
            rootFieldSchema: data?.rootFieldSchema ?? schema_1.storedEmptyFieldSchema,
        };
    }
    on(eventName, listener) {
        return this.events.on(eventName, listener);
    }
    clone() {
        return new InMemoryStoredSchemaRepository(this.policy, this.data);
    }
    get rootFieldSchema() {
        return this.data.rootFieldSchema;
    }
    get treeSchema() {
        return this.data.treeSchema;
    }
    update(newSchema) {
        this.events.emit("beforeSchemaChange", newSchema);
        this.data.rootFieldSchema = newSchema.rootFieldSchema;
        this.data.treeSchema.clear();
        for (const [name, schema] of newSchema.treeSchema) {
            this.data.treeSchema.set(name, schema);
        }
        this.invalidateDependents();
        this.events.emit("afterSchemaChange", newSchema);
    }
}
exports.InMemoryStoredSchemaRepository = InMemoryStoredSchemaRepository;
function schemaDataIsEmpty(data) {
    return data.treeSchema.size === 0;
}
exports.schemaDataIsEmpty = schemaDataIsEmpty;
//# sourceMappingURL=storedSchemaRepository.js.map