"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HubInitiativeTemplate = void 0;
const HubItemEntity_1 = require("../core/HubItemEntity");
const getEditorConfig_1 = require("../core/schemas/getEditorConfig");
const Catalog_1 = require("../search/Catalog");
const defaults_1 = require("./defaults");
const fetch_1 = require("./fetch");
const view_1 = require("./view");
const util_1 = require("../util");
const edit_1 = require("./edit");
const enrichEntity_1 = require("../core/enrichEntity");
const getEditorSlug_1 = require("../core/_internal/getEditorSlug");
const hubItemEntityFromEditor_1 = require("../core/_internal/hubItemEntityFromEditor");
const set_prop_1 = require("../objects/set-prop");
/**
 * Hub Initiative Template Class
 */
class HubInitiativeTemplate extends HubItemEntity_1.HubItemEntity {
    constructor(initiativeTemplate, context) {
        super(initiativeTemplate, context);
        this._catalog = Catalog_1.Catalog.fromJson(initiativeTemplate.catalog, this.context);
    }
    get catalog() {
        return this._catalog;
    }
    /**
     * Create an instance from an IHubInitiativeTemplate object
     * @param json - JSON object to create a HubInitiativeTemplate from
     * @param context - ArcGIS context
     * @returns - instance of HubInitiativeTemplate
     */
    static fromJson(json, context) {
        // merge what we have with the default values
        const pojo = this.applyDefaults(json, context);
        return new HubInitiativeTemplate(pojo, context);
    }
    /**
     * Create a new HubInitiativeTemplate, returning a HubInitiativeTemplate instance.
     * Note: This does not persist the HubInitiativeTemplate into the backing store
     * @param partialInitiativeTemplate
     * @param context
     * @param save
     * @returns
     */
    static async create(partialInitiativeTemplate, context, save = false) {
        const pojo = this.applyDefaults(partialInitiativeTemplate, context);
        // return an instance of HubInitiativeTemplate
        const instance = HubInitiativeTemplate.fromJson(pojo, context);
        if (save) {
            await instance.save();
        }
        return instance;
    }
    /**
     * Fetch a HubInitiativeTemplate from the backing store and return a HubInitiativeTemplate instance.
     * @param identifier - Identifier of the initiative template to load
     * @param context
     */
    static async fetch(identifier, context) {
        // fetch the initiative template by id or slug
        try {
            const initiativeTemplate = await (0, fetch_1.fetchInitiativeTemplate)(identifier, context.requestOptions);
            // create an instance of HubInitiativeTemplate from the initiative template
            return HubInitiativeTemplate.fromJson(initiativeTemplate, context);
        }
        catch (ex) {
            if (ex.message ===
                "CONT_0001: Item does not exist or is inaccessible.") {
                throw new Error(`Initiative Template not found.`);
            }
            else {
                throw ex;
            }
        }
    }
    /**
     * Given a partial initiative template, apply defaults to it to ensure that a baseline of properties are set
     * @param partialInitiativeTemplate
     * @param context
     * @returns
     */
    static applyDefaults(partialInitiativeTemplate, context) {
        // ensure we have the orgUrlKey
        if (!partialInitiativeTemplate.orgUrlKey) {
            partialInitiativeTemplate.orgUrlKey = context.portal.urlKey;
        }
        // ensure orgUrlKey is lowercase
        partialInitiativeTemplate.orgUrlKey =
            partialInitiativeTemplate.orgUrlKey.toLowerCase();
        const pojo = Object.assign(Object.assign({}, defaults_1.DEFAULT_INITIATIVE_TEMPLATE), partialInitiativeTemplate);
        return pojo;
    }
    /**
     * Convert the project entity into a card view model that can
     * be consumed by the suite of hub gallery components
     * @param opts - view model options
     * @returns
     */
    convertToCardModel(opts) {
        return (0, view_1.initiativeTemplateToCardModel)(this.entity, this.context, opts);
    }
    /**
     * Get the editor config for the HubInitiativeTemplate 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);
    }
    /**
     * Return the initiative template as an editor object
     * @param editorContext
     */
    async toEditor(_editorContext = {}, include = []) {
        // cast the entity to its 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);
        // slug life
        editor._slug = (0, getEditorSlug_1.getEditorSlug)(this.entity);
        // for now, just return
        return editor;
    }
    /**
     * Load the initiative template from the editor object
     * @param editor
     */
    async fromEditor(editor) {
        // delegate to an item-specific fromEditor util to
        // handle shared "fromEditor" logic
        const res = await (0, hubItemEntityFromEditor_1.hubItemEntityFromEditor)(editor, this.context);
        // iterate over the res object keys and set the values
        // on the HubInitiativeTemplate instance
        Object.entries(res).forEach(([key, value]) => {
            (0, set_prop_1.setProp)(key, value, this);
        });
        // save, which will also create an entity if we don't have an id for it
        await this.save();
        return this.entity;
    }
    /**
     * Apply a new state to the instance
     * @param changes
     */
    update(changes) {
        if (this.isDestroyed) {
            throw new Error("HubInitiativeTemplate is already destroyed.");
        }
        // merge partial onto existing entity
        this.entity = Object.assign(Object.assign({}, this.entity), changes);
        // update internal instances
        if (changes.catalog) {
            this._catalog = Catalog_1.Catalog.fromJson(this.entity.catalog, this.context);
        }
    }
    /**
     * Save the HubInitiativeTemplate to the backing store.
     * @returns
     */
    async save() {
        if (this.isDestroyed) {
            throw new Error("HubInitiativeTemplate is already destroyed.");
        }
        // get the catalog and permission configs
        this.entity.catalog = this._catalog.toJson();
        if (this.entity.id) {
            // update it
            this.entity = await (0, edit_1.updateInitiativeTemplate)(this.entity, this.context.userRequestOptions);
        }
        else {
            // create it
            this.entity = await (0, edit_1.createInitiativeTemplate)(this.entity, this.context.userRequestOptions);
        }
        // call the after save hook on superclass
        await super.afterSave();
    }
    /**
     * Delete the HubInitiativeTemplate from the store
     * set a flag to indicate that it is destroyed
     * @returns
     */
    async delete() {
        if (this.isDestroyed) {
            throw new Error("HubInitiativeTemplate is already destroyed.");
        }
        this.isDestroyed = true;
        await (0, edit_1.deleteInitiativeTemplate)(this.entity.id, this.context.userRequestOptions);
    }
}
exports.HubInitiativeTemplate = HubInitiativeTemplate;
//# sourceMappingURL=HubInitiativeTemplate.js.map