"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.initCatalogOnEntityCreate = void 0;
const getWellKnownGroup_1 = require("../groups/getWellKnownGroup");
const HubGroups_1 = require("../groups/HubGroups");
const upgradeCatalogSchema_1 = require("./upgradeCatalogSchema");
/**
 * Initializes an returns a catalog to persist on Hub item
 * entity upon creation based on user-configured options.
 *
 * @param entity - Hub item entity
 * @param catalogSetup - options for initializing the catalog
 * @param context - contextual portal & auth information
 */
const initCatalogOnEntityCreate = async (entity, catalogSetup, context) => {
    var _a;
    // If a user elects to initialize the entity with an
    // existing group, we grab the id of the group they've
    // selected.
    let groupId = (_a = catalogSetup.groupId) === null || _a === void 0 ? void 0 : _a[0];
    // If a user elects to initialize the catalog with a
    // new group, we create the new group
    if (catalogSetup.type === "newGroup") {
        const partialGroup = Object.assign(Object.assign({}, (0, getWellKnownGroup_1.getWellKnownGroup)("hubViewGroup", context)), { name: `${entity.name} Content` });
        const group = await (0, HubGroups_1.createHubGroup)(partialGroup, context);
        groupId = group.id;
    }
    return {
        schemaVersion: upgradeCatalogSchema_1.CATALOG_SCHEMA_VERSION,
        scopes: {
            item: {
                targetEntity: "item",
                filters: [{ predicates: [{ group: [groupId] }] }],
            },
            event: {
                targetEntity: "event",
                filters: [{ predicates: [{ group: [groupId] }] }],
            },
        },
        collections: [],
    };
};
exports.initCatalogOnEntityCreate = initCatalogOnEntityCreate;
//# sourceMappingURL=initCatalogOnEntityCreate.js.map