import { getItem } from "@esri/arcgis-rest-portal";
import { fetchEvent } from "../../events/fetch";
import { isCuid } from "../../utils/is-cuid";
import { getWithDefault } from "../../objects/get-with-default";
/**
 * Match by Id
 * Handles both GUIDs and CUIDs
 * @param type
 * @param identifier
 * @param context
 * @returns
 */
export async function matchById(type, identifier, context) {
    const result = {
        identifier,
        type,
        matcher: "id",
        num: 0,
        matches: [],
    };
    let entity = null;
    if (isCuid(identifier)) {
        // Only Event entities use CUIDs
        const event = await 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 getItem(identifier, context.userRequestOptions);
        // Convert item to IHubEntityBase
        if (item) {
            entity = {
                id: item.id,
                type: item.type,
                name: item.title,
                identifier: getWithDefault(item, "properties.slug", item.id),
            };
            result.matches.push(entity);
        }
        result.num = result.matches.length;
    }
    return result;
}
//# sourceMappingURL=matchById.js.map