"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createOrUpdateEntitySettings = createOrUpdateEntitySettings;
const getTypeFromEntity_1 = require("../getTypeFromEntity");
const getDefaultEntitySettings_1 = require("../../discussions/api/settings/getDefaultEntitySettings");
const settings_1 = require("../../discussions/api/settings/settings");
/**
 * Creates or updates the entity settings for an entity. Currently this how the following are configured/persisted:
 *   1. allowed participation channels
 *   2. allowed locations (geometries representing the region(s) on the map where users can draw custom geometries to attach to their posts/replies)
 * @param entity An IHubEntity that implements discussions-related traits
 * @param requestOptions An IHubRequestOptions object
 * @param allowedLocations An optional Polygon[] representing the allowed locations for discussion post/reply geometries, defaults to null
 * @returns a promise that resolves an IEntitySetting object
 */
async function createOrUpdateEntitySettings(entity, requestOptions, allowedLocations = null) {
    var _a, _b;
    // create or update entity settings
    const entityType = (0, getTypeFromEntity_1.getTypeFromEntity)(entity);
    const defaultSettings = (0, getDefaultEntitySettings_1.getDefaultEntitySettings)(entityType);
    const settings = Object.assign(Object.assign({}, defaultSettings.settings), { discussions: Object.assign(Object.assign(Object.assign({}, defaultSettings.settings.discussions), entity.discussionSettings), { allowedLocations }) });
    if (!((_a = settings.discussions.allowedChannelIds) === null || _a === void 0 ? void 0 : _a.length)) {
        settings.discussions.allowedChannelIds = null;
    }
    if (!((_b = settings.discussions.allowedLocations) === null || _b === void 0 ? void 0 : _b.length)) {
        settings.discussions.allowedLocations = null;
    }
    const newOrUpdatedSettings = entity.entitySettingsId
        ? await (0, settings_1.updateSettingV2)(Object.assign({ id: entity.entitySettingsId, data: {
                settings,
            } }, requestOptions))
        : await (0, settings_1.createSettingV2)(Object.assign({ data: {
                id: entity.id,
                type: defaultSettings.type,
                settings,
            } }, requestOptions));
    return newOrUpdatedSettings;
}
//# sourceMappingURL=createOrUpdateEntitySettings.js.map