"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createHubGroup = createHubGroup;
exports.fetchHubGroup = fetchHubGroup;
exports.updateHubGroup = updateHubGroup;
exports.deleteHubGroup = deleteHubGroup;
const arcgis_rest_portal_1 = require("@esri/arcgis-rest-portal");
const defaults_1 = require("./defaults");
const convertHubGroupToGroup_1 = require("./_internal/convertHubGroupToGroup");
const convertGroupToHubGroup_1 = require("./_internal/convertGroupToHubGroup");
const getUniqueGroupTitle_1 = require("./_internal/getUniqueGroupTitle");
const createOrUpdateEntitySettings_1 = require("../core/_internal/createOrUpdateEntitySettings");
const settings_1 = require("../discussions/api/settings/settings");
const utils_1 = require("../discussions/utils");
const set_prop_1 = require("../objects/set-prop");
const getDefaultEntitySettings_1 = require("../discussions/api/settings/getDefaultEntitySettings");
/**
 * Create a new Hub Group
 * we are creating an IGroup with the createGroup call
 * so we need to convert the Hub Group to IGroup first
 * then convert it back to Hub Group and return it
 * @param partialGroup
 * @param requestOptions
 */
async function createHubGroup(partialGroup, context) {
    // merge the incoming and default groups
    const hubGroup = Object.assign(Object.assign({}, defaults_1.DEFAULT_GROUP), partialGroup);
    // ensure the group has a unique title
    const uniqueTitle = await (0, getUniqueGroupTitle_1.getUniqueGroupTitle)(hubGroup.name, context.userRequestOptions);
    hubGroup.name = uniqueTitle;
    hubGroup.typeKeywords = (0, utils_1.setDiscussableKeyword)(hubGroup.typeKeywords, hubGroup.isDiscussable);
    const group = (0, convertHubGroupToGroup_1.convertHubGroupToGroup)(hubGroup);
    const opts = {
        group,
        authentication: context.userRequestOptions.authentication,
    };
    const result = await (0, arcgis_rest_portal_1.createGroup)(opts);
    // createGroup does not set a protection value based on the value of 'protected'
    // so we have to make an additional call to protectGroup to set protection
    if (group.protected) {
        result.group.protected = (await (0, arcgis_rest_portal_1.protectGroup)({
            id: result.group.id,
            authentication: context.userRequestOptions.authentication,
        })).success;
    }
    const entity = (0, convertGroupToHubGroup_1.convertGroupToHubGroup)(result.group, null, context.userRequestOptions);
    // create or update entity settings
    if (!context.isPortal) {
        const entitySetting = await (0, createOrUpdateEntitySettings_1.createOrUpdateEntitySettings)(Object.assign(Object.assign({}, hubGroup), { id: entity.id }), context.hubRequestOptions);
        entity.entitySettingsId = entitySetting.id;
        entity.discussionSettings = entitySetting.settings.discussions;
    }
    return entity;
}
/**
 * Get a Hub Group by id
 * we need to convert the IGroup we get to Hub Group
 * @param identifier
 * @param requestOptions
 */
async function fetchHubGroup(identifier, requestOptions) {
    const group = await (0, arcgis_rest_portal_1.getGroup)(identifier, requestOptions);
    let entitySettings;
    if (!requestOptions.isPortal) {
        entitySettings = await (0, settings_1.fetchSettingV2)(Object.assign({ id: group.id }, requestOptions)).catch(() => (Object.assign({ id: null }, (0, getDefaultEntitySettings_1.getDefaultEntitySettings)("group"))));
    }
    else {
        entitySettings = Object.assign({ id: null }, (0, getDefaultEntitySettings_1.getDefaultEntitySettings)("group"));
    }
    const hubGroup = (0, convertGroupToHubGroup_1.convertGroupToHubGroup)(group, entitySettings, requestOptions);
    return hubGroup;
}
/**
 * @private
 * Update a Hub Group and return it
 * we need to convert the incoming Hub Group to IGroup
 * before sending it to the API
 * @param hubGroup
 * @param requestOptions
 */
async function updateHubGroup(hubGroup, context) {
    // TODO: fetch the upstream group and convert to a HubGroup so we can compare props
    hubGroup.typeKeywords = (0, utils_1.setDiscussableKeyword)(hubGroup.typeKeywords, hubGroup.isDiscussable);
    const group = (0, convertHubGroupToGroup_1.convertHubGroupToGroup)(hubGroup);
    const opts = {
        group,
        authentication: context.requestOptions.authentication,
    };
    // if we have a field we are trying to clear
    // We need to send clearEmptyFields: true to the updateGroup call
    if (group._clearEmptyFields) {
        (0, set_prop_1.setProp)("params.clearEmptyFields", true, opts);
    }
    await (0, arcgis_rest_portal_1.updateGroup)(opts);
    // create or update entity settings
    if (!context.isPortal) {
        const entitySetting = await (0, createOrUpdateEntitySettings_1.createOrUpdateEntitySettings)(hubGroup, context.hubRequestOptions);
        hubGroup.entitySettingsId = entitySetting.id;
        hubGroup.discussionSettings = entitySetting.settings.discussions;
    }
    return hubGroup;
}
/**
 * @private
 * Remove a Hub Group
 * @param id
 * @param requestOptions
 */
async function deleteHubGroup(id, requestOptions) {
    const ro = Object.assign(Object.assign({}, requestOptions), { id });
    await (0, arcgis_rest_portal_1.removeGroup)(ro);
}
//# sourceMappingURL=HubGroups.js.map