"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.lookupTreeSchema = exports.lookupGlobalFieldSchema = exports.InMemoryStoredSchemaRepository = void 0;
const dependency_tracking_1 = require("../dependency-tracking");
const events_1 = require("../../events");
/**
 * 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 extraFields 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 extraFields (which is a bit odd).
     */
    constructor(policy, data) {
        var _a, _b;
        super("StoredSchemaRepository");
        this.policy = policy;
        this.events = (0, events_1.createEmitter)();
        this.data = {
            treeSchema: new Map((_a = data === null || data === void 0 ? void 0 : data.treeSchema) !== null && _a !== void 0 ? _a : []),
            globalFieldSchema: new Map((_b = data === null || data === void 0 ? void 0 : data.globalFieldSchema) !== null && _b !== void 0 ? _b : []),
        };
    }
    on(eventName, listener) {
        return this.events.on(eventName, listener);
    }
    clone() {
        return new InMemoryStoredSchemaRepository(this.policy, this.data);
    }
    get globalFieldSchema() {
        return this.data.globalFieldSchema;
    }
    get treeSchema() {
        return this.data.treeSchema;
    }
    update(newSchema) {
        this.events.emit("beforeSchemaChange", newSchema);
        for (const [name, schema] of newSchema.globalFieldSchema) {
            this.data.globalFieldSchema.set(name, schema);
        }
        for (const [name, schema] of newSchema.treeSchema) {
            this.data.treeSchema.set(name, schema);
        }
        this.invalidateDependents();
        this.events.emit("afterSchemaChange", newSchema);
    }
}
exports.InMemoryStoredSchemaRepository = InMemoryStoredSchemaRepository;
function lookupGlobalFieldSchema(data, identifier) {
    var _a;
    return (_a = data.globalFieldSchema.get(identifier)) !== null && _a !== void 0 ? _a : data.policy.defaultGlobalFieldSchema;
}
exports.lookupGlobalFieldSchema = lookupGlobalFieldSchema;
function lookupTreeSchema(data, identifier) {
    var _a;
    return (_a = data.treeSchema.get(identifier)) !== null && _a !== void 0 ? _a : data.policy.defaultTreeSchema;
}
exports.lookupTreeSchema = lookupTreeSchema;
function schemaDataIsEmpty(data) {
    return data.treeSchema.size === 0 && data.globalFieldSchema.size === 0;
}
exports.schemaDataIsEmpty = schemaDataIsEmpty;
//# sourceMappingURL=storedSchemaRepository.js.map