import CalendarUtils from '@dvn/core/util/calendar';
import { SozlukApi } from '@dvn/sozluk/api';
import { Component, Element, 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 { generateMembershipConfigForRole, generateMembershipForRole, getSessionDetails, getUser, getUserSettings } from '../../util/session';
import { sozlukCalendarEntryEn, sozlukCalendarEntryPtBr, sozlukCalendarEntryTr } from '../sozluk-calendar-entries';
import { sozlukEntries } from '../sozluk-entries';
import { getAvailableTimePickerViewConfig, getUserCardListViewConfig, getUserDetailDialogViewConfig } from './wrapper-util';
export class MentornityUserCardListWrapper {
  constructor() {
    this.weekStartDay = 'monday';
  }
  async componentWillLoad() {
    const { isZoomAuthenticated, language, timeFormat, dateFormat, timeZone, viewedUserRole, weekStartDay, } = this;
    // set user's locale and time zone
    CalendarUtils.setTimeFormat(timeFormat);
    CalendarUtils.setTimeZone(timeZone);
    CalendarUtils.setDateFormat(dateFormat);
    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);
    this.mentornityCookieContext = context;
    this.user = getUser(context.userId);
    this.userSettings = getUserSettings(timeFormat, dateFormat, timeZone, language, isZoomAuthenticated, weekStartDay);
    this.membership = generateMembershipForRole(context.role);
    this.mentornityBridgeApi = new MentornityBridgeApi(mentornityApiEndpoint, context, headers);
    const mentornityApi = createMentornityApiInstance(mentornityApiEndpoint, headers);
    // const getViewConfigsResponse = await mentornityApi.getViewConfigs(context.organisationId)
    //   .catch(error => this.error = error);
    // if (!getViewConfigsResponse) return;
    // const viewConfigs = getViewConfigsResponse.results;
    const config = await mentornityApi.getOrganisationConfig(context.organisationId)
      .catch(error => this.error = error);
    if (!config)
      return;
    const organisationConfig = Config.decode(config);
    this.organisationConfig = organisationConfig;
    this.mentornityBridgeApi.organisationConfig = organisationConfig;
    this.pairOverviewViewConfig = getUserCardListViewConfig(context, language, organisationConfig, viewedUserRole);
    this.userDetailDialogViewConfig = getUserDetailDialogViewConfig(context, language, organisationConfig, viewedUserRole);
  }
  componentDidRender() {
    var _a, _b;
    (_b = (_a = this.el.querySelector(`dvn-app-view-list-view-card`)) === null || _a === void 0 ? void 0 : _a.retainPosition) === null || _b === void 0 ? void 0 : _b.call(_a);
  }
  onAction(event) {
    const action = event.detail;
    switch (action.name) {
      case 'schedule-meeting':
        this.userToCreateMeeting = action.params.item;
        break;
      case 'view-details':
        this.userToViewDetails = action.params.item;
        break;
      case 'go-to-profile-page':
        // dispatch event for Mentornity web-client to handle the action
        window.dispatchEvent(new CustomEvent('dvn.mentornity-user-card-list-wrapper.open-profile-page', {
          detail: {
            user: action.params.item,
          }
        }));
        break;
      case 'open-meeting-detail-after-creation':
        // dispatch event for Mentornity web-client to handle the action
        window.dispatchEvent(new CustomEvent('dvn.mentornity-user-card-list-wrapper.open-meeting-page', {
          detail: {
            meetingId: action.params.meetingId,
          }
        }));
        break;
    }
  }
  render() {
    const { apiSozluk, membership, mentornityBridgeApi, organisationConfig, pairOverviewViewConfig, viewedUserRole, user, userSettings, userToCreateMeeting, userToViewDetails, userDetailDialogViewConfig, } = this;
    if (!pairOverviewViewConfig) {
      return (h("div", { class: "d-flex justify-content-center pt-3" },
        h("dvn-progress", { indefinite: true })));
    }
    return (h(Host, { style: { display: 'flex', flexDirection: 'column', height: '100%', overflow: 'hidden' } },
      h("mentornity-root-wrapper", null,
        h("dvn-app-view-list-view-card", { apiData: mentornityBridgeApi, apiSozluk: apiSozluk, view: pairOverviewViewConfig }),
        userToCreateMeeting &&
          h("dvn-dialog", { raw: true, shown: true, onDismiss: () => {
              this.userToCreateMeeting = undefined;
            } },
            h("dvn-card-header", { title: this.userToCreateMeeting.name }),
            h("dvn-app-view-takvim-available-time-picker-view", { apiSozluk: apiSozluk, apiTakvim: mentornityBridgeApi, apiUye: mentornityBridgeApi, user: user, userSettings: userSettings, membership: membership, params: {
                viewedUserMembership: userToCreateMeeting,
                viewedUserMembershipConfig: generateMembershipConfigForRole(viewedUserRole),
              }, view: getAvailableTimePickerViewConfig(organisationConfig) })),
        userToViewDetails &&
          h("dvn-dialog", { raw: true, shown: true, onDismiss: () => this.userToViewDetails = undefined },
            h("dvn-app-view-detail-view-card", { apiData: mentornityBridgeApi, apiSozluk: apiSozluk, id: "user-page", params: {
                itemId: userToViewDetails._id,
              }, view: userDetailDialogViewConfig })))));
  }
  static get is() { return "mentornity-user-card-list-wrapper"; }
  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
    }
  }; }
  static get states() { return {
    "organisationConfig": {},
    "organisationViewConfigs": {},
    "error": {},
    "pairOverviewViewConfig": {},
    "userDetailDialogViewConfig": {},
    "userToCreateMeeting": {},
    "userToViewDetails": {}
  }; }
  static get elementRef() { return "el"; }
  static get listeners() { return [{
      "name": "action",
      "method": "onAction",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
