import { nodeTypeToIcon } from "./catalog.js";
const nodeTypes = Object.keys(nodeTypeToIcon);
const nodeTypeSet = new Set(nodeTypes);
const nodeTypeAliases = new Map();
const uniqueAliases = new Map();
function normalizeLookupKey(value) {
    return value
        .trim()
        .toLowerCase()
        .replace(/&/g, "and")
        .replace(/[_\s/]+/g, "-")
        .replace(/-?\.-?/g, ".")
        .replace(/\.{2,}/g, ".")
        .replace(/-{2,}/g, "-")
        .replace(/^[-.]+|[-.]+$/g, "");
}
function isNodeType(value) {
    return nodeTypeSet.has(value);
}
function registerAlias(alias, type) {
    const normalizedAlias = normalizeLookupKey(alias);
    if (!normalizedAlias || nodeTypeAliases.has(normalizedAlias)) {
        return;
    }
    nodeTypeAliases.set(normalizedAlias, type);
}
function registerUniqueAlias(alias, type) {
    const normalizedAlias = normalizeLookupKey(alias);
    if (!normalizedAlias) {
        return;
    }
    const existing = uniqueAliases.get(normalizedAlias);
    if (existing === undefined) {
        uniqueAliases.set(normalizedAlias, type);
        return;
    }
    if (existing !== type) {
        uniqueAliases.set(normalizedAlias, null);
    }
}
for (const type of nodeTypes) {
    const segments = type.split(".");
    registerAlias(type, type);
    registerAlias(type.replace(/^aws\./, ""), type);
    const lastSegment = segments[segments.length - 1];
    if (lastSegment) {
        registerUniqueAlias(lastSegment, type);
    }
    if (segments.length >= 4) {
        const parentAndChild = `${segments[segments.length - 2]}.${segments[segments.length - 1]}`;
        registerUniqueAlias(parentAndChild, type);
    }
    const repeatedSegment = segments.length === 4 ? segments[2] : undefined;
    if (repeatedSegment && repeatedSegment === segments[3]) {
        registerAlias(segments.slice(0, 3).join("."), type);
        registerUniqueAlias(repeatedSegment, type);
    }
}
for (const [alias, type] of uniqueAliases) {
    if (type) {
        registerAlias(alias, type);
    }
}
const manualAliases = {
    "acm": "aws.security-identity.certificate-manager",
    "api-gateway": "aws.networking-content-delivery.api-gateway",
    "apigateway": "aws.networking-content-delivery.api-gateway",
    "apigw": "aws.networking-content-delivery.api-gateway",
    "aws.general.user": "aws.general.user.user",
    "aws.general.users": "aws.general.users.users",
    "bucket": "aws.storage.simple-storage-service.bucket",
    "cloudfront": "aws.networking-content-delivery.cloudfront",
    "cloudtrail": "aws.management-tools.cloudtrail",
    "cloudwatch": "aws.management-tools.cloudwatch",
    "ddb": "aws.databases.dynamodb",
    "dynamodb": "aws.databases.dynamodb",
    "eventbridge": "aws.application-integration.eventbridge",
    "iam": "aws.security-identity.identity-and-access-management",
    "kms": "aws.security-identity.key-management-service",
    "lambda": "aws.compute.lambda",
    "oac": "aws.security-identity.identity-and-access-management",
    "origin-access-control": "aws.security-identity.identity-and-access-management",
    "origin-access-identity": "aws.security-identity.identity-and-access-management",
    "route53": "aws.networking-content-delivery.route-53",
    "route-53": "aws.networking-content-delivery.route-53",
    "s3": "aws.storage.simple-storage-service",
    "s3-bucket": "aws.storage.simple-storage-service.bucket",
    "secrets-manager": "aws.security-identity.secrets-manager",
    "secretsmanager": "aws.security-identity.secrets-manager",
    "sns": "aws.application-integration.simple-notification-service",
    "sqs": "aws.application-integration.simple-queue-service",
    "textract": "aws.artificial-intelligence.textract",
    "user": "aws.general.user.user",
    "users": "aws.general.users.users",
    "waf": "aws.security-identity.waf",
};
for (const [alias, type] of Object.entries(manualAliases)) {
    nodeTypeAliases.set(normalizeLookupKey(alias), type);
}
function resolveRepeatedSuffix(normalizedValue) {
    const segments = normalizedValue.split(".").filter(Boolean);
    if (segments.length < 3) {
        return undefined;
    }
    const repeated = `${segments.join(".")}.${segments[segments.length - 1]}`;
    return isNodeType(repeated) ? repeated : undefined;
}
export function resolveNodeType(value) {
    if (isNodeType(value)) {
        return value;
    }
    const normalizedValue = normalizeLookupKey(value);
    if (!normalizedValue) {
        return undefined;
    }
    if (isNodeType(normalizedValue)) {
        return normalizedValue;
    }
    const directMatch = nodeTypeAliases.get(normalizedValue);
    if (directMatch) {
        return directMatch;
    }
    const repeatedMatch = resolveRepeatedSuffix(normalizedValue);
    if (repeatedMatch) {
        return repeatedMatch;
    }
    const awsPrefixedValue = normalizedValue.startsWith("aws.") ? normalizedValue : `aws.${normalizedValue}`;
    if (isNodeType(awsPrefixedValue)) {
        return awsPrefixedValue;
    }
    return nodeTypeAliases.get(awsPrefixedValue) ?? resolveRepeatedSuffix(awsPrefixedValue);
}
export function listNodeTypeAliases() {
    return [...nodeTypeAliases.entries()]
        .sort(([leftAlias], [rightAlias]) => leftAlias.localeCompare(rightAlias))
        .map(([alias, type]) => ({ alias, type }));
}
export function normalizeNodeTypeInput(value, options = {}) {
    const resolution = options.iconRegistry?.resolveType(value);
    if (resolution && resolution.kind !== "ambiguous") {
        return resolution.definition.type;
    }
    return normalizeLookupKey(value);
}
//# sourceMappingURL=node-resolver.js.map