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 = {
    compact: false,
    presets: [
      opts.env && [
        // eslint-disable-next-line global-require
        require('@umijs/deps/compiled/babel/preset-env'),
        {
          ...mergeConfig(defaultEnvConfig, {}),
          modules: false,
          ...opts.env,
        },
      ],
    ].filter(Boolean),
    plugins: [
      ['@babel/plugin-proposal-optional-chaining', { loose: false }],
      ['@babel/plugin-proposal-nullish-coalescing-operator', { loose: false }],
      require('@umijs/deps/compiled/babel/plugin-syntax-top-level-await')
        .default,
      ['@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 && [
        require('@umijs/deps/compiled/babel/plugin-transform-runtime').default,
        {
          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 => [
            'babel-plugin-import',
            importOpts,
            importOpts.libraryName,
          ])
        : []),
      opts.importToAwaitRequire && [
        '@umijs/babel-plugin-import-to-await-require',
        opts.importToAwaitRequire,
      ],
      'react-activation/babel',
      opts.lockCoreJS3 && [
        '@umijs/babel-plugin-lock-core-js-3',
        opts.lockCoreJS3,
      ],
      opts.noAnonymousDefaultExport && [
        require.resolve('@umijs/babel-plugin-no-anonymous-default-export'),
      ],
      [
        require('@umijs/deps/compiled/babel/plugin-proposal-record-and-tuple')
          .default,
        {
          syntaxType: 'hash',
          polyfillModuleName: '@umijs/deps/reexported/record-tuple-polyfill',
          importPolyfill: true,
        },
      ],
    ].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;
};