import {
  Products, StoreWritePostResponse
} from "@komi-app/assets-sdk";
import config, { getSDKConfig } from "config";
import { SdkConfig } from "@komi-app/sales-sdk";
import { Maybe } from "utils/types";


export type UploadFilePayload = {
  profileId: string,
  token: string,
  file: File
}

export interface IDigitalProductService {
  uploadFile(payload: UploadFilePayload): Promise<Maybe<StoreWritePostResponse>>;
}



export default class DigitalProductService implements IDigitalProductService {
  private digitalProduct: Products;
  constructor() {
    const digitalProductConfig: SdkConfig = getSDKConfig('assetsService')
    this.digitalProduct = new Products(digitalProductConfig);
  }

  init(profileId: string) {
    this.digitalProduct.init(profileId);
  }

  uploadFile = async (
    payload:UploadFilePayload
  ): Promise<Maybe<StoreWritePostResponse>> => {
    try {
      const {profileId, token, file} = payload
      let bodyFormData = new FormData();  
      bodyFormData.append('file', file); 
        
      return this.digitalProduct.uploadFile(profileId, token, file);
    } catch (err) {
      console.error(err);
      return void 0;
    }
  };
}
