import { InvalidationAction } from '@dvn/core/models/actions';
import { getCookieStorage } from '@dvn/kimlik/storage';
import { createMentornityApiInstance } from '../../../mentornity-js-client';
import { detailViewUser, formViewUserErase, tableListViewUser, } from '../configs/view-configs';
import { mentornityApiEndpoint } from '../constants/env';
export class EraseUserFormHandler {
  constructor() {
    const { jwt, sessionId } = getCookieStorage().getAll();
    this.api = createMentornityApiInstance(mentornityApiEndpoint, {
      'Authorization': `Bearer ${jwt}`,
      'Client': 'd2ViLWFkbWlu',
      'Session-Token': sessionId,
    });
  }
  onSubmit(event) {
    const { values: { userId } } = event.detail;
    return new Promise((resolve, reject) => {
      this.api.eraseUser(userId).then(() => {
        resolve({
          events: [
            new InvalidationAction(formViewUserErase.ID, detailViewUser.ID),
            new InvalidationAction(formViewUserErase.ID, tableListViewUser.ID),
          ],
          navigation: {
            type: 'navigation',
            direction: 'back'
          }
        });
      }).catch(() => {
        reject(['Unexpected error']);
      });
    });
  }
}
