import { removeFieldsWithNullishValues } from '../../base/support/index.js';
import { GENERATOR_JHIPSTER } from '../../generator-constants.js';
export const extractDataFromInfo = (info) => {
    const regexp = /<summary>(?<title>(?:(?!<\/summary>).)+)<\/summary>\s+<pre>(?<body>(?:(?!<\/pre>).)+)/gs;
    let yoRcContent;
    let jdlEntitiesDefinitions;
    let jdlDefinitions;
    let jdlApplications;
    let workspacesFolders;
    const files = [];
    for (const match of info.matchAll(regexp)) {
        const { title, body } = match.groups ?? {};
        if (title?.trim() && body?.trim()) {
            if (title.includes('.yo-rc.json file')) {
                if (title.includes(' for ')) {
                    const folder = title.split(' for ')[1].trim();
                    files.push({ filename: `${folder}/.yo-rc.json`, content: body.trim(), type: 'yo-rc' });
                }
                else {
                    yoRcContent = body.trim();
                    files.push({ filename: '.yo-rc.json', content: yoRcContent, type: 'yo-rc' });
                }
            }
            else if (title.includes(' file')) {
                files.push({ filename: title.split(' file')[0].trim(), content: body.trim(), type: 'json' });
            }
            else if (title.includes('JDL entity definitions')) {
                jdlEntitiesDefinitions = body.trim();
                files.push({ filename: 'entities.jdl', content: jdlEntitiesDefinitions, type: 'entity-jdl' });
            }
            else if (title.includes('JDL definitions')) {
                if ((body.match(/application\s*\{/g) || []).length > 0) {
                    const jdlCount = files.filter(file => file.type === 'jdl').length;
                    files.push({ filename: jdlCount === 0 ? 'app.jdl' : `app-${jdlCount}.jdl`, content: body.trim(), type: 'jdl' });
                    jdlApplications ??= (body.match(/application\s*\{/g) || []).length;
                    jdlDefinitions ??= body.trim();
                }
            }
        }
    }
    let yoRcBlank = true;
    let yoRcValid;
    if (yoRcContent) {
        yoRcBlank = false;
        try {
            let content = JSON.parse(yoRcContent);
            content = GENERATOR_JHIPSTER in content ? content : { [GENERATOR_JHIPSTER]: content };
            workspacesFolders = content[GENERATOR_JHIPSTER].appsFolders;
            yoRcContent = JSON.stringify(content);
            yoRcValid = true;
        }
        catch (error) {
            yoRcValid = false;
            console.log('Invalid .yo-rc.json file', error);
        }
    }
    if (jdlDefinitions) {
        yoRcContent = undefined;
        jdlEntitiesDefinitions = undefined;
    }
    return removeFieldsWithNullishValues({
        yoRcContent,
        jdlEntitiesDefinitions,
        jdlDefinitions,
        yoRcBlank,
        yoRcValid,
        files,
        workspacesFolders,
        jdlApplications,
    });
};
