// (C) 2022-2025 GoodData Corporation
import { REFERENCE_REGEX_MATCH } from "../plugins/types.js";
export function collectReferences(content) {
    const map = {};
    const regex = new RegExp(REFERENCE_REGEX_MATCH.source, REFERENCE_REGEX_MATCH.flags);
    let parts = regex.exec(content);
    while (parts) {
        const { id, ref } = createReference(parts);
        if (ref?.type) {
            map[id] = {
                ref,
                type: ref.type,
            };
        }
        parts = regex.exec(content);
    }
    return map;
}
export function createReference(parts) {
    const id = parts[2];
    const type = normalizeType(parts[3]);
    if (type) {
        const identifier = parts[4];
        return {
            id,
            ref: {
                type,
                identifier,
            },
        };
    }
    return {
        id,
        ref: null,
    };
}
function normalizeType(type) {
    if (type === "label" || type === "displayForm") {
        return "displayForm";
    }
    if (type === "metric" || type === "measure") {
        return "measure";
    }
    return null;
}
