#!/usr/bin/env node
const iconComponents = require('../../octicons_react')
const fse = require('fs-extra')
const {join, resolve} = require('path')

const srcDir = resolve(__dirname, '../src/__generated__')
const iconsFile = join(srcDir, 'index.js')
const typesFile = join(srcDir, 'index.d.ts')

const GENERATED_HEADER = '/* THIS FILE IS GENERATED. DO NOT EDIT IT. */'

const icons = Object.keys(iconComponents)
  .filter(name => name !== 'default')
  .map(name => {
    const code = `export const ${name} = styled(Icon.${name})(COMMON)
${name}.propTypes = COMMON.propTypes
`
    return {name, code}
  })
  .sort((a, b) => a.name.localeCompare(b.name))

function writeIcons(file) {
  const count = icons.length
  const code = `${GENERATED_HEADER}
import styled from 'styled-components'
import * as styledSystem from 'styled-system'
import systemPropTypes from '@styled-system/prop-types'
import * as Icon from '../../../octicons_react/dist/index.esm'

const COMMON = styledSystem.compose(styledSystem.space, styledSystem.color)

COMMON.propTypes = {
  ...systemPropTypes.space,
  ...systemPropTypes.color
}

${icons.map(({code}) => code).join('\n')}`

  return fse.writeFile(file, code, 'utf8').then(() => {
    console.warn('wrote %s with %d exports', file, count)
    return icons
  })
}

function writeTypes(file) {
  const count = icons.length
  const code = `${GENERATED_HEADER}
import * as React from 'react'
import * as StyledSystem from 'styled-system'

type Size = 'small' | 'medium' | 'large'

interface IconProps extends StyledSystem.ColorProps, StyledSystem.SpaceProps {
  'aria-label'?: string
  className?: string
  size?: number | Size
  verticalAlign?: 'middle' | 'text-bottom' | 'text-top' | 'top' | 'unset'
}

type Icon = React.FC<IconProps>

${icons.map(({name}) => `declare const ${name}: Icon`).join('\n')}

export {
  Icon,
  IconProps,
  ${icons.map(({name}) => name).join(',\n  ')}
}`

  return fse.writeFile(file, code, 'utf8').then(() => {
    console.warn('wrote %s with %d exports', file, count)
    return icons
  })
}

fse
  .mkdirs(srcDir)
  .then(() => writeIcons(iconsFile))
  .then(() => writeTypes(typesFile))
  .catch(error => {
    console.error(error)
    process.exit(1)
  })

@@ -532,46 +532,8 @@
 MON)
-%0A$%7Bname%7D.propTypes = COMMON.propTypes%0A
 %60%0A  
@@ -790,64 +790,8 @@
 em'%0A
-import systemPropTypes from '@styled-system/prop-types'%0A
 impo
@@ -850,16 +850,16 @@
 x.esm'%0A%0A
+
 const CO
@@ -931,87 +931,8 @@
 r)%0A%0A
-COMMON.propTypes = %7B%0A  ...systemPropTypes.space,%0A  ...systemPropTypes.color%0A%7D%0A%0A
 $%7Bic

Remove prop types