text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  const core_1 = require("@angular-devkit/core");
  const rxjs_1 = require("rxjs");
  const operators_1 = require("rxjs/operators");
  class CordovaBuildBuilder {
      constructor(context) {
          this.context = context;
      }
      run(builderConfig) {
          const [project, target, configuration] = builderConfig.options.browserTarget.split(':');
          const browserTargetSpec = { project, target, configuration, overrides: {} };
          let browserConfig = this.context.architect.getBuilderConfiguration(browserTargetSpec);
          let browserDescription;
          return rxjs_1.of(null).pipe(operators_1.concatMap(() => this.context.architect.getBuilderDescription(browserConfig)), operators_1.tap(description => browserDescription = description), operators_1.concatMap(() => this.context.architect.validateBuilderOptions(browserConfig, browserDescription)), operators_1.tap(config => browserConfig = config), operators_1.tap(() => this.prepareBrowserConfig(builderConfig.options, browserConfig.options)), operators_1.concatMap(() => rxjs_1.of(this.context.architect.getBuilder(browserDescription, this.context))), operators_1.concatMap(builder => builder.run(browserConfig)));
      }
      // Mutates browserOptions
      prepareBrowserConfig(options, browserOptions) {
          const cordovaBasePath = core_1.normalize(options.cordovaBasePath ? options.cordovaBasePath : '.');
          // We always need to output the build to `www` because it is a hard
          // requirement of Cordova.
          browserOptions.outputPath = core_1.join(cordovaBasePath, core_1.normalize('www'));
          // Cordova CLI will error if `www` is missing. The Angular CLI deletes it
          // by default. Let's keep it around.
          browserOptions.deleteOutputPath = false;
          if (options.cordovaAssets) {
              const platformWWWPath = core_1.join(cordovaBasePath, core_1.normalize(`platforms/${options.platform}/platform_www`));
              // Add Cordova www assets that were generated whenever platform(s) and
              // plugin(s) are added. This includes `cordova.js`,
              // `cordova_plugins.js`, and all plugin JS.
              browserOptions.assets.push({
                  glob: '**/*',
                  input: core_1.getSystemPath(platformWWWPath),
                  output: './',
              });
              // Register `cordova.js` as a global script so it is included in
              // `index.html`.
              browserOptions.scripts.push({
                  input: core_1.getSystemPath(core_1.join(platformWWWPath, core_1.normalize('cordova.js'))),
                  bundleName: 'cordova',
                  lazy: false,
              });
          }
      }
  }
  exports.CordovaBuildBuilder = CordovaBuildBuilder;
  exports.default = CordovaBuildBuilder;
  