import { getGroup, removeGroup, createGroup, updateGroup, protectGroup, } from "@esri/arcgis-rest-portal";
import { DEFAULT_GROUP } from "./defaults";
import { convertHubGroupToGroup } from "./_internal/convertHubGroupToGroup";
import { convertGroupToHubGroup } from "./_internal/convertGroupToHubGroup";
import { getUniqueGroupTitle } from "./_internal/getUniqueGroupTitle";
import { createOrUpdateEntitySettings } from "../core/_internal/createOrUpdateEntitySettings";
import { fetchSettingV2 } from "../discussions/api/settings/settings";
import { setDiscussableKeyword } from "../discussions/utils";
import { setProp } from "../objects/set-prop";
import { getDefaultEntitySettings } from "../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
 */
export async function createHubGroup(partialGroup, context) {
    // merge the incoming and default groups
    const hubGroup = Object.assign(Object.assign({}, DEFAULT_GROUP), partialGroup);
    // ensure the group has a unique title
    const uniqueTitle = await getUniqueGroupTitle(hubGroup.name, context.userRequestOptions);
    hubGroup.name = uniqueTitle;
    hubGroup.typeKeywords = setDiscussableKeyword(hubGroup.typeKeywords, hubGroup.isDiscussable);
    const group = convertHubGroupToGroup(hubGroup);
    const opts = {
        group,
        authentication: context.userRequestOptions.authentication,
    };
    const result = await 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 protectGroup({
            id: result.group.id,
            authentication: context.userRequestOptions.authentication,
        })).success;
    }
    const entity = convertGroupToHubGroup(result.group, null, context.userRequestOptions);
    // create or update entity settings
    if (!context.isPortal) {
        const entitySetting = await 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
 */
export async function fetchHubGroup(identifier, requestOptions) {
    const group = await getGroup(identifier, requestOptions);
    let entitySettings;
    if (!requestOptions.isPortal) {
        entitySettings = await fetchSettingV2(Object.assign({ id: group.id }, requestOptions)).catch(() => (Object.assign({ id: null }, getDefaultEntitySettings("group"))));
    }
    else {
        entitySettings = Object.assign({ id: null }, getDefaultEntitySettings("group"));
    }
    const hubGroup = 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
 */
export async function updateHubGroup(hubGroup, context) {
    // TODO: fetch the upstream group and convert to a HubGroup so we can compare props
    hubGroup.typeKeywords = setDiscussableKeyword(hubGroup.typeKeywords, hubGroup.isDiscussable);
    const group = 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) {
        setProp("params.clearEmptyFields", true, opts);
    }
    await updateGroup(opts);
    // create or update entity settings
    if (!context.isPortal) {
        const entitySetting = await createOrUpdateEntitySettings(hubGroup, context.hubRequestOptions);
        hubGroup.entitySettingsId = entitySetting.id;
        hubGroup.discussionSettings = entitySetting.settings.discussions;
    }
    return hubGroup;
}
/**
 * @private
 * Remove a Hub Group
 * @param id
 * @param requestOptions
 */
export async function deleteHubGroup(id, requestOptions) {
    const ro = Object.assign(Object.assign({}, requestOptions), { id });
    await removeGroup(ro);
}
//# sourceMappingURL=HubGroups.js.map