const { mergeConfig } = require('@umijs/utils');
const { dirname } = require('path');
function toObject(obj) {
  return typeof obj === 'object' ? obj : {};
}
module.exports = opts => {
  const defaultEnvConfig = {
    exclude: [
      'transform-typeof-symbol',
      'transform-unicode-regex',
      'transform-sticky-regex',
      'transform-new-target',
      'transform-modules-umd',
      'transform-modules-systemjs',
      'transform-modules-amd',
      'transform-literals',
    ],
  };

  const preset = {
    presets: [
      opts.env && [
        '@babel/preset-env',
        {
          ...mergeConfig(defaultEnvConfig,{}),
          modules: false,
          ...opts.env
        },
      ],
      // opts.react && ['@babel/preset-react'],
      // opts.typescript && [
      //   '@babel/preset-typescript',
      //   {
      //     allowNamespaces: true,
      //   },
      // ],
    ].filter(Boolean),
    plugins: [
      ['@babel/plugin-proposal-optional-chaining', { loose: false }],
      ['@babel/plugin-proposal-nullish-coalescing-operator', { loose: false }],
      '@babel/plugin-syntax-top-level-await',
      ['@babel/plugin-transform-destructuring', { loose: false }],
      // opts.typescript && ['babel-plugin-transform-typescript-metadata'],
      ['@babel/plugin-proposal-decorators', { legacy: true }],
      ['@babel/plugin-proposal-class-properties', { loose: true }],
      '@babel/plugin-transform-flow-strip-types',
      '@babel/plugin-proposal-export-default-from',
      [
        '@babel/plugin-proposal-pipeline-operator',
        {
          proposal: 'minimal',
        },
      ],
      'inline-react-svg',
      '@babel/plugin-proposal-do-expressions',
      '@babel/plugin-proposal-function-bind',
      '@babel/plugin-proposal-logical-assignment-operators',
      '@babel/plugin-syntax-dynamic-import',
      '@babel/plugin-transform-react-jsx',
      opts.transformRuntime && [
        '@babel/plugin-transform-runtime',
        {
          version: require('@babel/runtime/package.json').version,
          absoluteRuntime: dirname(
            require.resolve('@babel/runtime/package.json'),
          ),
          useESModules: true,
          ...toObject(opts.transformRuntime),
        },
      ],
      [
        'babel-plugin-transform-react-remove-prop-types',
        {
          removeImport: true,
        },
      ],
      opts.reactRequire && ['babel-plugin-react-require'],
      opts.dynamicImportNode && ['babel-plugin-dynamic-import-node'],
      opts.autoCSSModules && ['@umijs/babel-plugin-auto-css-modules'],
      opts.svgr && [
        'babel-plugin-named-asset-import',
        {
          loaderMap: {
            svg: {
              ReactComponent: `${require.resolve(
                '@svgr/webpack',
              )}?-svgo,+titleProp,+ref![path]`,
            },
          },
        },
      ],
      ...(opts.import
        ? opts.import.map(importOpts => {
            return ['babel-plugin-import', importOpts, importOpts.libraryName];
          })
        : []),
      opts.importToAwaitRequire && [
        '@umijs/babel-plugin-import-to-await-require',
        opts.importToAwaitRequire,
      ],
    //   opts.lockCoreJS3 && [
    //     '@umijs/babel-plugin-lock-core-js-3',
    //     opts.lockCoreJS3,
    //   ],
    ].filter(Boolean),
    env: {
      production: {
        only: ['src'],
        plugins: [
          'lodash',
          'transform-react-remove-prop-types',
          '@babel/plugin-transform-react-inline-elements',
          '@babel/plugin-transform-react-constant-elements',
        ],
      },
      development: {
        plugins: ['react-refresh/babel'],
      },
      test: {
        plugins: [
          '@babel/plugin-transform-modules-commonjs',
          'dynamic-import-node',
        ],
      },
    },
  };
  return opts.modify ? opts.modify(preset) : preset;
};
