import { Component, Element, Event, h, Listen, Prop, State } from "@stencil/core";
import * as translation from "./translation";
export class IntergiroPortalSettlementTable {
  constructor() {
    this.count = 100;
    this.selected = {};
    this.opened = [];
  }
  componentWillLoad() {
    this.t = translation.create(this.element);
  }
  onChecked(event) {
    this.selected = {};
    if (event.detail.selected && this.settlements)
      for (const settlement of this.settlements)
        this.selected[settlement.reference] = settlement;
    this.selectedChanged.emit(this.selected);
    event.stopPropagation();
    event.preventDefault();
  }
  handleToggle(event) {
    const settlements = Object.assign({}, this.selected);
    if (event.detail.checked)
      settlements[event.detail.settlement.reference] = event.detail.settlement;
    else
      delete settlements[event.detail.settlement.reference];
    this.selectedChanged.emit((this.selected = settlements));
  }
  viewOrders(event) {
    const opened = [...this.opened];
    opened.includes(event.detail) ? delete opened[opened.indexOf(event.detail)] : opened.push(event.detail);
    this.opened = opened;
  }
  render() {
    const t = this.t;
    let result;
    if (!this.settlements)
      result = h("p", null, t("Loading settlements from server."));
    else if (this.settlements.length <= 0)
      result = h("p", null, t("There are no settlements to display."));
    else {
      result = [
        h("table", null,
          h("thead", null,
            h("tr", null,
              h("th", { style: { width: "5%" }, color: "light" },
                h("smoothly-checkbox", { "select-all": true })),
              h("th", { color: "light" }, this.t("Reference number")),
              h("th", { color: "light" }, this.t("Start date")),
              h("th", { color: "light" }, this.t("End date")),
              h("th", { color: "light" }, this.t("Payout date")),
              h("th", { color: "light", class: "right" }, this.t("Gross")),
              h("th", { color: "light", class: "right" }, this.t("Fee")),
              this.interchange && (h("th", { color: "light", class: "right" }, this.t("Interchange"))),
              h("th", { color: "light", class: "right" }, this.t("Net")))),
          this.settlements
            ? this.settlements
              .slice(0, this.count)
              .map(settlement => [
              h("ig-portal-settlement", { interchange: this.interchange, settlement: settlement, selected: this.selected.hasOwnProperty.call(this.selected, settlement.reference) }),
              this.opened.includes(settlement.reference) ? (h("ig-portal-settlement-orders", { settlement: settlement, interchange: this.interchange })) : ([]),
            ])
            : null,
          h("td", { colSpan: 8 + (this.interchange ? 1 : 0) }, this.count < 1 ? (h("button", { onClick: () => (this.count += 100) },
            h("smoothly-icon", { name: "chevron-down-circle", size: "large", toolTip: "Load more settlements" }))) : (""))),
      ];
    }
    return result;
  }
  static get is() { return "ig-portal-settlement-table"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "settlements": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Settlement[]",
        "resolved": "Settlement[]",
        "references": {
          "Settlement": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "interchange": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "interchange",
      "reflect": false
    }
  }; }
  static get states() { return {
    "count": {},
    "selected": {},
    "opened": {},
    "t": {}
  }; }
  static get events() { return [{
      "method": "selectedChanged",
      "name": "selectedChanged",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }]; }
  static get elementRef() { return "element"; }
  static get listeners() { return [{
      "name": "checked",
      "method": "onChecked",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "settlementSelected",
      "method": "handleToggle",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "viewOrders",
      "method": "viewOrders",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
