"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.matchById = matchById;
const arcgis_rest_portal_1 = require("@esri/arcgis-rest-portal");
const fetch_1 = require("../../events/fetch");
const is_cuid_1 = require("../../utils/is-cuid");
const get_with_default_1 = require("../../objects/get-with-default");
/**
 * Match by Id
 * Handles both GUIDs and CUIDs
 * @param type
 * @param identifier
 * @param context
 * @returns
 */
async function matchById(type, identifier, context) {
    const result = {
        identifier,
        type,
        matcher: "id",
        num: 0,
        matches: [],
    };
    let entity = null;
    if ((0, is_cuid_1.isCuid)(identifier)) {
        // Only Event entities use CUIDs
        const event = await (0, fetch_1.fetchEvent)(identifier, context.userRequestOptions);
        if (event) {
            entity = {
                id: event.id,
                type: "Event",
                name: event.name,
                identifier: event.slug ? event.slug : event.id,
            };
            result.matches.push(entity);
        }
    }
    else {
        // Item-backed entity
        const item = await (0, arcgis_rest_portal_1.getItem)(identifier, context.userRequestOptions);
        // Convert item to IHubEntityBase
        if (item) {
            entity = {
                id: item.id,
                type: item.type,
                name: item.title,
                identifier: (0, get_with_default_1.getWithDefault)(item, "properties.slug", item.id),
            };
            result.matches.push(entity);
        }
        result.num = result.matches.length;
    }
    return result;
}
//# sourceMappingURL=matchById.js.map