import { createMentornityApiInstance } from '../../../../mentornity-js-client';
import { mentornityApiEndpoint } from '../../constants/env';
import { getSessionDetails } from '../../util/session';
export class UpdateUserMetadataHandler {
  constructor(userMetadata) {
    this.userMetadata = userMetadata;
    const { headers } = getSessionDetails();
    this.api = createMentornityApiInstance(mentornityApiEndpoint, headers);
  }
  onSubmit(event) {
    const { values } = event.detail;
    const metadataData = values;
    return new Promise((resolve, reject) => {
      let promises = [];
      if (this.userMetadata) {
        promises.push(this.api.update('metadatas', this.userMetadata._id, {
          data: Object.assign(Object.assign({}, this.userMetadata.data), metadataData)
        }));
      }
      Promise.all(promises)
        .then(() => resolve({}))
        .catch(() => {
        reject(['Unexpected error']);
      });
    });
  }
}
