"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.canonicalStackLabels = void 0;
exports.normalizeStackMetadataFromInitSelections = normalizeStackMetadataFromInitSelections;
exports.normalizeStackMetadata = normalizeStackMetadata;
const validProjectKinds = new Set(['backend', 'frontend', 'library']);
const validCanonicalStackIds = new Set([
    'node_typescript',
    'express',
    'nestjs',
    'java',
    'spring',
    'fastapi',
    'django',
    'flask',
    'python',
    'php',
    'laravel',
    'go',
    'dotnet',
    'ruby_rails',
    'kotlin',
    'kotlin_spring',
    'rust',
    'react',
    'angular',
    'vue',
    'nextjs',
    'terraform',
    'ansible',
    'library_generic',
    'other',
]);
exports.canonicalStackLabels = {
    node_typescript: 'TypeScript / Node.js',
    express: 'Express.js',
    nestjs: 'NestJS',
    java: 'Java',
    spring: 'Spring',
    fastapi: 'FastAPI',
    django: 'Django',
    flask: 'Flask',
    python: 'Python',
    php: 'PHP',
    laravel: 'Laravel',
    go: 'Go',
    dotnet: '.NET',
    ruby_rails: 'Ruby / Rails',
    kotlin: 'Kotlin',
    kotlin_spring: 'Kotlin / Spring',
    rust: 'Rust',
    react: 'React',
    angular: 'Angular',
    vue: 'Vue',
    nextjs: 'Next.js',
    terraform: 'Terraform (infrastructure context)',
    ansible: 'Ansible (infrastructure context)',
    library_generic: 'Generic Library',
    other: 'Other',
};
const legacyTechStackToCanonicalStack = {
    typescript_node: 'node_typescript',
    java: 'java',
    python: 'python',
    php: 'php',
    go: 'go',
    other: 'other',
};
function sortAndUniqueProjectKinds(kinds) {
    return [...new Set(kinds)].sort((left, right) => left.localeCompare(right));
}
function normalizeProjectKinds(value) {
    if (!Array.isArray(value)) {
        return undefined;
    }
    return sortAndUniqueProjectKinds(value.filter((entry) => validProjectKinds.has(entry)));
}
function normalizeStackSlots(value) {
    if (!value || typeof value !== 'object' || Array.isArray(value)) {
        return undefined;
    }
    const typed = value;
    const backendStack = typeof typed.backendStack === 'string' &&
        validCanonicalStackIds.has(typed.backendStack)
        ? typed.backendStack
        : undefined;
    const frontendStack = typeof typed.frontendStack === 'string' &&
        validCanonicalStackIds.has(typed.frontendStack)
        ? typed.frontendStack
        : undefined;
    const libraryStack = typeof typed.libraryStack === 'string' &&
        validCanonicalStackIds.has(typed.libraryStack)
        ? typed.libraryStack
        : undefined;
    const normalized = {
        ...(backendStack ? { backendStack } : {}),
        ...(frontendStack ? { frontendStack } : {}),
        ...(libraryStack ? { libraryStack } : {}),
    };
    return Object.keys(normalized).length > 0 ? normalized : undefined;
}
function mapInitTechStackToSlots(techStack, projectKinds) {
    if (!techStack) {
        return {};
    }
    const mappedStack = legacyTechStackToCanonicalStack[techStack];
    return {
        ...(projectKinds.includes('backend') ? { backendStack: mappedStack } : {}),
        ...(projectKinds.includes('library')
            ? { libraryStack: mappedStack === 'other' ? 'library_generic' : mappedStack }
            : {}),
        ...(projectKinds.includes('frontend')
            ? {
                frontendStack: mappedStack === 'node_typescript' ? 'react' : mappedStack,
            }
            : {}),
    };
}
function normalizeRepoTopology(value) {
    if (value === 'single_project' || value === 'monorepo') {
        return value;
    }
    return undefined;
}
function toProjectKindsFromRepositoryShape(repositoryShape) {
    if (repositoryShape === 'library') {
        return ['library'];
    }
    if (repositoryShape === 'monorepo') {
        return ['backend', 'frontend', 'library'];
    }
    return ['backend'];
}
function normalizeStackMetadataFromInitSelections(input) {
    const repoTopology = input.repositoryShape === 'monorepo' ? 'monorepo' : 'single_project';
    const projectKinds = toProjectKindsFromRepositoryShape(input.repositoryShape);
    const stacks = mapInitTechStackToSlots(input.techStack, projectKinds);
    return {
        repoTopology,
        projectKinds,
        stacks,
    };
}
function normalizeStackMetadata(value) {
    const typed = value && typeof value === 'object' && !Array.isArray(value)
        ? value
        : {};
    const repoTopology = normalizeRepoTopology(typed.repoTopology) ?? 'single_project';
    const projectKinds = normalizeProjectKinds(typed.projectKinds) ?? ['backend'];
    const stacks = normalizeStackSlots(typed.stacks) ?? {};
    return {
        repoTopology,
        projectKinds,
        stacks,
    };
}
//# sourceMappingURL=stackMetadata.js.map