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