import { r as registerInstance, h, H as Host, g as getElement } from './index-55faf181.js';
import { C as CalendarUtils } from './calendar-51e097ef.js';
import './kimlikApi-7350588e.js';
import './data-transfer-field-918730a3.js';
import './filter-cbaa3d7d.js';
import { c as createMentornityApiInstance, f as Config } from './instance-46ab1e99.js';
import { g as generateCalendarConfig, M as MentornityBridgeApi } from './mentorinty-bridge-api-946adc0d.js';
import { g as getSessionDetails, c as getUser, d as getUserSettings, e as generateMembershipForRole, m as mentornityApiEndpoint, a as generateMembershipConfigForRole } from './session-3ec0aa4c.js';
import { T as TextField } from './index-403e215e.js';
import { C as CardListViewConfig } from './card-list-view-config-49388d3f.js';
import { D as DetailViewConfig } from './detail-view-config-dafcc086.js';
import { T as TableConfig } from './table-config-5c917348.js';
import { T as TakvimAvailableTimePickerViewConfig } from './takvim-available-time-picker-view-config-94465a89.js';
import { d as getMembershipFields, e as getUserDetailViewFieldConfigs, c as convertFieldsToFieldReferences, f as getAvailableLegacyProfileFields, a as getAvailableMetadataFields } from './common-wrapper-util-36df331f.js';
import './index-e6685442.js';
import './_commonjsHelpers-27f122dd.js';
import './moment-6f6ab70c.js';
import './BaseField-461b6d0c.js';
import './FieldModelGenerator-1ee7fda2.js';
import './index-6a1cf9c1.js';
import './date-ce777316.js';
import './index-083a6906.js';
import './index-fa1db470.js';
import './index-ebde6a66.js';
import './attachment-32decf79.js';
import './data-model-f5160369.js';
import './membership-61c4a2f7.js';
import './membership-config-a51a5232.js';
import './string-3065a411.js';
import './User-d2ee8b71.js';
import './CalendarConfig-184230a0.js';
import './UserSettings-7b80b841.js';
import './cookie-93ea205f.js';
import './index-d3bd30bd.js';
import './view-config-39fd369a.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(config, viewedUserType, mentornityCookieContext.role);
  const fieldConfigReferences = getUserDetailViewFieldConfigs(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',
    }];
  // simple search ui config
  const quickSearchUIConfig = [];
  const quickSearchFieldConfigReferences = [
    { field: new TextField('search', 'Search') },
    ...convertFieldsToFieldReferences([
      // remove title and bio because they'' be searched via the 'search' field above
      ...getAvailableLegacyProfileFields(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`,
    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',
    table: generateCalendarConfig(config),
    successNavigationAction: {
      type: 'navigation',
      direction: 'forward',
      name: 'open-meeting-detail-after-creation',
      params: {
        conversionConfig: {
          'item._id': 'meetingId',
        }
      }
    },
  });
}
function getUserDetailDialogViewConfig(mentornityCookieContext, config, viewedUserRole) {
  if (!config) {
    return undefined;
  }
  const fields = getMembershipFields(config, viewedUserRole, mentornityCookieContext.role);
  const detailViewFields = getUserDetailViewFieldConfigs(config, viewedUserRole, mentornityCookieContext.role, false);
  const { organisationId } = mentornityCookieContext;
  return DetailViewConfig.decode({
    table: TableConfig.decode({
      base: organisationId,
      name: viewedUserRole === 'mentee' ? 'mentees' : 'mentors',
    }),
    ui: {
      fields,
      cardConfig: {
        header: {
          title: {
            fieldId: 'name',
          },
          actions: [
            {
              ui: {
                label: 'Go to Profile Page',
                name: 'go-to-profile-page',
              }
            }
          ],
        },
        body: {
          fields: detailViewFields,
        }
      }
    },
  });
}

const MentornityUserCardListWrapper = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
  }
  async componentWillLoad() {
    const { isZoomAuthenticated, language, timeFormat, timeZone, viewedUserRole, } = this;
    // set user's locale and time zone
    CalendarUtils.setLocale(language);
    CalendarUtils.setTimeFormat(timeFormat);
    CalendarUtils.setTimeZone(timeZone);
    const { context, headers } = getSessionDetails(this.groupId);
    this.mentornityCookieContext = context;
    this.user = getUser(context.userId);
    this.userSettings = getUserSettings(timeFormat, timeZone, language, isZoomAuthenticated);
    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, 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 { 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, 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", { apiUye: mentornityBridgeApi, apiTakvim: 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, id: "user-page", params: {
          itemId: userToViewDetails._id,
        }, view: userDetailDialogViewConfig })))));
  }
  get el() { return getElement(this); }
};

export { MentornityUserCardListWrapper as mentornity_user_card_list_wrapper };
