import { TakvimAvailableTimePickerViewConfig } from '@dvn/takvim/views/dvn-app-view-takvim-available-time-picker-view/takvim-available-time-picker-view-config';
import { TakvimDailyCalendarViewConfig } from '@dvn/takvim/views/dvn-app-view-takvim-daily-calendar-view/takvim-daily-calendar-view-config';
import { Component, h, Host, Listen, Prop, State } from '@stencil/core';
import { generateCalendarConfig } from '../../api/mentorinty-bridge-api';
import { generateMembershipForRole, generateMentornitySessionContext } from '../../util/session';
export class MentornityHomeMessageWrapper {
  componentWillLoad() {
    // fetch the context async to unblock the component render
    generateMentornitySessionContext(this.contextParams, { fetchConfig: true })
      .then((mentornitySessionContext) => {
      this.mentornitySessionContext = mentornitySessionContext;
      this.view = generateView(mentornitySessionContext);
      this.membership = generateMembershipForRole('admin');
    })
      .catch(error => this.error = error);
  }
  onViewMeeting(event) {
    window.dispatchEvent(new CustomEvent('dvn.mentornity-admin-calendar-view.open-meeting-page', {
      detail: {
        meetingId: event.detail._id,
      }
    }));
  }
  onAction(event) {
    const action = event.detail;
    switch (action.name) {
      case 'open-meeting-detail-after-creation':
        // dispatch event for Mentornity web-client to handle the action
        window.dispatchEvent(new CustomEvent('dvn.mentornity-admin-calendar-view.open-meeting-page', {
          detail: {
            meetingId: action.params.meetingId,
          }
        }));
        break;
    }
  }
  render() {
    const { error, mentornitySessionContext, view } = this;
    if (!!error) {
      return (h("dvn-card", { leanedSides: true, tint: "danger" }, "Failed to get data."));
    }
    if (!mentornitySessionContext) {
      return (h("div", { class: "d-flex justify-content-center pt-3" },
        h("dvn-progress", { indefinite: true })));
    }
    return (h(Host, { class: "d-flex flex-grow-1", style: { overflow: 'hidden', background: 'var(--color-background-primary)' } },
      h("mentornity-root-wrapper", null,
        h("dvn-app-view-takvim-daily-calendar-view", { apiData: this.mentornitySessionContext.mentornityBridgeApi, apiSozluk: this.mentornitySessionContext.apiSozluk, apiTakvim: this.mentornitySessionContext.mentornityBridgeApi, user: this.mentornitySessionContext.user, userSettings: this.mentornitySessionContext.userSettings, membership: this.membership, view: view }))));
  }
  static get is() { return "mentornity-admin-calendar-view"; }
  static get properties() { return {
    "contextParams": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "MentornityContextParams",
        "resolved": "MentornityContextParams",
        "references": {
          "MentornityContextParams": {
            "location": "import",
            "path": "../../util/session"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "error": {},
    "mentornitySessionContext": {},
    "view": {}
  }; }
  static get listeners() { return [{
      "name": "viewMeeting",
      "method": "onViewMeeting",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "action",
      "method": "onAction",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
function generateView(mentornitySessionContext) {
  // this value can be changed to 'mentees' or 'groups' to show them in the list on the left
  const membershipConfigIdToGroupMeetings = 'mentors';
  let membershipConfigIdToViewOnMeetings = 'mentees';
  if (mentornitySessionContext.organisationConfig.menteeType === 'group') {
    membershipConfigIdToViewOnMeetings = 'groups';
  }
  return TakvimDailyCalendarViewConfig.decode({
    i18nEntryId: 'dailyCalendarView',
    table: generateCalendarConfig(mentornitySessionContext.organisationConfig),
    membershipConfigIdToGroupMeetings,
    membershipConfigIdToViewOnMeetings,
    availableTimePickerConfig: TakvimAvailableTimePickerViewConfig.decode({
      _id: 'admin-calendar-page-available-time-picker-view-wrapper',
      i18nEntryId: 'user-card-list-page-available-time-picker-view',
      table: generateCalendarConfig(mentornitySessionContext.organisationConfig),
      successNavigationAction: {
        type: 'navigation',
        direction: 'forward',
        name: 'open-meeting-detail-after-creation',
        params: {
          conversionConfig: {
            'item._id': 'meetingId',
          }
        }
      },
    }),
  });
}
