"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HubDiscussion = void 0;
const defaults_1 = require("./defaults");
const HubItemEntity_1 = require("../core/HubItemEntity");
const fetch_1 = require("./fetch");
const getEditorConfig_1 = require("../core/schemas/getEditorConfig");
const util_1 = require("../util");
const enrichEntity_1 = require("../core/enrichEntity");
const getEditorSlug_1 = require("../core/_internal/getEditorSlug");
const slugs_1 = require("../items/_internal/slugs");
const edit_1 = require("./edit");
/**
 * Hub Discussion Class
 */
class HubDiscussion extends HubItemEntity_1.HubItemEntity {
    /**
     * Create an instance from an IHubDiscussion object
     * @param json JSON object to create a HubDiscussion from
     * @param context ArcGIS context
     * @returns a HubDiscussion
     */
    static fromJson(json, context) {
        // merge what we have with the default values
        const pojo = this.applyDefaults(json, context);
        return new HubDiscussion(pojo, context);
    }
    /**
     * Create a new HubDiscussion, returning a HubDiscussion instance.
     * Note: This does not persist the Discussion into the backing store
     * @param partialDiscussion a partial IHubDiscussion
     * @param context ArcGIS context
     * @returns promise that resolves a HubDiscussion
     */
    static async create(partialDiscussion, context, save = false) {
        const pojo = this.applyDefaults(partialDiscussion, context);
        // return an instance of HubDiscussion
        const instance = HubDiscussion.fromJson(pojo, context);
        if (save) {
            await instance.save();
        }
        return instance;
    }
    /**
     * Fetch a Discussion from the backing store and return a HubDiscussion instance.
     * @param identifier slug or item id
     * @param context ArcGIS context
     * @returns promise that resolves a HubDiscussion
     */
    static async fetch(identifier, context) {
        try {
            const entity = await (0, fetch_1.fetchDiscussion)(identifier, context.hubRequestOptions);
            // create an instance of HubDiscussion from the entity
            return HubDiscussion.fromJson(entity, context);
        }
        catch (ex) {
            throw ex.message ===
                "CONT_0001: Item does not exist or is inaccessible."
                ? new Error("Discussion not found.")
                : ex;
        }
    }
    static applyDefaults(partialDiscussion, context) {
        // ensure we have the orgUrlKey
        if (!partialDiscussion.orgUrlKey) {
            partialDiscussion.orgUrlKey = context.portal.urlKey;
        }
        // extend the partial over the defaults
        const pojo = Object.assign(Object.assign({}, defaults_1.DEFAULT_DISCUSSION), partialDiscussion);
        return pojo;
    }
    /**
     * Apply a new state to the instance
     * @param changes A partial IHubDiscussion
     */
    update(changes) {
        if (this.isDestroyed) {
            throw new Error("HubDiscussion is already destroyed.");
        }
        // merge partial onto existing entity
        this.entity = Object.assign(Object.assign({}, this.entity), changes);
    }
    async save() {
        if (this.isDestroyed) {
            throw new Error("HubDiscussion is already destroyed.");
        }
        if (this.entity.id) {
            // update it
            this.entity = await (0, edit_1.updateDiscussion)(this.entity, this.context.hubRequestOptions);
        }
        else {
            // create it
            this.entity = await (0, edit_1.createDiscussion)(this.entity, this.context.hubRequestOptions);
        }
        // call the after save hook on superclass
        await super.afterSave();
        return;
    }
    /**
     * Delete the HubDiscussion from the store
     * set a flag to indicate that it is destroyed
     * @returns a promise
     */
    async delete() {
        if (this.isDestroyed) {
            throw new Error("HubDiscussion is already destroyed.");
        }
        this.isDestroyed = true;
        // Delegate to module fn
        await (0, edit_1.deleteDiscussion)(this.entity.id, this.context.hubRequestOptions);
    }
    /*
     * Get the editor config for the HubDiscussion entity.
     * @param i18nScope translation scope to be interpolated into the uiSchema
     * @param type editor type - corresonds to the returned uiSchema
     * @param options optional hash of dynamic uiSchema element options
     */
    async getEditorConfig(i18nScope, type) {
        // delegate to the schema subsystem
        return (0, getEditorConfig_1.getEditorConfig)(i18nScope, type, this.entity, this.context);
    }
    /**
     * Return the project as an editor object
     * @param editorContext
     * @returns
     */
    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 the editor
        editor._slug = (0, getEditorSlug_1.getEditorSlug)(this.entity);
        return editor;
    }
    /**
     * Load the project from the editor object
     * @param editor
     * @returns
     */
    async fromEditor(editor) {
        // TODO: Ideally we should delegate out to the
        // hubItemEntityFromEditor util to handle shared
        // "fromEditor" logic; however, when I attempted
        // to do this, I ran into issues with circular
        // dependencies and test failures that felt out
        // of scope for this work. For now, we'll copy
        // the relevant logic here.
        var _a;
        // 1. extract the ephemeral props we graft onto the
        // editor for later user
        const _thumbnail = editor._thumbnail;
        const _slug = editor._slug;
        // 2. remove the ephemeral props we graft onto the editor
        delete editor._thumbnail;
        delete editor._slug;
        const entity = (0, util_1.cloneObject)(editor);
        // 3. ensure orgUrlKey is set
        entity.orgUrlKey = editor.orgUrlKey
            ? editor.orgUrlKey
            : this.context.portal.urlKey || "";
        // ensure orgUrlKey is lowercase
        if (entity.orgUrlKey) {
            entity.orgUrlKey = entity.orgUrlKey.toLowerCase();
        }
        // 4. copy the configured location extent up one level
        // on the entity.
        entity.extent = (_a = editor.location) === null || _a === void 0 ? void 0 : _a.extent;
        // 5. Perform pre-save operations using the ephemeral
        // properties that were extracted above.
        // a. ensure the slug is truncated
        if (_slug) {
            // ensure the slug is truncated
            entity.slug = (0, slugs_1.truncateSlug)(_slug, entity.orgUrlKey);
        }
        else {
            // if no slug is passed in, save an empty string as
            // the slug, so that it is not saved as the orgUrlKey
            // truncated with an empty string
            entity.slug = "";
        }
        // b. conditionally set the thumbnailCache to
        // ensure that the configured thumbnail is updated
        // on the next save
        if (_thumbnail) {
            if (_thumbnail.blob) {
                this.thumbnailCache = {
                    file: _thumbnail.blob,
                    filename: _thumbnail.fileName,
                    clear: false,
                };
            }
            else {
                this.thumbnailCache = {
                    clear: true,
                };
            }
        }
        // Save, which will also create new content if new
        this.entity = entity;
        await this.save();
        return this.entity;
    }
}
exports.HubDiscussion = HubDiscussion;
//# sourceMappingURL=HubDiscussion.js.map