import BaseNormalizer from '~/services/serializer/normalizer/baseNormalizer'
import {DataPersisterArgs} from '~/types/interfaces'
import { QUERY_TYPE } from '~/types/enums'

/**
 * @category Services/serializer/normalizer
 * @class Default
 * Classe assurant la normalization par défaut
 */
class File extends BaseNormalizer {
  static support (type: QUERY_TYPE): boolean {
    return type === QUERY_TYPE.FILE
  }

  /**
   * On transforme les data en FormData et on les renvois
   * @param {DataPersisterArgs} args
   * @return {any} réponse
   */
  public static normalize (args: DataPersisterArgs): any {
    if (!args.data) {
      throw new Error('*args* has no data attribute')
    }

    const fileData = new FormData();
    for(const key in args.data){
      fileData.set(key, args.data[key])
    }

    fileData.set('file', args.file as string)
    return fileData
  }
}
export default File
