import { DateField } from '@dvn/core/doldurma/src/fields/date';
import { RawDataField } from '@dvn/core/doldurma/src/fields/raw-data';
import { TextField } from '@dvn/core/doldurma/src/fields/text';
import { DataModel } from '@dvn/core/models/data-model';
import { ListFetchState } from '@dvn/core/models/list-fetch-state';
import { Component, h, Host, Prop, State } from '@stencil/core';
export class DamgaTargetGroupList {
  constructor() {
    this.list = ListFetchState.init();
  }
  componentWillLoad() {
    this.fetchList();
  }
  async fetchList() {
    const { list } = this;
    this.list = this.list.beforeFetch();
    const response = await this.apiDamga.getTargetGroups().catch(error => {
      this.list = list.afterFetchError(error);
    });
    if (!response)
      return;
    this.list = list.afterFetch(response, false);
  }
  render() {
    const { list } = this;
    // show the first target value as the only target value in the list
    const modifiedList = (list.data || []).map(item => (DataModel.decode(Object.assign(Object.assign({}, item), { target: item.targets.map(target => target.value).join(', ') })))).sort((a, b) => a.name.localeCompare(b.name));
    return (h(Host, { class: "container d-flex flex-grow-1 flex-column" },
      h("dvn-list-view-table", { config: {
          actions: [
            {
              ui: {
                name: 'edit',
                buttonType: 'icon-button',
                iconName: 'uil uil-pen',
                view: 'flat',
              },
            },
            {
              ui: {
                name: 'delete',
                buttonType: 'icon-button',
                iconName: 'uil uil-trash',
                view: 'flat',
                tint: 'danger',
              },
            },
          ],
          fields: [
            { fieldId: 'name' },
            { fieldId: 'target' },
            { fieldId: '_created' },
            { fieldId: '_id' },
          ],
          columns: {
            _created: {
              width: 150
            }
          }
        }, fields: [
          new TextField('name', 'Name'),
          new RawDataField('target', 'Target').setTrimStringQuotes(true),
          new RawDataField('_id', 'ID'),
          new DateField('_created', 'Creation Date'),
        ], items: modifiedList, isEndReached: list.isEndReached(), isProgressing: list.isPending, error: list.error ? 'Fetching list failed' : undefined, onRetry: () => this.fetchList(), scrollToFit: true })));
  }
  static get is() { return "damga-target-group-list"; }
  static get properties() { return {
    "apiDamga": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "DamgaApi",
        "resolved": "DamgaApi",
        "references": {
          "DamgaApi": {
            "location": "import",
            "path": "../../api/damga-api"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "list": {}
  }; }
}
