import { injectNotifier } from '@dvn/alaca/global/notifier';
import { Component, h, Listen, Prop, State } from '@stencil/core';
import { createMentornityApiInstance } from '../../../../mentornity-js-client';
import { MentornityViewConfigViewName } from '../../../../mentornity-js-client/models';
import { mentornityApiEndpoint } from '../../constants/env';
import { getSessionDetails } from '../../util/session';
import { getFormParameters, getFormViewConfig } from './wrapper-util';
export class MentornityMembershipSettingsWrapper {
  constructor() {
    this.isViewConfigWrapperLoaded = false;
  }
  onInvalidation(event) {
    this.notifier.showCustomNotification(event.detail.notification);
  }
  ;
  componentWillLoad() {
    const { context: { organisationId, role, userId }, headers } = getSessionDetails();
    const mentornityApi = createMentornityApiInstance(mentornityApiEndpoint, headers);
    mentornityApi.getOrganisationConfig(organisationId).then((response) => {
      this.organisationConfig = response;
    });
    const viewName = role === 'mentor'
      ? MentornityViewConfigViewName.MentorInfoForm
      : MentornityViewConfigViewName.MenteeInfoForm;
    mentornityApi.getViewConfig(organisationId, viewName)
      .then(viewConfigWrapper => {
      this.viewConfigWrapper = viewConfigWrapper;
    }).finally(() => this.isViewConfigWrapperLoaded = true);
    mentornityApi.get('users', userId).then((response) => {
      this.user = response;
    });
    mentornityApi
      .getMetadataOfUser(organisationId, role === 'mentor' ? 'mentors' : 'mentees', userId)
      .then((response) => {
      this.userMetadata = response;
    });
    if (role === 'mentor') {
      mentornityApi
        .getMentorshipOfUser(organisationId, userId)
        .then((response) => {
        this.userMentorship = response;
      });
    }
  }
  render() {
    const { isViewConfigWrapperLoaded, language, organisationConfig, user, userMentorship, userMetadata, viewConfigWrapper } = this;
    const { context } = getSessionDetails();
    if (!isViewConfigWrapperLoaded || !organisationConfig || !user || !userMetadata || (context.role === 'mentor' && !userMentorship)) {
      return (h("mentornity-root-wrapper", null,
        h("div", { class: "d-flex justify-content-center pt-3" },
          h("dvn-progress", { indefinite: true }))));
    }
    // filter fields if additional info has been deleted
    const fieldIdsToUse = {
      // default values
      firstName: true,
      lastName: true
    };
    const oldMetadataIds = ['bio', 'skills', 'title'];
    const showOldMetaData = !this.organisationConfig.webAppConfiguration.membershipForm;
    let pairDetailViewConfig = getFormViewConfig(context, language, organisationConfig, viewConfigWrapper, user, userMetadata, userMentorship);
    if (viewConfigWrapper) {
      (viewConfigWrapper.dvnAppViewConfig.table.fields || []).forEach(usableField => {
        const oldMetadataFieldNotFound = !oldMetadataIds.includes(usableField.id);
        if (showOldMetaData || oldMetadataFieldNotFound) {
          fieldIdsToUse[usableField.id] = true;
        }
      });
      pairDetailViewConfig.ui.fields = pairDetailViewConfig.ui.fields.filter(field => fieldIdsToUse[field.fieldId]);
    }
    return (h("mentornity-root-wrapper", null,
      h("dvn-card", null,
        h("dvn-app-view-form-view", { params: getFormParameters(context, organisationConfig, user, userMetadata, userMentorship), view: pairDetailViewConfig }))));
  }
  static get is() { return "mentornity-membership-settings-wrapper"; }
  static get properties() { return {
    "notifier": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Notifier",
        "resolved": "Notifier",
        "references": {
          "Notifier": {
            "location": "import",
            "path": "@dvn/alaca/global/notifier/models"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Used to show notification and passed by the\ncontext injector when injectNotifier() called in this file."
      }
    },
    "language": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Used to show proper translations, passed by the parent component."
      },
      "attribute": "language",
      "reflect": false
    }
  }; }
  static get states() { return {
    "organisationConfig": {},
    "user": {},
    "userMetadata": {},
    "userMentorship": {},
    "viewConfigWrapper": {},
    "isViewConfigWrapperLoaded": {}
  }; }
  static get listeners() { return [{
      "name": "notification",
      "method": "onInvalidation",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
injectNotifier(MentornityMembershipSettingsWrapper);
