var __rest = (this && this.__rest) || function (s, e) {
  var t = {};
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
    t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
import { Notification } from '@dvn/alaca/global/notifier';
import { NotificationAction } from '@dvn/core/models/actions';
import { createMentornityApiInstance } from '../../../../mentornity-js-client';
import { mentornityApiEndpoint } from '../../constants/env';
import { getSessionDetails } from '../../util/session';
import { en, tr, pt_br } from './i18n';
export class UpdateMembershipInfoFormHandler {
  constructor(user, userMetadata, userMentorship, language) {
    this.user = user;
    this.language = language;
    this.userMetadata = userMetadata;
    this.userMentorship = userMentorship;
    const { headers } = getSessionDetails();
    this.api = createMentornityApiInstance(mentornityApiEndpoint, headers);
  }
  onSubmit(event) {
    const { values } = event.detail;
    const { firstName, lastName, bio, skills, title, subscriptionLimit } = values, rest = __rest(values, ["firstName", "lastName", "bio", "skills", "title", "subscriptionLimit"]);
    const profileData = {
      firstName, lastName, bio, skills, title,
    };
    const mentorshipData = { subscriptionLimit: parseInt(subscriptionLimit) };
    const metadataData = rest;
    return new Promise((resolve, reject) => {
      let promises = [
        this.api.update('users', this.user._id, profileData),
      ];
      if (this.userMetadata) {
        promises.push(this.api.update('metadatas', this.userMetadata._id, {
          data: Object.assign(Object.assign({}, this.userMetadata.data), metadataData)
        }));
      }
      if (this.userMentorship && subscriptionLimit !== undefined) {
        promises.push(this.api.update('mentorships', this.userMentorship._id, mentorshipData));
      }
      const i18n = this.language === 'tr'
        ? tr
        : (this.language === 'pt-br' ? pt_br : en);
      Promise.all(promises).then(() => resolve({
        events: [
          new NotificationAction(Notification.congratulate(i18n.formView.infoUpdated)),
        ],
      }))
        .catch(() => {
        reject(['Unexpected error']);
      });
    });
  }
}
