"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyCatalogStructureMigration = applyCatalogStructureMigration;
const get_with_default_1 = require("../../objects/get-with-default");
const upgradeCatalogSchema_1 = require("../../search/upgradeCatalogSchema");
const util_1 = require("../../util");
const get_prop_1 = require("../../objects/get-prop");
/**
 * Add the default catalog structure to the Site model
 * @param model
 * @returns
 */
function applyCatalogStructureMigration(model) {
    const siteCatalog = (0, get_with_default_1.getWithDefault)(model, "data.catalog", {});
    // This _shouldn't_ happen, but some of our testing sites might have this
    // migration already persisted in AGO. In that case, we ignore and move on
    if (!siteCatalog.schemaVersion) {
        const clonedModel = (0, util_1.cloneObject)(model);
        clonedModel.data.catalog = (0, upgradeCatalogSchema_1.upgradeCatalogSchema)(siteCatalog);
        const upgradedCatalog = clonedModel.data.catalog;
        // The umbrella site needs a special predicate added to the catalog to ensure
        // that its item scope only includes results shared to open data groups.
        // We also don't want to include the groups from the site model
        if ((0, get_prop_1.getProp)(clonedModel, "data.values.isUmbrella")) {
            upgradedCatalog.scopes.item.filters = [
                {
                    predicates: [{ openData: true }],
                },
            ];
            // The umbrella site doesn't have any other scopes defined, so remove any
            // others that might have been added by the migration
            upgradedCatalog.scopes = { item: upgradedCatalog.scopes.item };
        }
        // applyCatalogSchema sets the catalog to `Default Catalog` but this fn previously
        // set it to `Default Site Catalog`. Overriding title to `Default Site Catalog` here
        // to prevent any potential regressions
        upgradedCatalog.title = "Default Site Catalog";
        return clonedModel;
    }
    return model;
}
//# sourceMappingURL=applyCatalogStructureMigration.js.map