interface resultType {
  [key: string]: any
}
interface dataType {
  must: number,
  type: number,
  name: string,
  data: Array<any>
}
export const fieldType: resultType = {
  0: 'String',
  1: 'Number',
  5: 'Number',
  2: 'Boolean',
  4: 'Object',
  6: 'Array',
  7: 'Array',
  8: 'Array',
  9: 'Array',
  10: 'Date',
  11: 'File'
}
export const fieldMapType: resultType = {
  0: 'String',
  1: 'Number',
  2: 'Boolean',
  3: 'Array',
  4: 'Object',
  5: 'Number',
  6: 'Array',
  7: 'Array',
  8: 'Array',
  9: 'Array',
  10: 'Date',
  11: 'String'
}
export class GenParams {
  private typeMap = fieldMapType
  public result: resultType = {}
  public gen(data: Array<any>, prefix = '', isArray = false) {
    this.result = {}
    genHandler.call(this, data, prefix, isArray)
    function genHandler(this: typeof GenParams.prototype, data: Array<any>, prefix = '', isArray = false) {
      data.forEach((d: dataType) => {
        let fullKey = `${isArray ? prefix + (d.name || '') + '[0]' : prefix + (d.name || '')}`
        this.result[fullKey] = this.getRes(d)
        if (d.data && d.data.length) {
          genHandler.call(this, d.data, isArray ? `${fullKey}.` : fullKey, this.typeMap[d.type] === 'Array')
        }
      })
    }
    return this.result
  }
  public getRes(d: dataType) {
    if (d.must) {
      return {
        type: this.typeMap[d.type],
        required: true
      }
    }
    return this.typeMap[d.type]
  }
}

