"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = update;
const devkit_1 = require("@nx/devkit");
const AUDIT_STEP = `      - name: Security Audit
        run: npx unisphere tools audit
        env:
          KALTURA_JFROG_TOKEN: \${{ steps.setup-jfrog.outputs.oidc-token }}`;
async function update(tree) {
    const cicdPath = '.github/workflows/cicd.yml';
    if (!tree.exists(cicdPath)) {
        devkit_1.logger.warn('⚠️ No cicd.yml found, skipping');
        return;
    }
    let content = tree.read(cicdPath, 'utf-8');
    if (!content) {
        devkit_1.logger.warn('⚠️ Could not read cicd.yml, skipping');
        return;
    }
    const commentedPattern = /^[ \t]*#\s*- name: Security Audit\s*\n(?:[ \t]*#.*\n)*/m;
    if (commentedPattern.test(content)) {
        content = content.replace(commentedPattern, AUDIT_STEP + '\n');
        tree.write(cicdPath, content);
        devkit_1.logger.info('✅ Uncommented Security Audit step in cicd.yml');
        return;
    }
    if (content.includes('Security Audit')) {
        devkit_1.logger.info('ℹ️ Security Audit step already exists in cicd.yml');
        return;
    }
    const installDepsMatch = content.match(/( *- name: Install Dependencies[ \t]*\n(?:.*\n)*?)([ \t]*- name: )/);
    if (!installDepsMatch) {
        devkit_1.logger.warn('⚠️ Could not find Install Dependencies step in cicd.yml');
        throw new Error('Could not find Install Dependencies step in cicd.yml');
    }
    const insertIndex = installDepsMatch.index + installDepsMatch[1].length;
    content =
        content.slice(0, insertIndex) +
            '\n' +
            AUDIT_STEP +
            '\n\n' +
            content.slice(insertIndex);
    tree.write(cicdPath, content);
    devkit_1.logger.info('✅ Added Security Audit step to cicd.yml');
}
