"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.UIROOT_ASPECT_IDS = exports.BundleUiTask = exports.BUNDLE_UI_TASK_NAME = exports.BUNDLE_UI_HASH_FILENAME = exports.BUNDLE_UI_DIR = exports.BUNDLE_UIROOT_DIR = void 0;
function _path() {
  const data = require("path");
  _path = function () {
    return data;
  };
  return data;
}
function _fs() {
  const data = require("fs");
  _fs = function () {
    return data;
  };
  return data;
}
function _ui() {
  const data = require("@teambit/ui");
  _ui = function () {
    return data;
  };
  return data;
}
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
const BUNDLE_UI_TASK_NAME = exports.BUNDLE_UI_TASK_NAME = 'BundleUI';
const BUNDLE_UI_DIR = exports.BUNDLE_UI_DIR = 'ui-bundle';
const UIROOT_ASPECT_IDS = exports.UIROOT_ASPECT_IDS = {
  SCOPE: 'teambit.scope/scope',
  WORKSPACE: 'teambit.workspace/workspace'
};
const BUNDLE_UIROOT_DIR = exports.BUNDLE_UIROOT_DIR = {
  [UIROOT_ASPECT_IDS.SCOPE]: 'scope',
  [UIROOT_ASPECT_IDS.WORKSPACE]: 'workspace'
};
const BUNDLE_UI_HASH_FILENAME = exports.BUNDLE_UI_HASH_FILENAME = '.hash';
class BundleUiTask {
  constructor(ui, logger) {
    this.ui = ui;
    this.logger = logger;
    _defineProperty(this, "aspectId", 'teambit.ui-foundation/ui');
    _defineProperty(this, "name", BUNDLE_UI_TASK_NAME);
    _defineProperty(this, "location", 'end');
  }
  async execute(context) {
    const capsule = context.capsuleNetwork.seedersCapsules.find(c => c.component.id.toStringWithoutVersion() === _ui().UIAspect.id);
    if (!capsule) {
      return {
        componentsResults: []
      };
    }
    try {
      await Promise.all(Object.values(UIROOT_ASPECT_IDS).map(async uiRootAspectId => {
        const outputPath = (0, _path().join)(capsule.path, BundleUiTask.getArtifactDirectory(uiRootAspectId));
        this.logger.info(`Generating UI bundle at ${outputPath}...`);
        await this.ui.build(uiRootAspectId, outputPath);
        await this.generateHash(outputPath);
      }));
    } catch (error) {
      this.logger.error('Generating UI bundle failed', error);
      throw new Error('Generating UI bundle failed');
    }
    const artifacts = BundleUiTask.getArtifactDef();
    return {
      componentsResults: [],
      artifacts
    };
  }
  async generateHash(outputPath) {
    const maybeUiRoot = this.ui.getUi();
    if (!maybeUiRoot) throw new Error('no uiRoot found');
    const [, uiRoot] = maybeUiRoot;
    const hash = await this.ui.createBundleUiHash(uiRoot);
    if (!(0, _fs().existsSync)(outputPath)) (0, _fs().mkdirSync)(outputPath);
    (0, _fs().writeFileSync)((0, _path().join)(outputPath, BUNDLE_UI_HASH_FILENAME), hash);
  }
  static getArtifactDirectory(uiRootAspectId) {
    return (0, _path().join)('artifacts', BUNDLE_UI_DIR, BUNDLE_UIROOT_DIR[uiRootAspectId]);
  }
  static getArtifactDef() {
    const scopeRootDir = BundleUiTask.getArtifactDirectory(UIROOT_ASPECT_IDS.SCOPE);
    const workspaceRootDir = BundleUiTask.getArtifactDirectory(UIROOT_ASPECT_IDS.WORKSPACE);
    return [{
      name: `${BUNDLE_UI_DIR}-${BUNDLE_UIROOT_DIR[UIROOT_ASPECT_IDS.SCOPE]}`,
      globPatterns: [`${scopeRootDir}/**`]
    }, {
      name: `${BUNDLE_UI_DIR}-${BUNDLE_UIROOT_DIR[UIROOT_ASPECT_IDS.WORKSPACE]}`,
      globPatterns: [`${workspaceRootDir}/**`]
    }];
  }
}
exports.BundleUiTask = BundleUiTask;

//# sourceMappingURL=bundle-ui.task.js.map