import { DateField } from '@dvn/core/doldurma/src/fields/date';
import { DateDataType } from '@dvn/core/doldurma/src/fields/date/date';
import { SingleChoiceField, SingleChoiceFieldOption } from '@dvn/core/doldurma/src/fields/single-choice';
import { TextField } from '@dvn/core/doldurma/src/fields/text';
import { TableListViewConfig } from '@dvn/core/models/table-list-view-config';
import { AppViewType } from '@dvn/core/models/view-config';
import { Component, h, Listen, Prop, State } from '@stencil/core';
import { damgaSpace } from '../../../../devingen/products/damga/configs/data-configs';
import * as tableListViewDomain from '../../../../devingen/products/damga/configs/view-configs/table-list-view-domain';
import { ID } from '../../../../devingen/products/damga/configs/view-configs/table-list-view-domain';
import { mentornityApiEndpoint } from '../../constants/env';
import { getSessionDetails } from '../../util/session';
import { en, tr } from './i18n';
import { MembershipApplicationWrapperBridgeApi } from './membership-application-wrapper-bridge-api';
export class MentornityMembershipApplicationListWrapper {
  constructor() {
    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: tableListViewDomain.ID, apiData: api, view: viewConfig(i18n) })));
  }
  static get is() { return "mentornity-membership-application-list-wrapper"; }
  static get properties() { return {
    "language": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Used to show proper translations, passed by the parent component."
      },
      "attribute": "language",
      "reflect": false
    }
  }; }
  static get states() { return {
    "error": {},
    "isProgressing": {}
  }; }
  static get listeners() { return [{
      "name": "acceptApplication",
      "method": "onAction",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
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',
        }
      }
    },
  }),
});
