'use strict';

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

const index = require('./index-7cdf1df1.js');
const index$1 = require('./index-c2a077ae.js');
require('./InterceptorHandler-09a1e84b.js');
const dataModel = require('./data-model-f9df493f.js');
require('./app-config-9060e777.js');
require('./BaseField-f50bb8f0.js');
require('./FieldModelGenerator-5a321a60.js');
const date = require('./date-8e8ca89a.js');
const index$2 = require('./index-e79eb186.js');
require('./table-config-b20b4da1.js');
require('./string-9e6ebf63.js');
require('./membership-config-47f4733e.js');
require('./_commonjsHelpers-206db00d.js');
require('./index-13fb62eb.js');
const kimlikApi = require('./kimlikApi-6a581864.js');
require('./data-transfer-field-025bce2f.js');
require('./filter-52876f3c.js');
require('./cookie-8a49bf23.js');
require('./index-17c935e7.js');
require('./form-view-config-cc5244bd.js');
const viewConfig$1 = require('./view-config-f8b4b929.js');
require('./actions-d7eabd95.js');
require('./services-895a8d1d.js');
require('./attachment-c2bc9126.js');
const session = require('./session-a266bfc2.js');
require('./spaces-5c8e9d99.js');
require('./index-35a4d634.js');
const tableListViewConfig = require('./table-list-view-config-1c3d8326.js');
require('./nested-14baa4ef.js');
const tableListViewDomain = require('./table-list-view-domain-32cf51ac.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 kimlikApi.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.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) {
    index.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 } = session.getSessionDetails();
    this.api = new MembershipApplicationWrapperBridgeApi(organisationId, this.language, session.mentornityApiEndpoint, headers);
  }
  render() {
    const { api, language } = this;
    const i18n = language === 'tr' ? tr : en;
    return (index.h("mentornity-root-wrapper", { class: "d-flex flex-column align-items-center" }, index.h("dvn-app-view-list-view-table", { ref: (el) => this.listView = el, id: tableListViewDomain.ID, apiData: api, view: viewConfig(i18n) })));
  }
};
const viewConfig = (i18n) => tableListViewConfig.TableListViewConfig.decode({
  _id: tableListViewDomain.ID,
  type: viewConfig$1.AppViewType.ListViewTable,
  base: 'mentornity',
  title: 'Applications',
  space: tableListViewDomain.damgaSpace,
  table: {
    fields: [
      new index$1.TextField('firstName', i18n.firstName),
      new index$1.TextField('lastName', i18n.lastName),
      new index$1.TextField('email', i18n.email),
      new date.DateField('createdAt', i18n.date).setDataType(date.DateDataType.Unix).setDateFormat('DD MMM YYYY'),
    ],
  },
  query: {},
  ui: tableListViewConfig.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 index$2.SingleChoiceField('role', 'Role', [
            new index$2.SingleChoiceFieldOption('Mentor applications', 'mentors'),
            new index$2.SingleChoiceFieldOption('Mentee applications', 'mentees'),
          ]).setValue('mentors'),
        }
      ],
      ui: {
        role: {
          width: '200px',
        }
      }
    },
  }),
});

exports.mentornity_membership_application_list_wrapper = MentornityMembershipApplicationListWrapper;
