import { isCuid } from "../utils/is-cuid";
import { isGuid } from "../utils/is-guid";
import { matchBySlug } from "./matchUtils/matchBySlug";
import { matchById } from "./matchUtils/matchById";
/**
 * Find entities that match the given identifier.
 * Supported identifiers:
 * - GUID / CUID
 * - ID segment (e.g. "custom-slug~1234-4567-890a-bcde-f1234567890")
 * - org-short::custom-slug, via search: typekeyword: "slug|{identifier}" and type: {type}
 * - indexer-slug-via-title, via search: title: "%identifier.replace('-', ' ')%" and type: {type}
 * @param type
 * @param identifier
 * @param context
 * @returns
 */
export async function matchIdentifier(type, identifier, context) {
    let result = {
        identifier,
        type,
        matcher: "id",
        num: 0,
        matches: [],
    };
    // is GUID or CUID? match by Id
    if (isGuid(identifier) || isCuid(identifier)) {
        result = await matchById(type, identifier, context);
    }
    else {
        // the route should handle this, but just in case,
        // if the slug has '::' in it, swap that to '|' for matching
        identifier = identifier.replace("::", "|");
        // Does the identifier have a ~? if so, split on that and check the last segment as an id
        const parts = identifier.split("~");
        if (parts.length > 1) {
            const slugPart = parts[0];
            const id = parts[1];
            if (isGuid(id) || isCuid(id)) {
                result = await matchById(type, id, context);
                result.matcher = "id-segment";
            }
            else {
                // id segment did not return results, match slug segment
                result = await matchBySlug(type, slugPart, context);
            }
        }
        else {
            // no ~id segment, match slug segment
            result = await matchBySlug(type, identifier, context);
        }
    }
    return result;
}
//# sourceMappingURL=matchIdentifier.js.map