"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createProject = createProject;
exports.updateProject = updateProject;
exports.deleteProject = deleteProject;
// 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 setEntityStatusKeyword_1 = require("../utils/internal/setEntityStatusKeyword");
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");
/**
 * @private
 * Create a new Hub Project item
 *
 * Minimal properties are name and org
 *
 * @param project
 * @param requestOptions
 */
async function createProject(partialProject, requestOptions) {
    // merge incoming with the default
    // this expansion solves the typing somehow
    const project = Object.assign(Object.assign({}, defaults_1.DEFAULT_PROJECT), partialProject);
    // ensure lowercase orgUrlKey
    project.orgUrlKey = project.orgUrlKey.toLowerCase();
    // Create a slug from the title if one is not passed in
    if (!project.slug) {
        project.slug = (0, slugs_1.constructSlug)(project.name, project.orgUrlKey);
    }
    // Ensure slug is  unique
    await (0, ensureUniqueEntitySlug_1.ensureUniqueEntitySlug)(project, requestOptions);
    // add status to keywords
    project.typeKeywords = (0, setEntityStatusKeyword_1.setEntityStatusKeyword)(project.typeKeywords, project.status);
    project.typeKeywords = (0, utils_1.setDiscussableKeyword)(project.typeKeywords, project.isDiscussable);
    // Map project object onto a default project 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(project, (0, util_1.cloneObject)(defaults_1.DEFAULT_PROJECT_MODEL));
    // create the item
    model = await (0, createModel_1.createModel)(model, requestOptions);
    // map the model back into a IHubProject
    let newProject = mapper.storeToEntity(model, {});
    newProject = (0, computeProps_1.computeProps)(model, newProject, requestOptions);
    // and return it
    return newProject;
}
/**
 * @private
 * Update a Hub Project
 * @param project
 * @param requestOptions
 */
async function updateProject(project, requestOptions) {
    // verify that the slug is unique, excluding the current project
    await (0, ensureUniqueEntitySlug_1.ensureUniqueEntitySlug)(project, requestOptions);
    // update the status keyword
    project.typeKeywords = (0, setEntityStatusKeyword_1.setEntityStatusKeyword)(project.typeKeywords, project.status);
    project.typeKeywords = (0, utils_1.setDiscussableKeyword)(project.typeKeywords, project.isDiscussable);
    // get the backing item & data
    const model = await (0, getModel_1.getModel)(project.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(project, model);
    // update the backing item
    const updatedModel = await (0, updateModel_1.updateModel)(modelToUpdate, requestOptions);
    // now map back into a project and return that
    let updatedProject = mapper.storeToEntity(updatedModel, project);
    updatedProject = (0, computeProps_1.computeProps)(model, updatedProject, requestOptions);
    // the casting is needed because modelToObject returns a `Partial<T>`
    // where as this function returns a `T`
    return updatedProject;
}
/**
 * @private
 * Remove a Hub Project
 * @param id
 * @param requestOptions
 */
async function deleteProject(id, requestOptions) {
    const ro = Object.assign(Object.assign({}, requestOptions), { id });
    await (0, arcgis_rest_portal_1.removeItem)(ro);
    return;
}
//# sourceMappingURL=edit.js.map