import { Component, Element, h, Prop, State } from "@stencil/core";
import { Currency, DateTime } from "isoly";
import * as translation from "../../../../translation";
export class IntergiroAuthorizationAmountTable {
  componentWillLoad() {
    this.t = translation.create(this.element);
  }
  render() {
    var _a;
    const t = this.t;
    const history = [
      { date: this.state.authorization.created, type: t("authorization"), amount: this.state.authorization.amount },
    ];
    this.state.authorization.voided && history.push({ date: this.state.authorization.voided, type: t("cancellation") });
    (_a = this.state.authorization.change) === null || _a === void 0 ? void 0 : _a.forEach(a => history.push({ date: a.created, type: t("authorization"), amount: a.amount }));
    this.state.authorization.captured.history.forEach(c => history.push({ date: c.created, type: t("capture"), amount: c.amount, settlement: c.settlement }));
    this.state.authorization.refunded.history.forEach(r => history.push({ date: r.created, type: t("refund"), amount: r.amount, settlement: r.settlement }));
    const gross = Currency.round(history.reduce((r, v) => (v.settlement ? (r += v.settlement.gross) : r), 0), this.settlementCurrency);
    const fee = Currency.round(history.reduce((r, v) => v.settlement ? (r += typeof v.settlement.fee == "object" ? v.settlement.fee.total : v.settlement.fee) : r, 0), this.settlementCurrency);
    const schemeFee = this.costPlus
      ? Currency.round(history.reduce((r, v) => (v.settlement ? (r += typeof v.settlement.fee == "object" ? v.settlement.fee.scheme : 0) : r), 0), this.settlementCurrency)
      : 0;
    const net = Currency.round(history.reduce((r, v) => (v.settlement ? (r += v.settlement.net) : r), 0), this.settlementCurrency);
    return (h("table", null,
      h("th", { color: "light" }, t("type")),
      h("th", { color: "light" }, t("created")),
      h("th", { color: "light" }, t("settled")),
      h("th", { color: "light" }, t("amount")),
      h("th", { color: "light" }, t("gross")),
      h("th", { color: "light" }, t("fee")),
      this.costPlus && h("th", { color: "light" }, t("interchange")),
      h("th", { color: "light" }, t("net")),
      history
        .sort((a, b) => DateTime.parse(a.date).getTime() - DateTime.parse(b.date).getTime())
        .map(a => (h("tr", null,
        h("smoothly-display", null, a === null || a === void 0 ? void 0 : a.type),
        h("smoothly-display-date-time", { datetime: DateTime.localize(a.date) }),
        h("smoothly-display-date-time", { datetime: DateTime.localize(a.settlement ? a.settlement.created : "-") }),
        h("smoothly-display-amount", { amount: a.amount, currency: this.state.authorization.currency }),
        a.settlement
          ? [
            h("smoothly-display-amount", { amount: a.settlement.gross, currency: this.settlementCurrency }),
            h("smoothly-display-amount", { amount: typeof a.settlement.fee == "object" ? a.settlement.fee.total : a.settlement.fee, currency: this.settlementCurrency }),
            this.costPlus && (h("smoothly-display-amount", { amount: schemeFee, currency: this.state.authorization.currency })),
            h("smoothly-display-amount", { amount: a.settlement.net, currency: this.settlementCurrency }),
          ]
          : [h("p", null, "-"), h("p", null, "-"), this.costPlus && h("p", null, "-"), h("p", null, "-")]))),
      h("tr", { style: { height: "1em" } }),
      h("tr", null,
        h("td", null),
        h("td", null),
        h("td", null),
        h("label", null, t("settled ")),
        h("smoothly-display", { type: gross > 0 ? "price" : "text", value: gross > 0 ? gross : "-", currency: this.state.authorization.currency }),
        h("smoothly-display", { type: fee > 0 ? "price" : "text", value: fee > 0 ? fee : "-", currency: this.state.authorization.currency }),
        this.costPlus && (h("smoothly-display", { type: schemeFee > 0 ? "price" : "text", value: schemeFee > 0 ? schemeFee : "-", currency: this.state.authorization.currency })),
        h("smoothly-display", { type: net > 0 ? "price" : "text", value: net > 0 ? net : "-", currency: this.state.authorization.currency }))));
  }
  static get is() { return "ig-authorization-amount-table"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "state": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "ModelState.Authorization",
        "resolved": "Authorization",
        "references": {
          "ModelState": {
            "location": "import",
            "path": "../../../../../model"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "settlementCurrency": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "Currency",
        "resolved": "\"AED\" | \"AFN\" | \"ALL\" | \"AMD\" | \"ANG\" | \"AOA\" | \"ARS\" | \"AUD\" | \"AWG\" | \"AZN\" | \"BAM\" | \"BBD\" | \"BDT\" | \"BGN\" | \"BHD\" | \"BIF\" | \"BMD\" | \"BND\" | \"BOB\" | \"BOV\" | \"BRL\" | \"BSD\" | \"BTN\" | \"BWP\" | \"BYN\" | \"BZD\" | \"CAD\" | \"CDF\" | \"CHE\" | \"CHF\" | \"CHW\" | \"CLF\" | \"CLP\" | \"CNY\" | \"COP\" | \"COU\" | \"CRC\" | \"CUC\" | \"CUP\" | \"CVE\" | \"CZK\" | \"DJF\" | \"DKK\" | \"DOP\" | \"DZD\" | \"EGP\" | \"ERN\" | \"ETB\" | \"EUR\" | \"FJD\" | \"FKP\" | \"GBP\" | \"GEL\" | \"GHS\" | \"GIP\" | \"GMD\" | \"GNF\" | \"GTQ\" | \"GYD\" | \"HKD\" | \"HNL\" | \"HRK\" | \"HTG\" | \"HUF\" | \"IDR\" | \"ILS\" | \"INR\" | \"IQD\" | \"IRR\" | \"ISK\" | \"JMD\" | \"JOD\" | \"JPY\" | \"KES\" | \"KGS\" | \"KHR\" | \"KMF\" | \"KPW\" | \"KRW\" | \"KWD\" | \"KYD\" | \"KZT\" | \"LAK\" | \"LBP\" | \"LKR\" | \"LRD\" | \"LSL\" | \"LYD\" | \"MAD\" | \"MDL\" | \"MGA\" | \"MKD\" | \"MMK\" | \"MNT\" | \"MOP\" | \"MRU\" | \"MUR\" | \"MVR\" | \"MWK\" | \"MXN\" | \"MXV\" | \"MYR\" | \"MZN\" | \"NAD\" | \"NGN\" | \"NIO\" | \"NOK\" | \"NPR\" | \"NZD\" | \"OMR\" | \"PAB\" | \"PEN\" | \"PGK\" | \"PHP\" | \"PKR\" | \"PLN\" | \"PYG\" | \"QAR\" | \"RON\" | \"RSD\" | \"RUB\" | \"RWF\" | \"SAR\" | \"SBD\" | \"SCR\" | \"SDG\" | \"SEK\" | \"SGD\" | \"SHP\" | \"SLL\" | \"SOS\" | \"SRD\" | \"SSP\" | \"STN\" | \"SVC\" | \"SYP\" | \"SZL\" | \"THB\" | \"TJS\" | \"TMT\" | \"TND\" | \"TOP\" | \"TRY\" | \"TTD\" | \"TWD\" | \"TZS\" | \"UAH\" | \"UGX\" | \"USD\" | \"USN\" | \"UYI\" | \"UYU\" | \"UYW\" | \"UZS\" | \"VES\" | \"VND\" | \"VUV\" | \"WST\" | \"XAF\" | \"XAG\" | \"XAU\" | \"XBA\" | \"XBB\" | \"XBC\" | \"XBD\" | \"XCD\" | \"XDR\" | \"XOF\" | \"XPD\" | \"XPF\" | \"XPT\" | \"XSU\" | \"XTS\" | \"XUA\" | \"XXX\" | \"YER\" | \"ZAR\" | \"ZMW\" | \"ZWL\"",
        "references": {
          "Currency": {
            "location": "import",
            "path": "isoly"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "settlement-currency",
      "reflect": false
    },
    "costPlus": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "cost-plus",
      "reflect": false
    }
  }; }
  static get states() { return {
    "t": {}
  }; }
  static get elementRef() { return "element"; }
}
