'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index$1 = require('./index-1ee80297.js');
const calendar = require('./calendar-e830b67f.js');
const index$2 = require('./index-b2910095.js');
require('./kimlikApi-1cd43caf.js');
require('./data-transfer-field-0ce05c1a.js');
require('./filter-d5040f65.js');
const instance = require('./instance-8904b0e4.js');
const mentorintyBridgeApi = require('./mentorinty-bridge-api-ede51458.js');
const env = require('./env-56a21d68.js');
const session = require('./session-612c0d2f.js');
const sozlukEntries = require('./sozluk-entries-34e8486b.js');
const index = require('./index-a77c3004.js');
const cardListViewConfig = require('./card-list-view-config-29981c18.js');
const detailViewConfig = require('./detail-view-config-c107e694.js');
const tableConfig = require('./table-config-be88299b.js');
const takvimAvailableTimePickerViewConfig = require('./takvim-available-time-picker-view-config-5b4dbdd1.js');
const commonWrapperUtil = require('./common-wrapper-util-5385d126.js');
require('./index-1f95aafc.js');
require('./_commonjsHelpers-206db00d.js');
require('./moment-d48a0164.js');
require('./BaseField-d019ade6.js');
require('./FieldModelGenerator-2c39c8e9.js');
require('./index-42baafa9.js');
require('./date-25b104f6.js');
require('./index-521d07b9.js');
require('./index-9e7c4ca0.js');
require('./index-30008542.js');
require('./attachment-3090e25e.js');
require('./data-model-c51de090.js');
require('./membership-14c44e7c.js');
require('./membership-config-6ba394cc.js');
require('./string-de672b69.js');
require('./User-286a0714.js');
require('./CalendarConfig-fed039db.js');
require('./UserSettings-016122e8.js');
require('./cookie-c4d9848a.js');
require('./index-33e96540.js');
require('./i18n-e6beaec7.js');
require('./view-config-c409ff28.js');

function getUserCardListViewConfig(mentornityCookieContext, language, config, viewedUserType) {
  if (!config) {
    return null;
  }
  // TODO
  // get the table fields from the view config if exists. otherwise generate them from the config
  // const viewConfig = getViewConfigForViewName(viewConfigs, MentornityViewConfigViewName.MenteeCardList);
  const membershipFields = commonWrapperUtil.getMembershipFields(language, config, viewedUserType, mentornityCookieContext.role);
  const fieldConfigReferences = commonWrapperUtil.getUserDetailViewFieldConfigs(language, config, viewedUserType, mentornityCookieContext.role, true, 4);
  // add the big button for schedule meeting and view details actions
  const cardBodyFields = [...fieldConfigReferences, {
      customViewTag: 'mentornity-schedule-meeting-button-on-card',
      language,
    }];
  // simple search ui config
  const quickSearchUIConfig = [];
  const quickSearchFieldConfigReferences = [
    { field: new index.TextField('search', mentorintyBridgeApi.getEmbeddedTranslation(language, 'search')) },
    ...commonWrapperUtil.convertFieldsToFieldReferences([
      // remove title and bio because they'll be searched via the 'search' field above
      ...commonWrapperUtil.getAvailableLegacyProfileFields(language, config, viewedUserType).filter(f => !['title', 'bio'].includes(f.id)),
      ...commonWrapperUtil.getAvailableMetadataFields(mentornityCookieContext.role, config),
    ]),
  ];
  quickSearchFieldConfigReferences.forEach(fieldConfigReference => {
    var _a;
    const fieldId = fieldConfigReference.fieldId || ((_a = fieldConfigReference.field) === null || _a === void 0 ? void 0 : _a.id);
    if (fieldId) {
      quickSearchUIConfig.push({ fieldId, width: '200px' });
    }
  });
  return cardListViewConfig.CardListViewConfig.decode({
    _id: `${viewedUserType}-card-list`,
    i18nEntryId: `mentornity-${viewedUserType}-card-list`,
    ui: {
      quickSearch: {
        fields: quickSearchFieldConfigReferences,
        fieldUIConfigs: quickSearchUIConfig,
      },
      main: {
        cardViewConfig: {
          header: {
            cover: {
              size: 100,
            },
            image: {
              fieldId: 'user.imageUrl',
              placeholderFieldId: 'user.initials',
              size: 72,
            },
            title: {
              fieldId: 'user.name',
            },
          },
          body: {
            fields: cardBodyFields,
          },
        },
      },
    },
    table: {
      fields: membershipFields,
      roleName: viewedUserType,
      name: `${viewedUserType}-card-list`,
    },
    query: {
      fields: cardBodyFields,
    },
  });
}
function getAvailableTimePickerViewConfig(config) {
  if (!config) {
    return undefined;
  }
  return takvimAvailableTimePickerViewConfig.TakvimAvailableTimePickerViewConfig.decode({
    _id: 'user-card-list-page-available-time-picker-view',
    i18nEntryId: 'user-card-list-page-available-time-picker-view',
    table: mentorintyBridgeApi.generateCalendarConfig(config),
    successNavigationAction: {
      type: 'navigation',
      direction: 'forward',
      name: 'open-meeting-detail-after-creation',
      params: {
        conversionConfig: {
          'item._id': 'meetingId',
        }
      }
    },
  });
}
function getUserDetailDialogViewConfig(mentornityCookieContext, language, config, viewedUserRole) {
  if (!config) {
    return undefined;
  }
  const fields = commonWrapperUtil.getMembershipFields(language, config, viewedUserRole, mentornityCookieContext.role);
  const detailViewFields = commonWrapperUtil.getUserDetailViewFieldConfigs(language, config, viewedUserRole, mentornityCookieContext.role, false);
  const { organisationId } = mentornityCookieContext;
  return detailViewConfig.DetailViewConfig.decode({
    table: tableConfig.TableConfig.decode({
      fields,
      base: organisationId,
      name: viewedUserRole === 'mentee' ? 'mentees' : 'mentors',
    }),
    ui: {
      cardConfig: {
        header: {
          title: {
            fieldId: 'name',
          },
          actions: [
            {
              ui: {
                label: mentorintyBridgeApi.getEmbeddedTranslation(language, 'goToProfilePage'),
                name: 'go-to-profile-page',
              }
            }
          ],
        },
        body: {
          fields: detailViewFields,
        }
      }
    },
  });
}

let MentornityUserCardListWrapper = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.weekStartDay = 'monday';
  }
  async componentWillLoad() {
    const { isZoomAuthenticated, language, timeFormat, timeZone, viewedUserRole, weekStartDay, } = this;
    // set user's locale and time zone
    calendar.CalendarUtils.setTimeFormat(timeFormat);
    calendar.CalendarUtils.setTimeZone(timeZone);
    switch (language) {
      case 'pt-br':
        calendar.CalendarUtils.setLocale(language, sozlukEntries.sozlukCalendarEntryPtBr, weekStartDay);
        break;
      case 'tr':
        calendar.CalendarUtils.setLocale(language, sozlukEntries.sozlukCalendarEntryTr, weekStartDay);
        break;
      default:
        calendar.CalendarUtils.setLocale(language, sozlukEntries.sozlukCalendarEntryEn, weekStartDay);
        break;
    }
    this.apiSozluk = new index$2.SozlukApi().setLanguage(language).setContents(sozlukEntries.sozlukEntries);
    const { context, headers } = session.getSessionDetails(this.groupId);
    this.mentornityCookieContext = context;
    this.user = session.getUser(context.userId);
    this.userSettings = session.getUserSettings(timeFormat, timeZone, language, isZoomAuthenticated, weekStartDay);
    this.membership = session.generateMembershipForRole(context.role);
    this.mentornityBridgeApi = new mentorintyBridgeApi.MentornityBridgeApi(env.mentornityApiEndpoint, context, headers);
    const mentornityApi = instance.createMentornityApiInstance(env.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 = instance.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 (index$1.h("div", { class: "d-flex justify-content-center pt-3" }, index$1.h("dvn-progress", { indefinite: true })));
    }
    return (index$1.h(index$1.Host, { style: { display: 'flex', flexDirection: 'column', height: '100%', overflow: 'hidden' } }, index$1.h("mentornity-root-wrapper", null, index$1.h("dvn-app-view-list-view-card", { apiData: mentornityBridgeApi, apiSozluk: apiSozluk, view: pairOverviewViewConfig }), userToCreateMeeting &&
      index$1.h("dvn-dialog", { raw: true, shown: true, onDismiss: () => {
          this.userToCreateMeeting = undefined;
        } }, index$1.h("dvn-card-header", { title: this.userToCreateMeeting.name }), index$1.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: session.generateMembershipConfigForRole(viewedUserRole),
        }, view: getAvailableTimePickerViewConfig(organisationConfig) })), userToViewDetails &&
      index$1.h("dvn-dialog", { raw: true, shown: true, onDismiss: () => this.userToViewDetails = undefined }, index$1.h("dvn-app-view-detail-view-card", { apiData: mentornityBridgeApi, apiSozluk: apiSozluk, id: "user-page", params: {
          itemId: userToViewDetails._id,
        }, view: userDetailDialogViewConfig })))));
  }
  get el() { return index$1.getElement(this); }
};

exports.mentornity_user_card_list_wrapper = MentornityUserCardListWrapper;
