"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HubUser = void 0;
const getEditorConfig_1 = require("../core/schemas/getEditorConfig");
const enrichEntity_1 = require("../core/enrichEntity");
const util_1 = require("../util");
const defaults_1 = require("./defaults");
const updateCommunityOrgSettings_1 = require("../utils/internal/updateCommunityOrgSettings");
const updatePortalOrgSettings_1 = require("../utils/internal/updatePortalOrgSettings");
class HubUser {
    constructor(user, context) {
        this.isDestroyed = false;
        this.entity = user;
        this.context = context;
    }
    /**
     * Create an instance from a IHubUser object
     * @param json - JSON object to create a HubProject from
     * @param context - ArcGIS context
     * @returns HubUser
     */
    static fromJson(json, context) {
        // merge what we have with the default values
        const pojo = this.applyDefaults(json);
        return new HubUser(pojo, context);
    }
    /**
     * Given a partial user object, apply defaults to it to ensure that a baseline of properties are set
     * @param partialUser
     * @returns IHubUser
     */
    static applyDefaults(partialUser) {
        return Object.assign(Object.assign({}, defaults_1.DEFAULT_USER), partialUser);
    }
    /**
     * Method that returns the entity as a JSON object
     * We have this on the EntityItem class, but we don't implement that here
     * @returns IHubUser
     */
    toJson() {
        return (0, util_1.cloneObject)(this.entity);
    }
    /**
     * Save the HubUser to the backing store.
     *
     * Note that Hub does not currently support the creation of users,
     * so this function should only be used for updating users.
     *
     * @returns
     */
    async save() {
        if (this.isDestroyed) {
            throw new Error("HubUser is already destroyed.");
        }
        // 1. update user hub settings
        await this.context.updateUserHubSettings(this.entity.settings);
        // 2. update portal signin settings
        // we are in community org, user is org admin, and we have org settings to send
        if (this.context.isCommunityOrg &&
            this.context.isOrgAdmin &&
            this.entity.hubOrgSettings) {
            const { hubOrgSettings } = this.entity;
            // only send values if we have settings enabled
            // else we send an empty string to reset
            const newCommunityOrgSettings = {
                signupText: hubOrgSettings.enableSignupText && hubOrgSettings.signupText
                    ? hubOrgSettings.signupText
                    : "",
                termsAndConditions: hubOrgSettings.enableTermsAndConditions &&
                    hubOrgSettings.termsAndConditions
                    ? hubOrgSettings.termsAndConditions
                    : "",
            };
            // make the request to update the settings
            await (0, updateCommunityOrgSettings_1.updateCommunityOrgSettings)(newCommunityOrgSettings, this.context);
        }
        // 3. update portal settings
        // User is org admin, we have org settings to send, and we have a banner to show
        if (this.context.isOrgAdmin &&
            this.entity.hubOrgSettings &&
            Object.prototype.hasOwnProperty.call(this.entity.hubOrgSettings, "showInformationalBanner")) {
            // update the portal settings
            await (0, updatePortalOrgSettings_1.updatePortalOrgSettings)(this.entity.hubOrgSettings, this.context);
        }
        return;
    }
    /**
     * Delete the HubUser from the store
     * set a flag to indicate that it is destroyed
     *
     * Note that Hub does not currently support the deletion of users,
     * so this function should not be used as of now.
     * @returns
     */
    // eslint-disable-next-line @typescript-eslint/require-await
    async delete() {
        if (this.isDestroyed) {
            throw new Error("HubUser is already destroyed.");
        }
        // TODO: implement delete here when we want this functionality
        this.isDestroyed = true;
    }
    /**
     * Get the editor config for the HubUser entity.
     * @param i18nScope translation scope to be interpolated into the uiSchema
     * @param type editor type - corresponds to the returned uiSchema
     */
    async getEditorConfig(i18nScope, type) {
        // delegate to the schema subsystem
        return (0, getEditorConfig_1.getEditorConfig)(i18nScope, type, this.entity, this.context);
    }
    /**
     * Transforms entity values into editor values
     * @param editorContext
     * @param include
     */
    async toEditor(_editorContext = {}, include = []) {
        // 1. optionally enrich entity and cast to editor
        const editor = include.length
            ? (await (0, enrichEntity_1.enrichEntity)((0, util_1.cloneObject)(this.entity), include, this.context.hubRequestOptions))
            : (0, util_1.cloneObject)(this.entity);
        // 2. Apply transforms to relevant entity values
        // so they can be consumed by editor
        return editor;
    }
    /**
     * Transforms editor values into entity values
     * @param editor
     */
    async fromEditor(editor) {
        const entity = (0, util_1.cloneObject)(editor);
        // save user
        this.entity = entity;
        await this.save();
        return this.entity;
    }
}
exports.HubUser = HubUser;
//# sourceMappingURL=HubUser.js.map