"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createInitiativeTemplate = createInitiativeTemplate;
exports.updateInitiativeTemplate = updateInitiativeTemplate;
exports.deleteInitiativeTemplate = deleteInitiativeTemplate;
// Note - we separate these imports so we can cleanly spy on things in tests
const slugs_1 = require("../items/slugs");
const arcgis_rest_portal_1 = require("@esri/arcgis-rest-portal");
const PropertyMapper_1 = require("../core/_internal/PropertyMapper");
const defaults_1 = require("./defaults");
const computeProps_1 = require("./_internal/computeProps");
const getPropertyMap_1 = require("./_internal/getPropertyMap");
const util_1 = require("../util");
const ensureUniqueEntitySlug_1 = require("../items/_internal/ensureUniqueEntitySlug");
const utils_1 = require("../discussions/utils");
const createModel_1 = require("../models/createModel");
const getModel_1 = require("../models/getModel");
const updateModel_1 = require("../models/updateModel");
const set_prop_1 = require("../objects/set-prop");
/**
 * @private
 * Create a new Hub Initiative Template item
 * @param partialInitiativeTemplate
 * @param requestOptions
 * @returns
 */
async function createInitiativeTemplate(partialInitiativeTemplate, requestOptions) {
    const initiativeTemplate = Object.assign(Object.assign({}, defaults_1.DEFAULT_INITIATIVE_TEMPLATE), partialInitiativeTemplate);
    initiativeTemplate.orgUrlKey = initiativeTemplate.orgUrlKey.toLowerCase();
    // Create slug from the title if one is not passed in
    if (!initiativeTemplate.slug) {
        initiativeTemplate.slug = (0, slugs_1.constructSlug)(initiativeTemplate.name, initiativeTemplate.orgUrlKey);
    }
    // Ensure slug is unique
    await (0, ensureUniqueEntitySlug_1.ensureUniqueEntitySlug)(initiativeTemplate, requestOptions);
    initiativeTemplate.typeKeywords = (0, utils_1.setDiscussableKeyword)(initiativeTemplate.typeKeywords, initiativeTemplate.isDiscussable);
    // Map initiative template object onto a default initiative template model
    const mapper = new PropertyMapper_1.PropertyMapper((0, getPropertyMap_1.getPropertyMap)());
    // create model from object, using the default model as a starting point
    let model = mapper.entityToStore(initiativeTemplate, (0, util_1.cloneObject)(defaults_1.DEFAULT_INITIATIVE_TEMPLATE_MODEL));
    // create the item
    model = await (0, createModel_1.createModel)(model, requestOptions);
    // map the model back into a IHubInitiativeTemplate
    let newInitiativeTemplate = mapper.storeToEntity(model, {});
    newInitiativeTemplate = (0, computeProps_1.computeProps)(model, newInitiativeTemplate, requestOptions);
    // and return it
    return newInitiativeTemplate;
}
/**
 * @private
 * Update a Hub Initiative Template
 * @param initiativeTemplate
 * @param requestOptions
 */
async function updateInitiativeTemplate(initiativeTemplate, requestOptions) {
    var _a;
    // verify that the slug is unique, excluding the current initiative template
    await (0, ensureUniqueEntitySlug_1.ensureUniqueEntitySlug)(initiativeTemplate, requestOptions);
    // set discussable keyword
    initiativeTemplate.typeKeywords = (0, utils_1.setDiscussableKeyword)(initiativeTemplate.typeKeywords, initiativeTemplate.isDiscussable);
    // get the backing item & data
    const model = await (0, getModel_1.getModel)(initiativeTemplate.id, requestOptions);
    // create the PropertyMapper
    const mapper = new PropertyMapper_1.PropertyMapper((0, getPropertyMap_1.getPropertyMap)());
    // Note: Although we are fetching the model, and applying changes onto it,
    // we are not attempting to handle "concurrent edit" conflict resolution
    // but this is where we would apply that sort of logic
    const modelToUpdate = mapper.entityToStore(initiativeTemplate, model);
    // update the backing item
    const updatedModel = await (0, updateModel_1.updateModel)(modelToUpdate, requestOptions);
    // now map back into an initiative template and return that
    const updatedInitiativeTemplate = mapper.storeToEntity(updatedModel, initiativeTemplate);
    // the showMap prop is recently added (2025-09) to the default initiative
    // template, so we need to ensure it is set to true for existing ones that
    // may not have it
    if (((_a = initiativeTemplate.view) === null || _a === void 0 ? void 0 : _a.showMap) === undefined) {
        (0, set_prop_1.setProp)("view.showMap", true, initiativeTemplate);
    }
    initiativeTemplate = (0, computeProps_1.computeProps)(model, updatedInitiativeTemplate, requestOptions);
    // the casting is needed because modelToObject returns a `Partial<T>`
    // whereas this function returns a `T`
    return updatedInitiativeTemplate;
}
/**
 * @private
 * Remove a Hub Initiative Template
 * @param id
 * @param requestOptions
 */
async function deleteInitiativeTemplate(id, requestOptions) {
    const ro = Object.assign(Object.assign({}, requestOptions), { id });
    await (0, arcgis_rest_portal_1.removeItem)(ro);
    return;
}
//# sourceMappingURL=edit.js.map