import { r as registerInstance, h } from './index-acdc1ac0.js';
import { T as TextField } from './index-d9d18e9c.js';
import './InterceptorHandler-6474bcf4.js';
import { D as DataModel } from './data-model-bb5789f5.js';
import './app-config-cf28072f.js';
import './BaseField-d94c5b14.js';
import './FieldModelGenerator-755e3a85.js';
import { D as DateField, a as DateDataType } from './date-b270be76.js';
import { S as SingleChoiceField, a as SingleChoiceFieldOption } from './index-a95fa2f4.js';
import './table-config-9f3dee34.js';
import './string-2d991277.js';
import './membership-config-1607e716.js';
import './_commonjsHelpers-63c253cd.js';
import './index-fd2b14ae.js';
import { A as APIBase } from './kimlikApi-81acfec6.js';
import './data-transfer-field-1ad95e08.js';
import './filter-cbaa3d7d.js';
import './cookie-d9098150.js';
import './index-8fb87ec4.js';
import './form-view-config-46b99ec4.js';
import { A as AppViewType } from './view-config-2a7feb02.js';
import './actions-b9a0f808.js';
import './services-fc4ce8f3.js';
import './attachment-8831acff.js';
import { g as getSessionDetails, m as mentornityApiEndpoint } from './session-78c3c986.js';
import './spaces-8bb938bc.js';
import './index-c43f1f2e.js';
import { T as TableListViewConfig } from './table-list-view-config-4385d962.js';
import './nested-15d95467.js';
import { I as ID, d as damgaSpace } from './table-list-view-domain-8b256b38.js';

const en = {
  applicationIsSent: 'Your application is sent.',
  applicationFailed: 'Sending the application failed.',
  firstName: 'First name',
  lastName: 'Last name',
  email: 'Email',
  date: 'Date',
  sendApplication: 'Send Application',
};
const tr = {
  applicationIsSent: 'Başvurunuz gönderildi.',
  applicationFailed: 'Başvuru gönderilirken hata oluştu.',
  firstName: 'İsim',
  lastName: 'Soyisim',
  email: 'E-posta',
  date: 'Tarih',
  sendApplication: 'Başvuruyu Gönder',
};

class MembershipApplicationWrapperBridgeApi {
  constructor(organisationId, language, endpoint, headers) {
    this.client = new APIBase(endpoint, headers);
    this.organisationId = organisationId;
    this.language = language;
  }
  addAuthInterceptor(_token) {
    // console.log('MBA.addAuthInterceptor', token);
  }
  // region DataApi
  get(_collection, _id, _query) {
    return undefined;
  }
  query(_collection, query) {
    var _a;
    const role = ((_a = query.filter) === null || _a === void 0 ? void 0 : _a.filters[0].value) || 'mentors';
    return new Promise((resolve => {
      this.client.get(`organisations/${this.organisationId}/${role}/applications`).then(response => {
        resolve({
          results: response.results.map(item => DataModel.decode(Object.assign(Object.assign({ _id: item._id, createdAt: item.createdAt, applicationStatus: item.applicationStatus }, (item.data || {})), { 
            // inserted for mentornity-accept-application-button
            role, language: this.language })))
        });
      }).catch(e => {
        console.log(e);
      });
    }));
  }
  update(_collection, _id, _body) {
    return undefined;
  }
}

const MentornityMembershipApplicationListWrapper = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.isProgressing = false;
  }
  /**
   * Called by mentornity-accept-application-button when an application is accepted.
   * Invalidates the table view to refresh its data.
   * @param event
   */
  onAction(event) {
    this.listView.invalidate(event);
  }
  componentWillLoad() {
    const { context: { organisationId }, headers } = getSessionDetails();
    this.api = new MembershipApplicationWrapperBridgeApi(organisationId, this.language, mentornityApiEndpoint, headers);
  }
  render() {
    const { api, language } = this;
    const i18n = language === 'tr' ? tr : en;
    return (h("mentornity-root-wrapper", { class: "d-flex flex-column align-items-center" }, h("dvn-app-view-list-view-table", { ref: (el) => this.listView = el, id: ID, apiData: api, view: viewConfig(i18n) })));
  }
};
const viewConfig = (i18n) => TableListViewConfig.decode({
  _id: ID,
  type: AppViewType.ListViewTable,
  base: 'mentornity',
  title: 'Applications',
  space: damgaSpace,
  table: {
    fields: [
      new TextField('firstName', i18n.firstName),
      new TextField('lastName', i18n.lastName),
      new TextField('email', i18n.email),
      new DateField('createdAt', i18n.date).setDataType(DateDataType.Unix).setDateFormat('DD MMM YYYY'),
    ],
  },
  query: {},
  ui: TableListViewConfig.decode({
    main: {
      fields: [
        { fieldId: 'createdAt' },
        { fieldId: 'email' },
        { fieldId: 'firstName' },
        { fieldId: 'lastName' },
        { fieldDisplay: { id: 'actions' }, componentTag: 'mentornity-accept-application-button' },
      ],
      columns: {
        'createdAt': {
          width: 150,
        },
        'email': {
          width: 250,
        },
        'firstName': {
          width: 100,
        },
        'lastName': {
          width: 100,
        },
        'actions': {
          width: 150,
        },
      },
    },
    quickSearch: {
      fields: [
        {
          field: new SingleChoiceField('role', 'Role', [
            new SingleChoiceFieldOption('Mentor applications', 'mentors'),
            new SingleChoiceFieldOption('Mentee applications', 'mentees'),
          ]).setValue('mentors'),
        }
      ],
      ui: {
        role: {
          width: '200px',
        }
      }
    },
  }),
});

export { MentornityMembershipApplicationListWrapper as mentornity_membership_application_list_wrapper };
