import { r as registerInstance, h, H as Host, g as getElement } from './index-b7735e0d.js';
import { C as CalendarUtils } from './calendar-67e174ec.js';
import { S as SozlukApi } from './index-1e9f4c53.js';
import './apiBase-d573f009.js';
import './data-transfer-field-918730a3.js';
import './filter-cbaa3d7d.js';
import { c as createMentornityApiInstance, b as Config } from './instance-7b02ef1d.js';
import { g as getEmbeddedTranslation, d as generateCalendarConfig, M as MentornityBridgeApi } from './mentorinty-bridge-api-78ab1153.js';
import { m as mentornityApiEndpoint } from './env-4ec3c807.js';
import { g as getSessionDetails, c as getUser, d as getUserSettings, e as generateMembershipForRole, a as generateMembershipConfigForRole } from './session-b16a440c.js';
import { a as sozlukCalendarEntryEn, b as sozlukCalendarEntryTr, c as sozlukCalendarEntryPtBr, s as sozlukEntries } from './sozluk-entries-849428da.js';
import { T as TextField } from './index-403101cb.js';
import { C as CardListViewConfig } from './card-list-view-config-6825cda0.js';
import { D as DetailViewConfig } from './detail-view-config-59c98d8d.js';
import { T as TableConfig } from './table-config-0c06fb1f.js';
import { T as TakvimAvailableTimePickerViewConfig } from './takvim-available-time-picker-view-config-47827049.js';
import { d as getMembershipFields, e as getUserDetailViewFieldConfigs, c as convertFieldsToFieldReferences, h as getAvailableLegacyProfileFields, a as getAvailableMetadataFields } from './common-wrapper-util-289d199c.js';
import './index-3958847a.js';
import './_commonjsHelpers-27f122dd.js';
import './moment-6f6ab70c.js';
import './BaseField-461b6d0c.js';
import './FieldModelGenerator-020f4a04.js';
import './reference-field-0e7cd534.js';
import './index-7b119e61.js';
import './date-ce777316.js';
import './index-1d3efe49.js';
import './index-eb14fd2d.js';
import './index-80ec23ef.js';
import './index-4c39b6a6.js';
import './index-774783ee.js';
import './attachment-32decf79.js';
import './data-model-f7ccf826.js';
import './membership-e92743d1.js';
import './string-3065a411.js';
import './User-1ef5a482.js';
import './CalendarConfig-88c2213e.js';
import './UserSettings-27df01e6.js';
import './cookie-1686830b.js';
import './kimlikApi-0d7e6ce7.js';
import './index-d3bd30bd.js';
import './i18n-e90027e3.js';
import './view-config-dfb61752.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 = getMembershipFields(language, config, viewedUserType, mentornityCookieContext.role);
  const fieldConfigReferences = 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 TextField('search', getEmbeddedTranslation(language, 'search')) },
    ...convertFieldsToFieldReferences([
      // remove title and bio because they'll be searched via the 'search' field above
      ...getAvailableLegacyProfileFields(language, config, viewedUserType).filter(f => !['title', 'bio'].includes(f.id)),
      ...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.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.decode({
    _id: 'user-card-list-page-available-time-picker-view',
    i18nEntryId: 'user-card-list-page-available-time-picker-view',
    table: 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 = getMembershipFields(language, config, viewedUserRole, mentornityCookieContext.role);
  const detailViewFields = getUserDetailViewFieldConfigs(language, config, viewedUserRole, mentornityCookieContext.role, false);
  const { organisationId } = mentornityCookieContext;
  return DetailViewConfig.decode({
    table: TableConfig.decode({
      fields,
      base: organisationId,
      name: viewedUserRole === 'mentee' ? 'mentees' : 'mentors',
    }),
    ui: {
      cardConfig: {
        header: {
          title: {
            fieldId: 'name',
          },
          actions: [
            {
              ui: {
                label: getEmbeddedTranslation(language, 'goToProfilePage'),
                name: 'go-to-profile-page',
              }
            }
          ],
        },
        body: {
          fields: detailViewFields,
        }
      }
    },
  });
}

let MentornityUserCardListWrapper = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    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 })))));
  }
  get el() { return getElement(this); }
};

export { MentornityUserCardListWrapper as mentornity_user_card_list_wrapper };
