import CalendarUtils from '@dvn/core/util/calendar';
import { User } from '@dvn/kimlik/api/models';
import { SozlukApi } from '@dvn/sozluk/api';
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 { sozlukCalendarEntryEn, sozlukCalendarEntryPtBr, sozlukCalendarEntryTr } from '../sozluk-calendar-entries';
import { sozlukEntries } from '../sozluk-entries';
import { getAppConfig, getCompositeViewUIConfig } from './wrapper-util';
export class MentornityUserDetailWrapper {
  constructor() {
    this.weekStartDay = 'monday';
  }
  componentWillLoad() {
    const { isZoomAuthenticated, language, timeFormat, dateFormat, timeZone, weekStartDay, groupId, viewedUserRole, } = this;
    // set user's locale and time zone
    CalendarUtils.setTimeFormat(timeFormat);
    CalendarUtils.setDateFormat(dateFormat);
    CalendarUtils.setTimeZone(timeZone);
    switch (language) {
      case 'pt-br':
        CalendarUtils.setLocale(language, sozlukCalendarEntryPtBr, weekStartDay);
        break;
      case 'tr':
        CalendarUtils.setLocale(language, sozlukCalendarEntryTr, weekStartDay);
        break;
      default:
        CalendarUtils.setLocale(language, sozlukCalendarEntryEn, weekStartDay);
        break;
    }
    this.apiSozluk = new SozlukApi().setLanguage(language).setContents(sozlukEntries);
    const { context, headers } = getSessionDetails(this.groupId);
    const { userId, role } = context;
    this.user = getUser(userId);
    this.userSettings = getUserSettings(timeFormat, dateFormat, timeZone, language, isZoomAuthenticated);
    this.mentornityBridgeApi = new MentornityBridgeApi(mentornityApiEndpoint, context, headers);
    const mentornityApi = createMentornityApiInstance(mentornityApiEndpoint, headers);
    mentornityApi.get('users', context.userId)
      .then(user => this.user = User.decode(user));
    mentornityApi
      .getOrganisationConfig(context.organisationId)
      .then((config) => {
      this.organisationConfig = Config.decode(config);
      this.mentornityBridgeApi.organisationConfig = Config.decode(config);
      if (context.role === 'mentee' && this.organisationConfig.menteeType === 'group') {
        this.membership = generateMembershipForRole('group');
      }
      else {
        this.membership = generateMembershipForRole(context.role);
      }
      this.guestToPassToAvailableTimePickerAsCurrentUser = this.user;
      if (context.role === 'mentee' && config.menteeType === 'group') {
        // fetch the group of the mentee and set as the guest instead of the mentee
        if (context.groupId) {
          mentornityApi.getGroup(context.groupId).then(group => {
            if (group) {
              this.guestToPassToAvailableTimePickerAsCurrentUser = Object.assign(Object.assign({}, group), { name: group.title, initials: group.title[0] });
            }
          });
        }
        else {
          this.guestToPassToAvailableTimePickerAsCurrentUser = undefined;
        }
      }
      this.appConfig = getAppConfig(context, this.organisationConfig, language, role, viewedUserRole);
      this.compositeViewUIConfig = getCompositeViewUIConfig(this.organisationConfig, language, userId, groupId, role, viewedUserRole);
    });
  }
  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 { compositeViewUIConfig, appConfig, membership, mentornityBridgeApi, organisationConfig, userSettings, viewedUserId, user, } = this;
    if (!organisationConfig || !user) {
      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", { apiSozluk: this.apiSozluk, apiData: mentornityBridgeApi, apiTakvim: mentornityBridgeApi, appConfig: appConfig, params: {
            itemId: viewedUserId,
          }, user: this.guestToPassToAvailableTimePickerAsCurrentUser, userSettings: userSettings, membership: membership, view: compositeViewUIConfig }))));
  }
  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
    },
    "dateFormat": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "date-format",
      "reflect": false
    },
    "weekStartDay": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "'monday' | 'saturday' | 'sunday'",
        "resolved": "\"monday\" | \"saturday\" | \"sunday\"",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "week-start-day",
      "reflect": false,
      "defaultValue": "'monday'"
    },
    "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": {},
    "appConfig": {},
    "compositeViewUIConfig": {},
    "guestToPassToAvailableTimePickerAsCurrentUser": {}
  }; }
  static get listeners() { return [{
      "name": "action",
      "method": "onAction",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
