import path from 'path'
import fs from 'fs'

import { registerComponent } from '../components'

export function resolveComponentPath(compPath, componentRootPath) {
  if (!compPath) {
    return null
  } else if (compPath.startsWith('.') || path.isAbsolute(compPath)) {
    return path.resolve(componentRootPath, compPath)
  }
  return require.resolve(compPath)
}

export function readMjmlConfig(configPathOrDir = process.cwd()) {
  let componentRootPath = process.cwd()
  let mjmlConfigPath = configPathOrDir
  try {
    mjmlConfigPath = path.basename(configPathOrDir) === '.mjmlconfig'
      ? path.resolve(configPathOrDir)
      : path.resolve(configPathOrDir, '.mjmlconfig')
    componentRootPath = path.dirname(mjmlConfigPath)
    const mjmlConfig = JSON.parse(fs.readFileSync(path.resolve(mjmlConfigPath), 'utf8'))
    return { mjmlConfig, componentRootPath }
  } catch (e) {
    if (e.code !== 'ENOENT') {
      console.log('Error reading mjmlconfig : ', e) // eslint-disable-line no-console
    }
    return { mjmlConfig: { packages: [] }, mjmlConfigPath, componentRootPath }
  }
}

export default function registerCustomComponents(configPathOrDir = process.cwd(), registerCompFn = registerComponent) {
  const { mjmlConfig: { packages }, componentRootPath } = readMjmlConfig(configPathOrDir)
  packages.forEach(compPath => {
    const resolvedPath = resolveComponentPath(compPath, componentRootPath)
    if (resolvedPath) {
      try {
        const requiredComp = require(resolvedPath) // eslint-disable-line global-require, import/no-dynamic-require
        registerCompFn(requiredComp.default || requiredComp)
      } catch (e) {
        if (e.code !== 'ENOENT') {
          console.log('Error when registering custom component : ', resolvedPath, e) // eslint-disable-line no-console
        } else {
          console.log('Missing or unreadable custom component : ', resolvedPath) // eslint-disable-line no-console
        }
      }
    }
  })
}
@@ -93,276 +93,8 @@
 s'%0A%0A
-export function resolveComponentPath(compPath, componentRootPath) %7B%0A  if (!compPath) %7B%0A    return null%0A  %7D else if (compPath.startsWith('.') %7C%7C path.isAbsolute(compPath)) %7B%0A    return path.resolve(componentRootPath, compPath)%0A  %7D%0A  return require.resolve(compPath)%0A%7D%0A%0A
 expo
@@ -155,16 +155,16 @@
 wd()) %7B%0A
+
   let co
@@ -815,16 +815,1055 @@
 %0A  %7D%0A%7D%0A%0A
+%0Aexport function resolveComponentPath(compPath, componentRootPath) %7B%0A  if (!compPath) %7B%0A    return null%0A  %7D%0A  if (!compPath.startsWith('.') && !path.isAbsolute(compPath)) %7B%0A    try %7B%0A      return require.resolve(compPath)%0A    %7D catch (e) %7B%0A      if (e.code !== 'MODULE_NOT_FOUND') %7B%0A        console.log('Error resolving custom component path : ', e) // eslint-disable-line no-console%0A        return null%0A      %7D%0A      // if we get a 'MODULE_NOT_FOUND' error try again with relative path:%0A      return resolveComponentPath(%60./$%7BcompPath%7D%60, componentRootPath)%0A    %7D%0A  %7D%0A  return require.resolve(path.resolve(componentRootPath, compPath))%0A%7D%0A%0Aexport function registerCustomComponent(comp, registerCompFn = registerComponent) %7B%0A  if (comp instanceof Function) %7B%0A    registerCompFn(comp)%0A  %7D else %7B%0A    const compNames = Object.keys(comp) // this approach handles both an array and an object (like the mjml-accordion default export)%0A    compNames.forEach(compName =%3E %7B%0A      registerCustomComponent(comp%5BcompName%5D, registerCompFn)%0A    %7D)%0A  %7D%0A%7D%0A%0A
 export d
@@ -2341,21 +2341,30 @@
 egisterC
-ompFn
+ustomComponent
 (require
@@ -2392,16 +2392,32 @@
 iredComp
+, registerCompFn
 )%0A      

handle multi-component objects and ambiguous package paths