import {
  GenerateStyle,
  GenerateTpl,
  GenerateJs
} from './index'
const babel = require('../babel')
class GenerateComTpl extends GenerateTpl {
  CODE: string = ``
  COMPOSE: Record<
    string | number | symbol,
    any
  > = {}
  constructor (name: string, props: any) {
    super()
    //生成组件
    this.CODE = this.composeCode(this.generate(name, props))
  }
  generate (name: string, props: any) {
    return this.baseGenerate(name, props, {
      before () {},
      executing: (isString: boolean, props: any, key: string) => {
        if(!isString) {
          this.COMPOSE[key] = props[key]
        }
      },
      after () {}
    })
  }
}
class GenerateComJs extends GenerateJs {
  CODE: string = ''
  TPL: string = `
  import TreeList from '@/page/business/inline-tree-list'
  export default {
    data () {
      return {}
    },
    components: {
      TreeList
    },
    methods: {}
  }`
  constructor(options: VueConfig) {
    super()
    this.CODE = this.composeCode(this.generate(options))
  }
  generate(options: VueConfig) {
    return babel(this.TPL, {
      visitor(t: any, babylon: any) {
        return {
          ReturnStatement(path: any) {
            if (path.node.leadingComments) return
            const newBlockStatement = t.returnStatement(
              babylon.parseExpression(JSON.stringify(options.data))
            )
            path.replaceWith(newBlockStatement)
          }
        }
      },
      lintCode (code: string) {
        return code.replace(/data\(\) \{/, 'data () {').replace(/\"/g, '\'')
      }
    })
  }
}
class GenerateComStyle extends GenerateStyle {
  constructor(name: string) {
    super()
  }
}
module.exports = (props: any = {}) => {
  const name = 'TreeList'
  const tpl = new GenerateComTpl(name, props)
  const js = new GenerateComJs({
    data: tpl.COMPOSE
  })
  const style = new GenerateComStyle(name)
  return tpl.CODE + js.CODE + style.CODE
}
