import { PropertyMapper } from "../core/_internal/PropertyMapper";
import { cloneObject } from "../util";
import { getPropertyMap } from "./_internal/getPropertyMap";
import { computeProps } from "./_internal/computeProps";
import { removeItem, } from "@esri/arcgis-rest-portal";
import { ensureUniqueEntitySlug } from "../items/_internal/ensureUniqueEntitySlug";
import { setDiscussableKeyword } from "../discussions/utils";
import { getModel } from "../models/getModel";
import { updateModel } from "../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
 */
export 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
 */
export async function updateTemplate(template, requestOptions) {
    // 1. Verify the slug is unique, excluding the current template
    await ensureUniqueEntitySlug(template, requestOptions);
    // 2. Update relevant typeKeywords
    template.typeKeywords = setDiscussableKeyword(template.typeKeywords, template.isDiscussable);
    // 3. Fetch the backing model (item + data)
    const model = await getModel(template.id, requestOptions);
    // 4. Create a property mapper between the template
    // object and item model
    const mapper = new PropertyMapper(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 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 = computeProps(model, updatedTemplate, requestOptions);
    return updatedTemplate;
}
/**
 * Convert an IHubTemplateEditor back into a IHubTemplate
 * @param editor
 * @param portal
 */
export function editorToTemplate(editor, portal) {
    // 1. cast editor to IHubTemplate
    const template = 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
 */
export async function deleteTemplate(id, requestOptions) {
    const ro = Object.assign(Object.assign({}, requestOptions), { id });
    await removeItem(ro);
    return;
}
//# sourceMappingURL=edit.js.map