// Note - we separate these imports so we can cleanly spy on things in tests
import { constructSlug, getItemBySlug } from "../items/slugs";
import { removeItem, getItem, } from "@esri/arcgis-rest-portal";
import { PropertyMapper } from "../core/_internal/PropertyMapper";
import { parseInclude } from "../search/_internal/parseInclude";
import { fetchItemEnrichments } from "../items/_enrichments";
import { DEFAULT_INITIATIVE, DEFAULT_INITIATIVE_MODEL } from "./defaults";
import { getPropertyMap } from "./_internal/getPropertyMap";
import { computeProps } from "./_internal/computeProps";
import { combineQueries } from "../search/combineQueries";
import { getTypeWithKeywordQuery } from "../associations/internal/getTypeWithKeywordQuery";
import { negateGroupPredicates } from "../search/negateGroupPredicates";
import { computeLinks } from "./_internal/computeLinks";
import { deriveLocationFromItem } from "../content/_internal/internalContentUtils";
import { setEntityStatusKeyword } from "../utils/internal/setEntityStatusKeyword";
import { ensureUniqueEntitySlug } from "../items/_internal/ensureUniqueEntitySlug";
import { setDiscussableKeyword } from "../discussions/utils";
import { cloneObject, unique } from "../util";
import { isGuid } from "../utils/is-guid";
import { getFamily } from "../content/get-family";
import { getProp } from "../objects/get-prop";
import { mapBy } from "../utils/map-by";
import { createModel } from "../models/createModel";
import { fetchModelFromItem } from "../models/fetchModelFromItem";
import { getModel } from "../models/getModel";
import { updateModel } from "../models/updateModel";
/**
 * @private
 * Create a new Hub Initiative item
 *
 * Minimal properties are name and orgUrlKey
 *
 * @param partialInitiative
 * @param requestOptions
 */
export async function createInitiative(partialInitiative, requestOptions) {
    // merge incoming with the default
    // this expansion solves the typing somehow
    const initiative = Object.assign(Object.assign({}, DEFAULT_INITIATIVE), partialInitiative);
    // ensure orgUrlKey is lowercase
    initiative.orgUrlKey = initiative.orgUrlKey.toLowerCase();
    // Create a slug from the title if one is not passed in
    if (!initiative.slug) {
        initiative.slug = constructSlug(initiative.name, initiative.orgUrlKey);
    }
    // Ensure slug is  unique
    await ensureUniqueEntitySlug(initiative, requestOptions);
    // add the status keyword
    initiative.typeKeywords = setEntityStatusKeyword(initiative.typeKeywords, initiative.status);
    initiative.typeKeywords = setDiscussableKeyword(initiative.typeKeywords, initiative.isDiscussable);
    // Map initiative object onto a default initiative Model
    const mapper = new PropertyMapper(getPropertyMap());
    // create model from object, using the default model as a starting point
    let model = mapper.entityToStore(initiative, cloneObject(DEFAULT_INITIATIVE_MODEL));
    // create the item
    model = await createModel(model, requestOptions);
    // map the model back into a IHubInitiative
    let newInitiative = mapper.storeToEntity(model, {});
    newInitiative = computeProps(model, newInitiative, requestOptions);
    // and return it
    return newInitiative;
}
/**
 * @private
 * Update a Hub Initiative
 * @param initiative
 * @param requestOptions
 */
export async function updateInitiative(initiative, requestOptions) {
    // verify that the slug is unique, excluding the current initiative
    await ensureUniqueEntitySlug(initiative, requestOptions);
    // update the status keyword
    initiative.typeKeywords = setEntityStatusKeyword(initiative.typeKeywords, initiative.status);
    initiative.typeKeywords = setDiscussableKeyword(initiative.typeKeywords, initiative.isDiscussable);
    // get the backing item & data
    const model = await getModel(initiative.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(initiative, model);
    // update the backing item
    const updatedModel = await updateModel(modelToUpdate, requestOptions);
    // now map back into an initiative and return that
    let updatedInitiative = mapper.storeToEntity(updatedModel, initiative);
    updatedInitiative = computeProps(model, updatedInitiative, requestOptions);
    // the casting is needed because modelToObject returns a `Partial<T>`
    // where as this function returns a `T`
    return updatedInitiative;
}
/**
 * @private
 * Get a Hub Initiative by id or slug
 * @param identifier item id or slug
 * @param requestOptions
 */
export function fetchInitiative(identifier, requestOptions) {
    let getPrms;
    if (isGuid(identifier)) {
        // get item by id
        getPrms = getItem(identifier, requestOptions);
    }
    else {
        getPrms = getItemBySlug(identifier, requestOptions);
    }
    return getPrms.then((item) => {
        if (!item)
            return null;
        return convertItemToInitiative(item, requestOptions);
    });
}
/**
 * @private
 * Remove a Hub Initiative
 * @param id
 * @param requestOptions
 */
export async function deleteInitiative(id, requestOptions) {
    const ro = Object.assign(Object.assign({}, requestOptions), { id });
    await removeItem(ro);
    return;
}
/**
 * @private
 * Convert an Hub Initiative Item into a Hub Initiative, fetching any additional
 * information that may be required
 * @param item
 * @param auth
 * @returns
 */
export async function convertItemToInitiative(item, requestOptions) {
    const model = await fetchModelFromItem(item, requestOptions);
    const mapper = new PropertyMapper(getPropertyMap());
    const prj = mapper.storeToEntity(model, {});
    return computeProps(model, prj, requestOptions);
}
/**
 * @private
 * Fetch Initiative specific enrichments
 * @param item
 * @param include
 * @param requestOptions
 * @returns
 */
export async function enrichInitiativeSearchResult(item, include, requestOptions) {
    // Create the basic structure
    const result = {
        access: item.access,
        id: item.id,
        type: item.type,
        name: item.title,
        owner: item.owner,
        typeKeywords: item.typeKeywords,
        summary: item.snippet || item.description,
        createdDate: new Date(item.created),
        createdDateSource: "item.created",
        updatedDate: new Date(item.modified),
        updatedDateSource: "item.modified",
        family: getFamily(item.type),
        links: {
            self: "not-implemented",
            siteRelative: "not-implemented",
            thumbnail: "not-implemented",
            workspaceRelative: "not-implemented",
        },
        location: deriveLocationFromItem(item),
        rawResult: item,
    };
    // default includes
    const DEFAULTS = [];
    // merge includes
    include = [...DEFAULTS, ...include].filter(unique);
    // Parse the includes into a valid set of enrichments
    const specs = include.map(parseInclude);
    // Extract out the low-level enrichments needed
    const enrichments = mapBy("enrichment", specs).filter(unique);
    // fetch the enrichments
    let enriched = {};
    if (enrichments.length) {
        // TODO: Look into caching for the requests in fetchItemEnrichments
        enriched = await fetchItemEnrichments(item, enrichments, requestOptions);
    }
    // map the enriched props onto the result
    specs.forEach((spec) => {
        result[spec.prop] = getProp(enriched, spec.path);
    });
    // Handle links
    // TODO: Link handling should be an enrichment
    result.links = computeLinks(item, requestOptions);
    return result;
}
// NOTE: even though I can't find any uses of
// this deprecated function in the codebase
// if you delete it, tests in the following files will fail
// but _only in node_, not in chrome, so go figure:
// - projects/fetch.test.ts
// - search/_internal/portalSearchItems.test.ts
/**
 * ** DEPRECATED: Please use the association methods directly.
 * This will be removed in the next breaking version **
 *
 * Related Projects are those that have the Initiative id in the
 * typekeywords but NOT in the catalog. We use this query to show
 * Projects which want to be associated but are not yet included in
 * the catalog
 * This is passed into the Gallery showing "Pending Projects"
 * @param initiative
 * @returns
 */
/* istanbul ignore next -- @preserve */
export function getPendingProjectsQuery(initiative) {
    // get query that returns Hub Projects with the initiative keyword
    let query = getTypeWithKeywordQuery("Hub Project", `initiative|${initiative.id}`);
    // The the item scope from the catalog...
    const qry = getProp(initiative, "catalog.scopes.item");
    // negate the scope, combine that with the base query
    query = combineQueries([query, negateGroupPredicates(qry)]);
    return query;
}
//# sourceMappingURL=HubInitiatives.js.map