import CalendarUtils from '@dvn/core/util/calendar';
import { Component, h, Host, Listen, Prop, State } from '@stencil/core';
import { createMentornityApiInstance } from '../../../../mentornity-js-client';
import { Config } from '../../../../mentornity-js-client/models';
import { MentornityBridgeApi } from '../../api/mentorinty-bridge-api';
import { mentornityApiEndpoint } from '../../constants/env';
import { generateMembershipForRole, getSessionDetails, getUser, getUserSettings } from '../../util/session';
import { getAppConfig, getCompositeViewUIConfig } from './wrapper-util';
export class MentornityUserDetailWrapper {
  componentWillLoad() {
    const { isZoomAuthenticated, language, timeFormat, timeZone, } = this;
    // set user's locale and time zone
    CalendarUtils.setLocale(language);
    CalendarUtils.setTimeFormat(timeFormat);
    CalendarUtils.setTimeZone(timeZone);
    const { context, headers } = getSessionDetails(this.groupId);
    this.mentornityCookieContext = context;
    this.user = getUser(context.userId);
    this.userSettings = getUserSettings(timeFormat, timeZone, language, isZoomAuthenticated);
    this.membership = generateMembershipForRole(context.role);
    this.mentornityBridgeApi = new MentornityBridgeApi(mentornityApiEndpoint, context, headers);
    createMentornityApiInstance(mentornityApiEndpoint, headers)
      .getOrganisationConfig(context.organisationId)
      .then((response) => {
      this.organisationConfig = Config.decode(response);
      this.mentornityBridgeApi.organisationConfig = Config.decode(response);
    });
  }
  onAction(event) {
    const action = event.detail;
    switch (action.name) {
      case 'send-message':
        // called when user clicks the "Send Message" button from the detail page
        window.dispatchEvent(new CustomEvent('dvn.mentornity-user-detail-wrapper.send-message', {
          detail: {
            user: action.params.item,
          }
        }));
        break;
      case 'open-meeting-detail-after-creation':
      case 'open-meeting-detail-from-meeting-list':
        window.dispatchEvent(new CustomEvent('dvn.mentornity-user-detail-wrapper.open-meeting-detail', {
          detail: {
            meetingId: action.params.meetingId,
          }
        }));
        break;
    }
  }
  render() {
    const { groupId, language, membership, mentornityBridgeApi, mentornityCookieContext, organisationConfig, user, userSettings, viewedUserId, viewedUserRole, } = this;
    if (!organisationConfig) {
      return (h("div", { class: "d-flex justify-content-center pt-3" },
        h("dvn-progress", { indefinite: true })));
    }
    return (h(Host, null,
      h("mentornity-root-wrapper", null,
        h("dvn-app-view-composite-view-linear", { apiData: mentornityBridgeApi, apiTakvim: mentornityBridgeApi, appConfig: getAppConfig(mentornityCookieContext, organisationConfig, language, viewedUserRole), params: {
            itemId: viewedUserId,
          }, user: user, userSettings: userSettings, membership: membership, view: getCompositeViewUIConfig(user._id, groupId, viewedUserRole) }))));
  }
  static get is() { return "mentornity-user-detail-wrapper"; }
  static get originalStyleUrls() { return {
    "$": ["mentornity-user-detail-wrapper.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["mentornity-user-detail-wrapper.css"]
  }; }
  static get properties() { return {
    "language": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "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
    },
    "timeFormat": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "time-format",
      "reflect": false
    },
    "isZoomAuthenticated": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "is-zoom-authenticated",
      "reflect": false
    },
    "organisationId": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "organisation-id",
      "reflect": false
    },
    "groupId": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "group-id",
      "reflect": false
    },
    "viewedUserRole": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "'mentee' | 'mentor'",
        "resolved": "\"mentee\" | \"mentor\"",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "viewed-user-role",
      "reflect": false
    },
    "viewedUserId": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "viewed-user-id",
      "reflect": false
    }
  }; }
  static get states() { return {
    "organisationConfig": {}
  }; }
  static get listeners() { return [{
      "name": "action",
      "method": "onAction",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
