import { Component, Element, h, Listen, Prop, State, Watch } from "@stencil/core";
import { Currency, Date } from "isoly";
import * as translation from "./translation";
export class IntergiroMonitorSettlements {
  constructor() {
    this.settlementsGrouped = {};
    this.opened = {};
    this.view = "multiple";
    this.selected = {};
    this.selectedSettlements = [];
  }
  componentWillLoad() {
    this.t = translation.create(this.element);
    this.settlementsChanged();
  }
  settlementsChanged() {
    var _a;
    this.settlementsFiltered = (_a = this.settlements) === null || _a === void 0 ? void 0 : _a.sort((a, b) => a.reference.slice(0.1) < b.reference.slice(0.1) ? 1 : -1);
  }
  handleFilterChanged(event) {
    this.settlementsFiltered = event.detail;
    event.stopPropagation();
  }
  toggleColumn(e) {
    const opened = Object.assign({}, this.opened);
    opened[e.detail.index] == e.detail.column
      ? delete opened[e.detail.index]
      : (opened[e.detail.index] = e.detail.column);
    this.opened = opened;
    e.stopPropagation();
  }
  groupSettlements() {
    var _a;
    const registry = {};
    (_a = this.settlementsFiltered) === null || _a === void 0 ? void 0 : _a.forEach(settlement => {
      var _a;
      let date = settlement.reference.slice(0, 10);
      date = Date.is(date) ? date : settlement.created;
      return (registry[date] = [...((_a = registry[date]) !== null && _a !== void 0 ? _a : []), settlement]);
    });
    this.settlementsGrouped = registry;
  }
  calculateFees(settlements) {
    const result = [];
    settlements === null || settlements === void 0 ? void 0 : settlements.map(s => s.transactions.map(t => {
      const exists = result.find(v => v.scheme == t.scheme && v.currency == s.currency);
      if (exists) {
        exists.gross += t.gross;
        exists.fee += typeof t.fee == "number" ? t.fee : t.fee.total;
        exists.net += t.net;
        exists.interchange += typeof t.fee == "number" ? 0 : t.fee.scheme;
      }
      else
        result.push({
          scheme: t.scheme,
          gross: t.gross,
          fee: typeof t.fee == "number" ? t.fee : t.fee.total,
          net: t.net,
          interchange: typeof t.fee == "number" ? 0 : t.fee.scheme,
          currency: s.currency,
        });
    }));
    return result;
  }
  handleSelectedChanged(event) {
    this.selected = event.detail["allSelected"] ? Object.assign({}, this.settlementsGrouped) : event.detail;
    this.selectedSettlements = Object.values(this.selected).reduce((s, r) => r.concat(s), []);
  }
  render() {
    var _a;
    const t = this.t;
    const isSelected = this.selectedSettlements.length > 0;
    return [
      this.settlements && h("ig-filter", { value: this.settlements }),
      h("ig-table", { selectable: true, selected: this.selected, value: this.settlements, name: t("settlements"), header: [{ name: t("date") }, { name: t("transactions") }, { name: t("total"), align: "right" }] }, (_a = Object.entries(this.settlementsGrouped)) === null || _a === void 0 ? void 0 : _a.map((s, index) => {
        const fees = this.calculateFees(s[1]);
        const fee = Currency.round(fees.reduce((r, f) => { var _a, _b; return (r += ((_b = (_a = this.conversion) === null || _a === void 0 ? void 0 : _a["SEK"][f.currency]) !== null && _b !== void 0 ? _b : 1) * f.gross); }, 0), "SEK");
        return [
          h("ig-table-row", { row: [
              { column: s[0] },
              {
                column: s[1].reduce((r, s) => (r += s.transactions.length), 0) + " " + t("transactions"),
                openable: true,
              },
              {
                column: h("smoothly-display-amount", { amount: fee, currency: "SEK" }),
                openable: fees.length > 0,
                align: "right",
              },
            ], identifier: s[0], value: s[1], index: index, selected: !!this.selected[s[0]], selectable: true, opened: this.opened[index] }),
          h("ig-monitor-settlements-details", { settlement: s[1], fees: fees, opened: this.opened[index], "cost-plus": this.costPlus, view: this.view }),
        ];
      })),
      h("ig-monitor-settlements-footer", { settlements: isSelected ? this.selectedSettlements : this.settlementsFiltered, selected: isSelected, authorizations: this.authorizations, fees: this.calculateFees(isSelected ? this.selectedSettlements : this.settlementsFiltered), conversion: this.conversion }),
    ];
  }
  static get is() { return "ig-monitor-settlements"; }
  static get encapsulation() { return "scoped"; }
  static get properties() { return {
    "settlements": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "AcquirerSettlement[]",
        "resolved": "Settlement[] | undefined",
        "references": {
          "AcquirerSettlement": {
            "location": "import",
            "path": "../model"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "authorizations": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Authorization[]",
        "resolved": "Authorization[] | undefined",
        "references": {
          "Authorization": {
            "location": "import",
            "path": "@payfunc/model-acquirer"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "conversion": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Record<Currency, Partial<Record<Currency, number>>>",
        "resolved": "undefined | { AED: Partial<Record<Currency, number>>; AFN: Partial<Record<Currency, number>>; ALL: Partial<Record<Currency, number>>; AMD: Partial<Record<Currency, number>>; ANG: Partial<Record<Currency, number>>; AOA: Partial<Record<Currency, number>>; ARS: Partial<Record<Currency, number>>; AUD: Partial<Record<Currency, number>>; AWG: Partial<Record<Currency, number>>; AZN: Partial<Record<Currency, number>>; BAM: Partial<Record<Currency, number>>; BBD: Partial<Record<Currency, number>>; BDT: Partial<Record<Currency, number>>; BGN: Partial<Record<Currency, number>>; BHD: Partial<Record<Currency, number>>; BIF: Partial<Record<Currency, number>>; BMD: Partial<Record<Currency, number>>; BND: Partial<Record<Currency, number>>; BOB: Partial<Record<Currency, number>>; BOV: Partial<Record<Currency, number>>; BRL: Partial<Record<Currency, number>>; BSD: Partial<Record<Currency, number>>; BTN: Partial<Record<Currency, number>>; BWP: Partial<Record<Currency, number>>; BYN: Partial<Record<Currency, number>>; BZD: Partial<Record<Currency, number>>; CAD: Partial<Record<Currency, number>>; CDF: Partial<Record<Currency, number>>; CHE: Partial<Record<Currency, number>>; CHF: Partial<Record<Currency, number>>; CHW: Partial<Record<Currency, number>>; CLF: Partial<Record<Currency, number>>; CLP: Partial<Record<Currency, number>>; CNY: Partial<Record<Currency, number>>; COP: Partial<Record<Currency, number>>; COU: Partial<Record<Currency, number>>; CRC: Partial<Record<Currency, number>>; CUC: Partial<Record<Currency, number>>; CUP: Partial<Record<Currency, number>>; CVE: Partial<Record<Currency, number>>; CZK: Partial<Record<Currency, number>>; DJF: Partial<Record<Currency, number>>; DKK: Partial<Record<Currency, number>>; DOP: Partial<Record<Currency, number>>; DZD: Partial<Record<Currency, number>>; EGP: Partial<Record<Currency, number>>; ERN: Partial<Record<Currency, number>>; ETB: Partial<Record<Currency, number>>; EUR: Partial<Record<Currency, number>>; FJD: Partial<Record<Currency, number>>; FKP: Partial<Record<Currency, number>>; GBP: Partial<Record<Currency, number>>; GEL: Partial<Record<Currency, number>>; GHS: Partial<Record<Currency, number>>; GIP: Partial<Record<Currency, number>>; GMD: Partial<Record<Currency, number>>; GNF: Partial<Record<Currency, number>>; GTQ: Partial<Record<Currency, number>>; GYD: Partial<Record<Currency, number>>; HKD: Partial<Record<Currency, number>>; HNL: Partial<Record<Currency, number>>; HRK: Partial<Record<Currency, number>>; HTG: Partial<Record<Currency, number>>; HUF: Partial<Record<Currency, number>>; IDR: Partial<Record<Currency, number>>; ILS: Partial<Record<Currency, number>>; INR: Partial<Record<Currency, number>>; IQD: Partial<Record<Currency, number>>; IRR: Partial<Record<Currency, number>>; ISK: Partial<Record<Currency, number>>; JMD: Partial<Record<Currency, number>>; JOD: Partial<Record<Currency, number>>; JPY: Partial<Record<Currency, number>>; KES: Partial<Record<Currency, number>>; KGS: Partial<Record<Currency, number>>; KHR: Partial<Record<Currency, number>>; KMF: Partial<Record<Currency, number>>; KPW: Partial<Record<Currency, number>>; KRW: Partial<Record<Currency, number>>; KWD: Partial<Record<Currency, number>>; KYD: Partial<Record<Currency, number>>; KZT: Partial<Record<Currency, number>>; LAK: Partial<Record<Currency, number>>; LBP: Partial<Record<Currency, number>>; LKR: Partial<Record<Currency, number>>; LRD: Partial<Record<Currency, number>>; LSL: Partial<Record<Currency, number>>; LYD: Partial<Record<Currency, number>>; MAD: Partial<Record<Currency, number>>; MDL: Partial<Record<Currency, number>>; MGA: Partial<Record<Currency, number>>; MKD: Partial<Record<Currency, number>>; MMK: Partial<Record<Currency, number>>; MNT: Partial<Record<Currency, number>>; MOP: Partial<Record<Currency, number>>; MRU: Partial<Record<Currency, number>>; MUR: Partial<Record<Currency, number>>; MVR: Partial<Record<Currency, number>>; MWK: Partial<Record<Currency, number>>; MXN: Partial<Record<Currency, number>>; MXV: Partial<Record<Currency, number>>; MYR: Partial<Record<Currency, number>>; MZN: Partial<Record<Currency, number>>; NAD: Partial<Record<Currency, number>>; NGN: Partial<Record<Currency, number>>; NIO: Partial<Record<Currency, number>>; NOK: Partial<Record<Currency, number>>; NPR: Partial<Record<Currency, number>>; NZD: Partial<Record<Currency, number>>; OMR: Partial<Record<Currency, number>>; PAB: Partial<Record<Currency, number>>; PEN: Partial<Record<Currency, number>>; PGK: Partial<Record<Currency, number>>; PHP: Partial<Record<Currency, number>>; PKR: Partial<Record<Currency, number>>; PLN: Partial<Record<Currency, number>>; PYG: Partial<Record<Currency, number>>; QAR: Partial<Record<Currency, number>>; RON: Partial<Record<Currency, number>>; RSD: Partial<Record<Currency, number>>; RUB: Partial<Record<Currency, number>>; RWF: Partial<Record<Currency, number>>; SAR: Partial<Record<Currency, number>>; SBD: Partial<Record<Currency, number>>; SCR: Partial<Record<Currency, number>>; SDG: Partial<Record<Currency, number>>; SEK: Partial<Record<Currency, number>>; SGD: Partial<Record<Currency, number>>; SHP: Partial<Record<Currency, number>>; SLL: Partial<Record<Currency, number>>; SOS: Partial<Record<Currency, number>>; SRD: Partial<Record<Currency, number>>; SSP: Partial<Record<Currency, number>>; STN: Partial<Record<Currency, number>>; SVC: Partial<Record<Currency, number>>; SYP: Partial<Record<Currency, number>>; SZL: Partial<Record<Currency, number>>; THB: Partial<Record<Currency, number>>; TJS: Partial<Record<Currency, number>>; TMT: Partial<Record<Currency, number>>; TND: Partial<Record<Currency, number>>; TOP: Partial<Record<Currency, number>>; TRY: Partial<Record<Currency, number>>; TTD: Partial<Record<Currency, number>>; TWD: Partial<Record<Currency, number>>; TZS: Partial<Record<Currency, number>>; UAH: Partial<Record<Currency, number>>; UGX: Partial<Record<Currency, number>>; USD: Partial<Record<Currency, number>>; USN: Partial<Record<Currency, number>>; UYI: Partial<Record<Currency, number>>; UYU: Partial<Record<Currency, number>>; UYW: Partial<Record<Currency, number>>; UZS: Partial<Record<Currency, number>>; VES: Partial<Record<Currency, number>>; VND: Partial<Record<Currency, number>>; VUV: Partial<Record<Currency, number>>; WST: Partial<Record<Currency, number>>; XAF: Partial<Record<Currency, number>>; XAG: Partial<Record<Currency, number>>; XAU: Partial<Record<Currency, number>>; XBA: Partial<Record<Currency, number>>; XBB: Partial<Record<Currency, number>>; XBC: Partial<Record<Currency, number>>; XBD: Partial<Record<Currency, number>>; XCD: Partial<Record<Currency, number>>; XDR: Partial<Record<Currency, number>>; XOF: Partial<Record<Currency, number>>; XPD: Partial<Record<Currency, number>>; XPF: Partial<Record<Currency, number>>; XPT: Partial<Record<Currency, number>>; XSU: Partial<Record<Currency, number>>; XTS: Partial<Record<Currency, number>>; XUA: Partial<Record<Currency, number>>; XXX: Partial<Record<Currency, number>>; YER: Partial<Record<Currency, number>>; ZAR: Partial<Record<Currency, number>>; ZMW: Partial<Record<Currency, number>>; ZWL: Partial<Record<Currency, number>>; }",
        "references": {
          "Record": {
            "location": "global"
          },
          "Currency": {
            "location": "import",
            "path": "isoly"
          },
          "Partial": {
            "location": "global"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "costPlus": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "cost-plus",
      "reflect": false
    },
    "view": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "\"single\" | \"multiple\"",
        "resolved": "\"multiple\" | \"single\"",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "view",
      "reflect": false,
      "defaultValue": "\"multiple\""
    }
  }; }
  static get states() { return {
    "t": {},
    "settlementsFiltered": {},
    "settlementsGrouped": {},
    "opened": {},
    "selected": {},
    "selectedSettlements": {}
  }; }
  static get elementRef() { return "element"; }
  static get watchers() { return [{
      "propName": "settlements",
      "methodName": "settlementsChanged"
    }, {
      "propName": "settlementsFiltered",
      "methodName": "groupSettlements"
    }]; }
  static get listeners() { return [{
      "name": "filterChanged",
      "method": "handleFilterChanged",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "columnClicked",
      "method": "toggleColumn",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "selectedChanged",
      "method": "handleSelectedChanged",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
