import faker from 'faker'
import _ from 'lodash'
import PropTypes from 'prop-types'
import React from 'react'
import SourceRender from 'react-source-render'
import * as SUIR from 'semantic-ui-react'

import { updateForKeys } from 'docs/src/hoc'
import * as selectCommon from 'docs/src/examples/addons/Select/common'
import Wireframe from 'docs/src/examples/behaviors/Visibility/Wireframe'
import * as dropdownCommon from 'docs/src/examples/modules/Dropdown/common'

const babelConfig = {
  presets: [
    [
      'env',
      {
        targets: {
          browsers: ['last 4 versions', 'not dead'],
        },
      },
    ],
    ['stage-1', { decoratorsLegacy: true }],
  ],
}

const externals = {
  faker,
  lodash: require('lodash'),
  react: React,
  'prop-types': PropTypes,
  'semantic-ui-react': SUIR,
}
const commons = {
  Dropdown: dropdownCommon,
  Select: selectCommon,
}

const resolver = (importPath, { displayName }) => {
  if (externals[importPath]) return externals[importPath]
  if (commons[displayName] && _.endsWith(importPath, '/common')) return commons[displayName]
  if (_.endsWith(importPath, '/Wireframe')) return Wireframe

  throw new Error(
    [
      'An error in resolver(), please check that import is defined, details:',
      JSON.stringify({ displayName, importPath }),
    ].join('\n'),
  )
}

const ComponentExampleRenderSource = ({ displayName, onError, onSuccess, sourceCode }) => (
  <SourceRender
    babelConfig={babelConfig}
    displayName={displayName}
    onError={onError}
    onSuccess={onSuccess}
    resolver={resolver}
    source={sourceCode}
  />
)

ComponentExampleRenderSource.propTypes = {
  displayName: PropTypes.string.isRequired,
  onError: PropTypes.func.isRequired,
  onSuccess: PropTypes.func.isRequired,
  sourceCode: PropTypes.string.isRequired,
}

export default updateForKeys(['sourceCode'])(ComponentExampleRenderSource)

@@ -457,16 +457,210 @@
 /common'
+%0Aimport SearchExampleStandard from 'docs/src/examples/modules/Search/Types/SearchExampleStandard'%0Aimport SearchExampleCategory from 'docs/src/examples/modules/Search/Types/SearchExampleCategory'
 %0A%0Aconst 
@@ -1275,16 +1275,17 @@
 ayName%5D%0A
+%0A
   if (_.
@@ -1336,16 +1336,186 @@
 ireframe
+%0A  if (_.endsWith(importPath, '/SearchExampleCategory')) return SearchExampleCategory%0A  if (_.endsWith(importPath, '/SearchExampleStandard')) return SearchExampleStandard
 %0A%0A  thro

fix resolver imports (#3213)