"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ensureAngularJsonStyle = ensureAngularJsonStyle;
const json_file_1 = require("@schematics/angular/utility/json-file");
const BROWSER_BUILDERS = [
    '@angular-devkit/build-angular:browser',
    '@angular-devkit/build-angular:application',
    '@angular-builders/custom-webpack:browser',
];
/**
 * Adds a CSS entry to the `styles` array of all browser build targets in angular.json.
 * No-op if the entry already exists or if angular.json is not found.
 */
function ensureAngularJsonStyle(tree, style, context) {
    if (!tree.exists('/angular.json'))
        return;
    const json = new json_file_1.JSONFile(tree, '/angular.json');
    const projects = json.get(['projects']);
    if (!projects)
        return;
    for (const projectName of Object.keys(projects)) {
        const builderPath = ['projects', projectName, 'architect', 'build', 'builder'];
        const builder = json.get(builderPath);
        if (!builder || !BROWSER_BUILDERS.some((b) => builder.includes(b)))
            continue;
        const stylesPath = ['projects', projectName, 'architect', 'build', 'options', 'styles'];
        const styles = json.get(stylesPath);
        if (!styles)
            continue;
        const alreadyPresent = styles.some((s) => s === style || (typeof s === 'object' && s.input === style));
        if (alreadyPresent)
            continue;
        json.modify([...stylesPath, styles.length], style);
        context.logger.info(`${projectName}: adicionado "${style}" ao array styles do angular.json.`);
    }
}
//# sourceMappingURL=workspace-utils.js.map