import { DataModel } from '@dvn/core/models/data-model';
import CalendarUtils from '@dvn/core/util/calendar';
import { Component, h, Listen, Prop, State } from '@stencil/core';
import { createMentornityApiInstance } from '../../../../mentornity-js-client';
import { MentornityBridgeApi } from '../../api/mentorinty-bridge-api';
import { mentornityApiEndpoint } from '../../constants/env';
import { getSessionDetails } from '../../util/session';
import { getPairDetailParameters, getPairDetailViewConfig } from './wrapper-util';
export class MentornityPairDetailWrapper {
  onAction(event) {
    const action = event.detail;
    switch (action.name) {
      case 'send-message':
        window.dispatchEvent(new CustomEvent('dvn.atama-pairs.send-message', {
          detail: {
            user: action.data.item.user,
          }
        }));
        break;
      case 'view-availabilities':
        window.dispatchEvent(new CustomEvent('dvn.atama-pairs.view-availabilities', {
          detail: {
            user: action.data.item.user,
          }
        }));
        break;
      case 'create-meeting':
        const meetingConfig = action.data.meetingConfig;
        window.dispatchEvent(new CustomEvent('dvn.atama-pairs.create-meeting', {
          detail: {
            goal: {
              identifier: meetingConfig.id,
              meetingTitle: meetingConfig.name,
            },
            user: action.data.item.user,
          }
        }));
        break;
      case 'open-meeting-detail':
        window.dispatchEvent(new CustomEvent('dvn.atama-pairs.open-meeting-detail', {
          detail: {
            meetingId: action.data.meeting._id,
          }
        }));
        break;
    }
    if (action.type === 'navigation') {
      const { direction, resetHistory, viewId } = action;
      if (direction === 'forward' && resetHistory && viewId === 'pair-overview-page') {
        // user ended the subscription, go to pair list page
        window.dispatchEvent(new CustomEvent('dvn.atama-pairs.open-pair-overview-page', {}));
      }
    }
  }
  componentWillLoad() {
    const { context, headers } = getSessionDetails();
    this.mentornityBridgeApi = new MentornityBridgeApi(mentornityApiEndpoint, context, headers);
    const mentornityApi = createMentornityApiInstance(mentornityApiEndpoint, headers);
    mentornityApi.getOrganisationMeetingGoalConfig(context.organisationDomain).then(response => this.goalConfig = response);
    mentornityApi.getOrganisationConfig(context.organisationId).then((response) => {
      this.organisationConfig = response;
    });
  }
  render() {
    // set user's locale and time zone
    const { goalConfig, language, mentornityBridgeApi, organisationConfig, timeZone } = this;
    CalendarUtils.setLocale(language);
    CalendarUtils.setTimeZone(timeZone);
    const { context } = getSessionDetails();
    const pairDetailViewConfig = getPairDetailViewConfig(context, language, organisationConfig, goalConfig);
    if (!pairDetailViewConfig) {
      return null;
    }
    return (h("mentornity-root-wrapper", null,
      h("dvn-app-view-atama-member-pair-detail-view", { apiAtama: mentornityBridgeApi, apiData: mentornityBridgeApi, apiUye: mentornityBridgeApi, membership: DataModel.decode({}), params: getPairDetailParameters(context, organisationConfig), view: pairDetailViewConfig })));
  }
  static get is() { return "mentornity-pair-detail-wrapper"; }
  static get properties() { return {
    "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
    },
    "timeZone": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "time-zone",
      "reflect": false
    }
  }; }
  static get states() { return {
    "goalConfig": {},
    "organisationConfig": {}
  }; }
  static get listeners() { return [{
      "name": "action",
      "method": "onAction",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
