// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path')

module.exports = {
  template: componentTemplate,
  indexTemplate: indexTemplate,
  typescript: true,
  memo: true,
  ref: true,
  prettier: true,
  jsxRuntime: 'automatic',
  svgoConfig: {
    plugins: [
      {
        name: 'preset-default',
        params: {
          overrides: {
            removeViewBox: false,
            cleanupIDs: false, // Figma already assigns unique ID for every SVG. With this option enabled, the IDs are no longer globally unique
          },
        },
      },
    ],
  },
}

const comment = '// THIS FILE WAS AUTOGENERATED BY SVGR. DO NOT MODIFY IT MANUALLY'

function componentTemplate(variables, { tpl }) {
  return tpl`
${comment}

${variables.imports}

${variables.interfaces}

const ${variables.componentName} = forwardRef((${variables.props}) => (
  ${variables.jsx}
))

${variables.componentName}.displayName = '${variables.componentName}'
const Memo = memo(${variables.componentName})
export { Memo as ${variables.componentName} }
`
}

function indexTemplate(filePaths) {
  const exportEntries = filePaths.map((filePath) => {
    const basename = path.basename(filePath, path.extname(filePath))
    return `export * from './${basename}'`
  })
  return comment + '\n' + exportEntries.join('\n') + '\n'
}
