"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runBuilder = void 0;
const architect_1 = require("@angular-devkit/architect");
const build_angular_1 = require("@angular-devkit/build-angular");
const static_injector_1 = require("static-injector");
const platform_inject_config_1 = require("../platform/platform-inject-config");
const webpack_configuration_change_service_1 = require("./webpack-configuration-change.service");
exports.default = (0, architect_1.createBuilder)((angularOptions, context) => {
    return runBuilder(angularOptions, context);
});
function runBuilder(angularOptions, context) {
    return (0, build_angular_1.executeBrowserBuilder)(angularOptions, context, {
        webpackConfiguration: (options) => __awaiter(this, void 0, void 0, function* () {
            const injector = static_injector_1.Injector.create({
                providers: [
                    ...(0, platform_inject_config_1.getBuildPlatformInjectConfig)(angularOptions.platform),
                    {
                        provide: webpack_configuration_change_service_1.WebpackConfigurationChangeService,
                        useFactory: (injector) => {
                            return new webpack_configuration_change_service_1.WebpackConfigurationChangeService(angularOptions, context, options, injector);
                        },
                        deps: [static_injector_1.Injector],
                    },
                ],
            });
            const config = injector.get(webpack_configuration_change_service_1.WebpackConfigurationChangeService);
            config.init();
            yield config.change();
            return options;
        }),
    });
}
exports.runBuilder = runBuilder;
