import { APIBase } from '@dvn/core/api/apiBase';
import { TextField } from '@dvn/core/doldurma/src/fields/text';
import { DataModel } from '@dvn/core/models/data-model';
import { TableListViewConfig } from '@dvn/core/models/table-list-view-config';
import { AppViewType } from '@dvn/core/models/view-config';
import { SozlukApi } from '@dvn/sozluk/api';
import { Component, h, Host, Prop, State } from '@stencil/core';
import { mentornityApiEndpoint } from '../../constants/env';
import { getSessionDetails } from '../../util/session';
import { sozlukEntries } from '../sozluk-entries';
import { mentornityAnnouncementWrapperI18nDefault, MentornityAnnouncementWrapperI18nKey } from './i18n';
class MentornityAnnouncementReportBridgeApi {
  constructor(announcementId, endpoint, headers, strings) {
    this.client = new APIBase(endpoint, headers);
    this.announcementId = announcementId;
    this.strings = strings;
  }
  addAuthInterceptor(_token) {
    // console.log('MBA.addAuthInterceptor', token);
  }
  // region DataApi
  get(_collection, _id, _query) {
    return undefined;
  }
  update(_collection, _id, _body) {
    return undefined;
  }
  async query(_collection) {
    return new Promise((resolve => {
      this.client.get(`announcements/${this.announcementId}/email-report`).then(response => {
        resolve({
          results: response.results.map((item) => {
            var _a, _b;
            return DataModel.decode(Object.assign(Object.assign({ _id: item.recipient[0]._id }, (item.recipient[0] || {})), { firstName: item.recipient[0].firstName, lastName: item.recipient[0].lastName, email: item.recipient[0].email, status: ((_a = item.metadata) === null || _a === void 0 ? void 0 : _a.delivered) ? ((_b = item.metadata) === null || _b === void 0 ? void 0 : _b.opened) ? this.strings.opened : this.strings.delivered : this.strings.pending }));
          }),
          meta: {
            total: response.results.length,
          }
        });
      }).catch(e => {
        console.log(e);
      });
    }));
  }
}
export class MentornityAnnouncementReport {
  constructor() {
    this.setDictionary = async () => {
      const apiSozluk = new SozlukApi().setLanguage(this.language).setContents(sozlukEntries);
      this.strings = await apiSozluk.getContent(MentornityAnnouncementWrapperI18nKey, mentornityAnnouncementWrapperI18nDefault);
    };
  }
  async componentWillLoad() {
    const { headers } = getSessionDetails();
    await this.setDictionary();
    this.api = new MentornityAnnouncementReportBridgeApi(this.announcementId, mentornityApiEndpoint, headers, this.strings);
    this.apiSozluk = new SozlukApi().setLanguage(this.language).setContents(sozlukEntries);
  }
  render() {
    const listViewConfig = viewConfig(this.strings);
    return (h(Host, { class: "row", style: { overflow: 'hidden' } },
      h("div", { class: "col-12" },
        h("dvn-app-view-list-view-table", { apiData: this.api, apiSozluk: this.apiSozluk, view: listViewConfig, style: { height: '500px' } }))));
  }
  static get is() { return "mentornity-announcement-report"; }
  static get properties() { return {
    "announcementId": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "announcement-id",
      "reflect": false
    },
    "language": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "language",
      "reflect": false
    }
  }; }
  static get states() { return {
    "strings": {}
  }; }
}
const viewConfig = (i18n) => {
  const columns = {
    'firstName': {
      width: 200
    },
    'lastName': {
      width: 200
    },
    'email': {
      width: 200
    }
  };
  return TableListViewConfig.decode({
    _id: 'mentornity-announcement-report',
    i18nEntryId: 'mentornity-announcement-report',
    type: AppViewType.ListViewTable,
    base: 'mentornity3',
    title: i18n.mailingReport,
    table: {
      fields: [
        new TextField('firstName', i18n.firstName),
        new TextField('lastName', i18n.lastName),
        new TextField('email', i18n.email),
        new TextField('status', i18n.status),
      ],
    },
    query: {},
    ui: TableListViewConfig.decode({
      main: {
        actionsPlacement: 'left',
        fields: [
          { fieldId: 'firstName' },
          { fieldId: 'lastName' },
          { fieldId: 'email' },
          { fieldId: 'status' },
        ],
        columns
      }
    })
  });
};
