import { useContext, OperatorType, HttpTriggerType, HttpMethod } from '@midwayjs/hooks-core';
import groupBy from 'lodash.groupby';

function useFiles() {
  const ctx = useContext();
  const files = ctx.files;
  return groupBy(files, 'fieldName');
}

function useFields() {
  const ctx = useContext();
  return ctx.fields;
}





function Upload(path) {
  return {
    name: 'Upload',
    input: true,
    metadata({ setMetadata }) {
      setMetadata(OperatorType.Trigger, {
        type: HttpTriggerType,
        method: HttpMethod.POST,
        path,
        requestClient: {
          fetcher: 'upload',
          client: '@midwayjs/rpc' } });


    } };

}

export { Upload, useFields, useFiles };
