// Note - we separate these imports so we can cleanly spy on things in tests
import { constructSlug } from "../items/slugs";
import { removeItem } from "@esri/arcgis-rest-portal";
import { PropertyMapper } from "../core/_internal/PropertyMapper";
import { DEFAULT_INITIATIVE_TEMPLATE, DEFAULT_INITIATIVE_TEMPLATE_MODEL, } from "./defaults";
import { computeProps } from "./_internal/computeProps";
import { getPropertyMap } from "./_internal/getPropertyMap";
import { cloneObject } from "../util";
import { ensureUniqueEntitySlug } from "../items/_internal/ensureUniqueEntitySlug";
import { setDiscussableKeyword } from "../discussions/utils";
import { createModel } from "../models/createModel";
import { getModel } from "../models/getModel";
import { updateModel } from "../models/updateModel";
import { setProp } from "../objects/set-prop";
/**
 * @private
 * Create a new Hub Initiative Template item
 * @param partialInitiativeTemplate
 * @param requestOptions
 * @returns
 */
export async function createInitiativeTemplate(partialInitiativeTemplate, requestOptions) {
    const initiativeTemplate = Object.assign(Object.assign({}, 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 = constructSlug(initiativeTemplate.name, initiativeTemplate.orgUrlKey);
    }
    // Ensure slug is unique
    await ensureUniqueEntitySlug(initiativeTemplate, requestOptions);
    initiativeTemplate.typeKeywords = setDiscussableKeyword(initiativeTemplate.typeKeywords, initiativeTemplate.isDiscussable);
    // Map initiative template object onto a default initiative template model
    const mapper = new PropertyMapper(getPropertyMap());
    // create model from object, using the default model as a starting point
    let model = mapper.entityToStore(initiativeTemplate, cloneObject(DEFAULT_INITIATIVE_TEMPLATE_MODEL));
    // create the item
    model = await createModel(model, requestOptions);
    // map the model back into a IHubInitiativeTemplate
    let newInitiativeTemplate = mapper.storeToEntity(model, {});
    newInitiativeTemplate = computeProps(model, newInitiativeTemplate, requestOptions);
    // and return it
    return newInitiativeTemplate;
}
/**
 * @private
 * Update a Hub Initiative Template
 * @param initiativeTemplate
 * @param requestOptions
 */
export async function updateInitiativeTemplate(initiativeTemplate, requestOptions) {
    var _a;
    // verify that the slug is unique, excluding the current initiative template
    await ensureUniqueEntitySlug(initiativeTemplate, requestOptions);
    // set discussable keyword
    initiativeTemplate.typeKeywords = setDiscussableKeyword(initiativeTemplate.typeKeywords, initiativeTemplate.isDiscussable);
    // get the backing item & data
    const model = await getModel(initiativeTemplate.id, requestOptions);
    // create the PropertyMapper
    const mapper = new PropertyMapper(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 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) {
        setProp("view.showMap", true, initiativeTemplate);
    }
    initiativeTemplate = 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
 */
export async function deleteInitiativeTemplate(id, requestOptions) {
    const ro = Object.assign(Object.assign({}, requestOptions), { id });
    await removeItem(ro);
    return;
}
//# sourceMappingURL=edit.js.map