import { r as registerInstance, h } from './index-b6f10a81.js';
import './index-ec7dc2ad.js';
import { D as DataModel } from './data-model-bb5789f5.js';
import './BaseField-d94c5b14.js';
import './FieldModelGenerator-6ec60042.js';
import './date-b270be76.js';
import './index-94c1d7bf.js';
import { T as TableConfig, d as decodeFields } from './table-config-c0c15e50.js';
import './string-2d991277.js';
import './membership-config-856f6e58.js';
import './User-e14f1ece.js';
import './_commonjsHelpers-63c253cd.js';
import './index-fd2b14ae.js';
import './kimlikApi-81acfec6.js';
import './data-transfer-field-1ad95e08.js';
import './filter-cbaa3d7d.js';
import './cookie-d9098150.js';
import { D as DvnAppViewConfig } from './view-config-3ab16b28.js';
import './attachment-8831acff.js';
import { b as InfoVisibility, c as createMentornityApiInstance, d as Config } from './instance-f50cdfd9.js';
import { g as getSessionDetails, m as mentornityApiEndpoint } from './session-78c3c986.js';
import { D as DetailViewConfig } from './detail-view-config-6178fa2c.js';
import './index-47482faa.js';
import { b as convertFieldsToFieldReferences, d as getPairConfigOptionsFromOrganisationConfig, e as commonUserInfoFields, g as getFieldsToShow, M as MentornityBridgeApi } from './common-wrapper-util-3e52e361.js';
import './pair-config-6cadad1f.js';
import { p as pairConfig } from './pair-config-ff3c76f5.js';

class AtamaMemberPairsOverviewViewConfig extends DvnAppViewConfig {
  static decode(json) {
    const model = Object.assign(Object.create(AtamaMemberPairsOverviewViewConfig.prototype), json);
    model.table = TableConfig.decode(model.table);
    return model;
  }
}

const en = {
  view: {
    accept: 'Accept',
    fetchConfigurationError: 'Getting configuration failed.',
    fetchListError: 'Fetching list failed.',
    noPairsMessage: 'You don\'t have any pairs.',
    noReceivedRequests: 'There are no pending requests.',
    pairs: 'Pairs',
    receivedRequests: 'Received Requests',
    reject: 'Reject',
    selectionCardActionTint: 'secondary',
    selectionCardActionLabel: 'Go to selection page',
    selectionCardMessage: 'You can view all available users and send them requests.',
    selectionCardTint: 'primary',
    selectionCardTitle: 'Selection is on!',
    sentOnDateFormat: 'DD MMM YYYY, HH:mm',
    statusLabels: {
      accepted: 'Accepted',
      canceled: 'Canceled',
      completed: 'Completed',
      rejected: 'Rejected',
      requested: 'Requested',
    },
    viewAndMeet: 'View & Meet',
  },
};
const tr = {
  view: {
    accept: 'Kabul Et',
    fetchConfigurationError: 'Ayarlar alınırken hata oluştu.',
    fetchListError: 'Seçim listesi alınırken hata oluştu.',
    noPairsMessage: 'Eşleştiğiniz kimse yok.',
    noReceivedRequests: 'Onay bekleyen istek yok.',
    pairs: 'Eşleşmeler',
    receivedRequests: 'Alınan İstekler',
    reject: 'Reddet',
    selectionCardActionTint: 'secondary',
    selectionCardActionLabel: 'Seçim sayfasına git',
    selectionCardMessage: 'Müsait olan tüm kullanıcıları görüp onlara istek gönderebilirsiniz.',
    selectionCardTint: 'primary',
    selectionCardTitle: 'Seçim başladı!',
    sentOnDateFormat: 'DD MMM YYYY, HH:mm',
    statusLabels: {
      accepted: 'Onaylandı',
      canceled: 'Reddedildi',
      completed: 'Sonlandırıldı',
      rejected: 'Reddedildi',
      requested: 'Gönderildi',
    },
    viewAndMeet: 'Detay & Toplantılar',
  },
};

function getPairOverviewParameters(mentornityCookieContext) {
  const { role } = mentornityCookieContext;
  // if user is mentor, then the view config should say that the 'subscribed' is viewing the 'subscriber'
  // if user is mentee, then it should say otherwise
  const userRefFieldIdValue = role === 'mentor' ? 'subscribed' : 'subscriber';
  const memberToViewRefFieldIdValue = role === 'mentor' ? 'subscriber' : 'subscribed';
  return {
    memberToViewRefFieldId: memberToViewRefFieldIdValue,
    userRefFieldId: userRefFieldIdValue,
    viewIdToNavigateToSelectPair: 'pair-selection-page',
    viewIdToViewRequestSender: 'user-page',
  };
}
function getPairOverviewViewConfig(mentornityCookieContext, language, config) {
  if (!config) {
    return null;
  }
  const i18n = language === 'tr' ? tr : en;
  const { role } = mentornityCookieContext;
  const viewingGroups = role === 'mentor' && config.menteeType === 'group';
  const profileFieldVisibility = role === 'mentor' ? config.profileFieldVisibilities.mentee : config.profileFieldVisibilities.mentor;
  const defaultUserFields = ['title', 'bio', 'skills', 'imagePlaceholderValue'].filter(id => profileFieldVisibility[id]).map(id => ({ fieldId: id }));
  const defaultGroupFields = ['description', 'imagePlaceholderValue'].map(id => ({ fieldId: id }));
  const fields = viewingGroups ? [] : getFieldsToShow(role, config);
  const cardBodyFields = (viewingGroups ? defaultGroupFields : [...defaultUserFields, ...convertFieldsToFieldReferences(fields)])
    .slice(0, 3)
    .map(fieldConfig => (Object.assign(Object.assign({}, fieldConfig), { lines: 1 }))) // to trim long texts
    .filter(field => !['fullName', 'imageUrl', 'imagePlaceholderValue'].includes(field.fieldId));
  return AtamaMemberPairsOverviewViewConfig.decode({
    _id: 'pair-overview-page',
    ui: {
      i18n: i18n.view,
      main: {
        cardViewConfig: {
          header: {
            cover: {
              size: 100,
            },
            image: {
              fieldId: 'imageUrl',
              placeholderFieldId: 'imagePlaceholderValue',
              shape: viewingGroups ? 'rounded' : undefined,
              size: 72,
            },
            title: {
              fieldId: viewingGroups ? 'title' : 'fullName',
            },
          },
          body: {
            fields: cardBodyFields,
          },
          footer: {
            actions: [
              {
                data: {
                  type: 'navigation',
                  direction: 'forward',
                  placement: 'view',
                  viewId: 'subscriptions',
                  params: {
                    conversionConfig: {
                      'item._id': 'memberToViewRefFieldValue',
                      'item.subscription._id': 'pairId',
                    }
                  }
                },
                ui: {
                  label: i18n.view.viewAndMeet || 'View & Meet',
                  name: 'open-pair-detail-view',
                  view: 'ghost',
                }
              }
            ]
          }
        },
      },
    },
    params: {},
    table: pairConfig(config, undefined, getPairConfigOptionsFromOrganisationConfig(config)),
  });
}
function getUserDetailDialogParams(actionData) {
  return {
    itemId: actionData.params.item._id,
  };
}
function getUserDetailDialogViewConfig(mentornityCookieContext, config) {
  if (!config) {
    return undefined;
  }
  const { organisationId, role } = mentornityCookieContext;
  const profileFieldVisibility = role === 'mentor' ? config.profileFieldVisibilities.mentee : config.profileFieldVisibilities.mentor;
  const defaultUserFields = ['title', 'bio', 'skills'].filter(id => profileFieldVisibility[id]).map(id => ({ fieldId: id }));
  const fields = getFieldsToShow(role, config);
  const detailViewMembershipFields = decodeFields([...commonUserInfoFields, ...fields]);
  const detailViewCardFields = [...defaultUserFields, ...convertFieldsToFieldReferences(fields)]
    .filter(field => !['fullName', 'imageUrl'].includes(field.fieldId));
  const showMessageButton = [
    InfoVisibility.allMentees,
    InfoVisibility.everyone,
  ].includes(config.conversationPermissions.mentor);
  return DetailViewConfig.decode({
    table: TableConfig.decode({
      base: organisationId,
      name: 'mentees',
    }),
    ui: {
      fields: detailViewMembershipFields,
      cardConfig: {
        header: {
          title: {
            fieldId: 'fullName',
          },
          actions: showMessageButton ? [
            {
              ui: {
                label: 'Send Message',
                name: 'send-message',
              }
            }
          ] : [],
        },
        body: {
          fields: detailViewCardFields,
        }
      }
    },
  });
}

const MentornityPairsOverviewWrapper = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
  }
  onAction(event) {
    const action = event.detail;
    if (action.type === 'navigation') {
      const navigateAction = action;
      if (navigateAction.direction === 'forward') {
        const navigateForwardAction = action;
        if (navigateForwardAction.name === 'open-pair-detail-view') {
          const { memberToViewRefFieldValue, pairId } = navigateForwardAction.params;
          // called when mentor/mentee clicks the "View User & Meet" button on the user card
          window.dispatchEvent(new CustomEvent('dvn.atama-pairs.open-pair-detail', {
            detail: {
              userId: memberToViewRefFieldValue,
              pairId,
            }
          }));
        }
        if (navigateForwardAction.viewId === 'pair-selection-page') {
          // called when mentee clicks the "Go to mentor selection page" on the top announcement card
          window.dispatchEvent(new CustomEvent('dvn.atama-pairs.open-pair-selection-page', {}));
        }
        if (navigateForwardAction.viewId === 'user-page') {
          // called when mentor clicks the user information from the received requests list
          this.viewUserDetailDialogActionData = navigateForwardAction;
        }
      }
    }
    if (action.name === 'send-message') {
      // called when mentor clicks the "Send Message" button from the user detail dialog
      window.dispatchEvent(new CustomEvent('dvn.atama-pairs.send-message', {
        detail: {
          user: action.params.item.user,
        }
      }));
      this.viewUserDetailDialogActionData = undefined;
    }
  }
  componentWillLoad() {
    const { context, headers } = getSessionDetails();
    this.mentornityBridgeApi = new MentornityBridgeApi(mentornityApiEndpoint, context, headers);
    createMentornityApiInstance(mentornityApiEndpoint, headers)
      .getOrganisationConfig(context.organisationId)
      .then((response) => {
      this.organisationConfig = Config.decode(response);
    });
  }
  render() {
    const { context } = getSessionDetails();
    const { language, mentornityBridgeApi, organisationConfig, viewUserDetailDialogActionData } = this;
    const pairOverviewViewConfig = getPairOverviewViewConfig(context, language, organisationConfig);
    if (!pairOverviewViewConfig) {
      return null;
    }
    return (h("mentornity-root-wrapper", null, h("dvn-app-view-atama-member-pairs-overview-view", { apiAtama: mentornityBridgeApi, apiUye: mentornityBridgeApi, membership: DataModel.decode({}), params: getPairOverviewParameters(context), view: pairOverviewViewConfig }), viewUserDetailDialogActionData &&
      h("dvn-dialog", { raw: true, shown: true, onDismiss: () => this.viewUserDetailDialogActionData = undefined }, h("dvn-app-view-detail-view", { apiData: mentornityBridgeApi, id: "user-page", params: getUserDetailDialogParams(viewUserDetailDialogActionData), view: getUserDetailDialogViewConfig(context, organisationConfig) }))));
  }
};

export { MentornityPairsOverviewWrapper as mentornity_pairs_overview_wrapper };
