"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AspectEnvType = exports.AspectEnv = void 0;
function _defender() {
  const data = require("@teambit/defender.jest-tester");
  _defender = function () {
    return data;
  };
  return data;
}
function _toolboxPath() {
  const data = require("@teambit/toolbox.path.path");
  _toolboxPath = function () {
    return data;
  };
  return data;
}
function _compilation() {
  const data = require("@teambit/compilation.babel-compiler");
  _compilation = function () {
    return data;
  };
  return data;
}
function _lodash() {
  const data = require("lodash");
  _lodash = function () {
    return data;
  };
  return data;
}
function _builder() {
  const data = require("@teambit/builder");
  _builder = function () {
    return data;
  };
  return data;
}
function _preview() {
  const data = require("@teambit/preview");
  _preview = function () {
    return data;
  };
  return data;
}
function _defender2() {
  const data = require("@teambit/defender.prettier-formatter");
  _defender2 = function () {
    return data;
  };
  return data;
}
function _typescript() {
  const data = require("@teambit/typescript.typescript-compiler");
  _typescript = function () {
    return data;
  };
  return data;
}
function _defender3() {
  const data = require("@teambit/defender.eslint-linter");
  _defender3 = function () {
    return data;
  };
  return data;
}
function _path() {
  const data = require("path");
  _path = function () {
    return data;
  };
  return data;
}
function _babelConfig() {
  const data = require("./babel/babel-config");
  _babelConfig = function () {
    return data;
  };
  return data;
}
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
const tsconfig = require('./typescript/tsconfig.json');
const AspectEnvType = exports.AspectEnvType = 'aspect';
/**
 * a component environment built for Aspects .
 */
class AspectEnv {
  constructor(reactEnv, aspectLoader, devFiles, compiler, worker, logger) {
    this.reactEnv = reactEnv;
    this.aspectLoader = aspectLoader;
    this.devFiles = devFiles;
    this.compiler = compiler;
    this.worker = worker;
    this.logger = logger;
    _defineProperty(this, "icon", 'https://static.bit.dev/extensions-icons/default.svg');
  }
  async __getDescriptor() {
    return {
      type: AspectEnvType
    };
  }
  getTsConfig(tsConfig) {
    const targetConf = (0, _lodash().merge)(tsconfig, tsConfig);
    return targetConf;
  }

  // TODO: should probably use the transformer from the main runtime?
  // TODO: this doesn't seems to work as expected, the getTsConfig is not a transformer and the react env API expect a transformers array not an object
  createTsCompiler(tsConfig) {
    return this.reactEnv.getTsCjsCompiler(this.getTsConfig(tsConfig));
  }
  getCompiler() {
    return this.getBabelCompiler();
  }
  getBabelCompiler() {
    const options = {
      babelTransformOptions: _babelConfig().babelConfig,
      distDir: 'dist',
      distGlobPatterns: [`dist/**`, `!dist/**/*.d.ts`, `!dist/tsconfig.tsbuildinfo`]
    };
    const babelCompiler = _compilation().BabelCompiler.create(options, {
      logger: this.logger
    });
    return babelCompiler;
  }

  /**
   * returns a component tester.
   */
  getTester(jestConfigPath, jestModulePath) {
    const config = jestConfigPath || require.resolve('./jest/jest.config');
    return this.reactEnv.createCjsJestTester(config, jestModulePath);
  }

  /**
   * returns the component build pipeline.
   */
  getBuildPipe(modifiers = {}) {
    const transformer = tsConfigMutator => {
      tsConfigMutator.mergeTsConfig(tsconfig).setArtifactName('declaration').setDistGlobPatterns([`dist/**/*.d.ts`]).setShouldCopyNonSupportedFiles(false);
      return tsConfigMutator;
    };
    // @ts-ignore
    const externalTransformer = modifiers?.tsModifier?.transformers || [];
    const tsCompilerTask = this.reactEnv.getCjsCompilerTask([transformer, ...externalTransformer]);
    const babelCompiler = this.getBabelCompiler();
    const babelCompilerTask = this.compiler.createTask('BabelCompiler', babelCompiler);
    const jestTesterTask = this.getJestTesterTask();
    return [babelCompilerTask, tsCompilerTask, jestTesterTask];
  }
  getJestTesterTask(jestModifier = {}) {
    const pathToSource = (0, _toolboxPath().pathNormalizeToLinux)(__dirname).replace('/dist', '');
    const jestConfigPath = jestModifier?.transformers?.[0]() || (0, _path().join)(pathToSource, './jest/jest.config.js');
    const jestPath = jestModifier?.module || require.resolve('jest');
    const worker = this.getJestWorker();
    const testerTask = _defender().JestTask.create({
      config: jestConfigPath,
      jest: jestPath
    }, {
      logger: this.logger,
      worker,
      devFiles: this.devFiles
    });
    return testerTask;
  }
  getJestWorker() {
    return this.worker.declareWorker('jest', _defender().jestWorkerPath);
  }
  async getTemplateBundler(context, transformers = []) {
    return this.createTemplateWebpackBundler(context, transformers);
  }
  async createTemplateWebpackBundler(context, transformers = []) {
    return this.reactEnv.createTemplateWebpackBundler(context, transformers);
  }
  getPackageJsonProps() {
    return _objectSpread(_objectSpread({}, this.reactEnv.getCjsPackageJsonProps()), {}, {
      exports: {
        node: {
          require: './dist/{main}.js',
          import: './dist/esm.mjs'
        },
        default: './dist/{main}.js'
      }
    });
  }
  getNpmIgnore() {
    return [`${_builder().CAPSULE_ARTIFACTS_DIR}/*`];
  }
  getPreviewConfig() {
    return {
      strategyName: _preview().COMPONENT_PREVIEW_STRATEGY_NAME,
      splitComponentBundle: false
    };
  }
  async getDependencies() {
    return {
      dependencies: {
        react: '-',
        'react-dom': '-',
        'core-js': '^3.0.0',
        // For aspects the babel runtime should be a runtime dep not only dev as they are compiled by babel
        '@babel/runtime': '7.20.0'
      },
      // TODO: add this only if using ts
      devDependencies: {
        react: '-',
        'react-dom': '-',
        '@types/mocha': '-',
        '@types/node': '12.20.4',
        '@types/react': '^17.0.8',
        '@types/react-dom': '^17.0.5',
        '@types/jest': '^26.0.0',
        '@types/testing-library__jest-dom': '5.9.5'
      },
      peerDependencies: {
        // TODO: check if we really need react for aspects (maybe for ink support)
        react: '^16.8.0 || ^17.0.0',
        'react-dom': '^16.8.0 || ^17.0.0'
      }
    };
  }
  workspaceConfig() {
    return [_typescript().TypescriptConfigWriter.create({
      tsconfig: require.resolve('./typescript/tsconfig.json')
      // types: resolveTypes(__dirname, ["./types"]),
    }, this.logger), _defender3().EslintConfigWriter.create({
      configPath: require.resolve('./eslint/eslintrc.js'),
      tsconfig: require.resolve('./typescript/tsconfig.json')
    }, this.logger), _defender2().PrettierConfigWriter.create({
      configPath: require.resolve('./prettier/prettier.config.js')
    }, this.logger)];
  }
}
exports.AspectEnv = AspectEnv;

//# sourceMappingURL=aspect.env.js.map