"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTemplate = createTemplate;
exports.updateTemplate = updateTemplate;
exports.editorToTemplate = editorToTemplate;
exports.deleteTemplate = deleteTemplate;
const PropertyMapper_1 = require("../core/_internal/PropertyMapper");
const util_1 = require("../util");
const getPropertyMap_1 = require("./_internal/getPropertyMap");
const computeProps_1 = require("./_internal/computeProps");
const arcgis_rest_portal_1 = require("@esri/arcgis-rest-portal");
const ensureUniqueEntitySlug_1 = require("../items/_internal/ensureUniqueEntitySlug");
const utils_1 = require("../discussions/utils");
const getModel_1 = require("../models/getModel");
const updateModel_1 = require("../models/updateModel");
/**
 * @private
 * Creates a Hub Template's backing Solution item and returns
 * the created entity
 *
 * NOTE: We have no immediate plans to allow template creation
 * from the context of the Hub application, but scaffolding
 * this util for potential future implementation. For now,
 * we will throw an error
 * @param _partialTemplate
 * @param _requestOptions
 */
function createTemplate(_partialTemplate, _requestOptions) {
    throw new Error("Template creation is not currently supported from the context of Hub");
}
/**
 * @private
 * Updates a Hub Template's backing item and returns
 * the updated entity
 * @param template
 * @param requestOptions
 */
async function updateTemplate(template, requestOptions) {
    // 1. Verify the slug is unique, excluding the current template
    await (0, ensureUniqueEntitySlug_1.ensureUniqueEntitySlug)(template, requestOptions);
    // 2. Update relevant typeKeywords
    template.typeKeywords = (0, utils_1.setDiscussableKeyword)(template.typeKeywords, template.isDiscussable);
    // 3. Fetch the backing model (item + data)
    const model = await (0, getModel_1.getModel)(template.id, requestOptions);
    // 4. Create a property mapper between the template
    // object and item model
    const mapper = new PropertyMapper_1.PropertyMapper((0, getPropertyMap_1.getPropertyMap)());
    // 5. Create item model from updated template object, using
    // the existing model as a starting point
    const modelToUpdate = mapper.entityToStore(template, model);
    // 6. Update the backing item
    const updatedModel = await (0, updateModel_1.updateModel)(modelToUpdate, requestOptions);
    // 7. Map the item back into an IHubTemplate
    const updatedTemplate = mapper.storeToEntity(updatedModel, template);
    // 8. Compute + set various properties on the IHubTemplate
    // that cannot be directly mapped from the item
    template = (0, computeProps_1.computeProps)(model, updatedTemplate, requestOptions);
    return updatedTemplate;
}
/**
 * Convert an IHubTemplateEditor back into a IHubTemplate
 * @param editor
 * @param portal
 */
function editorToTemplate(editor, portal) {
    // 1. cast editor to IHubTemplate
    const template = (0, util_1.cloneObject)(editor);
    // 2. ensure there's an org url key
    template.orgUrlKey = (editor.orgUrlKey ? editor.orgUrlKey : portal.urlKey);
    // 3. ensure orgUrlKey is lowercase
    template.orgUrlKey = template.orgUrlKey.toLowerCase();
    return template;
}
/**
 * @private
 * Remove a Hub Template's backing item
 * @param id
 * @param requestOptions
 */
async function deleteTemplate(id, requestOptions) {
    const ro = Object.assign(Object.assign({}, requestOptions), { id });
    await (0, arcgis_rest_portal_1.removeItem)(ro);
    return;
}
//# sourceMappingURL=edit.js.map