import fs from 'node:fs';
/**
 * Add Zenodo ID to project identifiers in config file
 *
 * This function assumes there is no zenodo identifier present
 */
export function addZenodoToConfig(configFile, zenodoId, sandbox) {
    if (!configFile)
        return;
    const file = fs.readFileSync(configFile).toString();
    const lines = file.split('\n');
    const identifiersIndex = lines.findIndex((line) => line.trim() === 'identifiers:');
    let newLines;
    if (identifiersIndex !== -1) {
        newLines = [
            ...lines.slice(0, identifiersIndex + 1),
            `    zenodo: https://${sandbox ? 'sandbox.' : ''}zenodo.org/deposit/${zenodoId}`,
            ...lines.slice(identifiersIndex + 1),
        ];
    }
    else {
        const projectIndex = lines.findIndex((line) => line.trim() === 'project:');
        newLines = [
            ...lines.slice(0, projectIndex + 1),
            `  identifiers:`,
            `    zenodo: https://${sandbox ? 'sandbox.' : ''}zenodo.org/deposit/${zenodoId}`,
            ...lines.slice(projectIndex + 1),
        ];
    }
    fs.writeFileSync(configFile, newLines.join('\n'));
}
/**
 * Add DOI to project in config file
 *
 * This function assumes there is no DOI present
 */
export function addDoiToConfig(configFile, doi) {
    if (!configFile)
        return;
    const file = fs.readFileSync(configFile).toString();
    const lines = file.split('\n');
    const projectIndex = lines.findIndex((line) => line.trim() === 'project:');
    const newLines = [
        ...lines.slice(0, projectIndex + 1),
        `  doi: ${doi}`,
        ...lines.slice(projectIndex + 1),
    ];
    fs.writeFileSync(configFile, newLines.join('\n'));
}
