import { Component, Element, Event, h, Listen, Prop, State } from "@stencil/core";
import { Currency } from "isoly";
import { Notice } from "smoothly";
import { Connection } from "@payfunc/model-base";
import { AcquirerSettlement } from "../../model";
import * as translation from "../translation";
export class IntergiroMonitorSettlementFooter {
  componentWillLoad() {
    this.t = translation.create(this.element);
  }
  downloadCsv() {
    const file = !this.settlements || !this.type
      ? ""
      : this.type == "summary"
        ? AcquirerSettlement.toCsv(this.settlements)
        : this.authorizations
          ? AcquirerSettlement.toDetailedCsv(this.settlements, this.authorizations)
          : "";
    if (file) {
      const link = document.createElement("a");
      link.setAttribute("href", "data:text/csv;charset=utf-8," + encodeURIComponent(file));
      link.setAttribute("download", "settlements.csv");
      link.style.display = "none";
      document.body.appendChild(link);
      link.click();
      link.remove();
    }
    else
      this.notice.emit(Notice.failed("Failed to download CSV."));
  }
  downloadPdf() {
    var _a;
    this.inputElement.value = JSON.stringify((_a = this.settlements) !== null && _a !== void 0 ? _a : []);
    this.submitElement.submit();
  }
  getAmount(currency, scheme) {
    var _a, _b;
    return Currency.round((_b = (_a = this.fees) === null || _a === void 0 ? void 0 : _a.reduce((r, f) => (currency == f.currency && (scheme == "total" || scheme == f.scheme) ? (r += f.gross) : r), 0)) !== null && _b !== void 0 ? _b : 0, currency);
  }
  handlePopupToggle(event) {
    if (event.detail == false)
      this.type = undefined;
  }
  render() {
    var _a;
    const schemes = (_a = this.fees) === null || _a === void 0 ? void 0 : _a.reduce((r, fee) => (r.includes(fee.scheme) ? r : [...r, fee.scheme]), ["total"]);
    return [
      this.settlements &&
        (schemes === null || schemes === void 0 ? void 0 : schemes.map(scheme => [
          h("smoothly-display", null, this.t(this.selected ? "total selected" : "total") + (scheme == "total" ? "" : " " + scheme) + ":"),
          h("smoothly-display-amount", { amount: this.getAmount("SEK", scheme), currency: "SEK" }),
          h("span", null),
          h("smoothly-display-amount", { amount: this.getAmount("EUR", scheme), currency: "EUR" }),
        ])),
      h("smoothly-popup", { direction: "up" },
        h("smoothly-icon", { color: "light", toolTip: this.t("Download settlement report"), name: "cloud-download" }),
        h("div", { class: "popup", slot: "popup" },
          h("section", { class: this.type == "detailed" ? "selected" : "" },
            h("smoothly-display", { onClick: () => (this.type = this.type == "detailed" ? undefined : "detailed") }, "Detailed"),
            h("smoothly-display", { style: { display: this.type ? "block" : "none" }, onClick: () => this.downloadCsv() }, "CSV")),
          h("section", { class: this.type == "summary" ? "selected" : "" },
            h("smoothly-display", { onClick: () => (this.type = this.type == "summary" ? undefined : "summary") }, "Summary"),
            h("smoothly-display", { style: { display: this.type ? "block" : "none" }, onClick: () => this.downloadPdf() },
              "PDF",
              h("form", { action: `${Connection.url}settlement/report?key=${Connection.key}${this.type == "detailed" ? "&detailed=true" : ""}`, target: "_blank", method: "post" },
                h("input", { type: "hidden", name: "settlements", ref: (element) => (this.inputElement = element) }),
                h("smoothly-submit", { hidden: true, ref: (element) => (this.submitElement = element) })))))),
    ];
  }
  static get is() { return "ig-monitor-settlements-footer"; }
  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": "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[]",
        "references": {
          "Authorization": {
            "location": "import",
            "path": "@payfunc/model-acquirer"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "selected": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "selected",
      "reflect": false
    },
    "fees": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "{\n\t\tscheme: string\n\t\tgross: number\n\t\tfee: number\n\t\tnet: number\n\t\tinterchange: number\n\t\tcurrency: Currency\n\t}[]",
        "resolved": "undefined | { scheme: string; gross: number; fee: number; net: number; interchange: number; currency: Currency; }[]",
        "references": {
          "Currency": {
            "location": "import",
            "path": "isoly"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "conversion": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Record<Currency, Partial<Record<Currency, number>>>",
        "resolved": "{ 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": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "t": {},
    "type": {}
  }; }
  static get events() { return [{
      "method": "notice",
      "name": "notice",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "Notice",
        "resolved": "Notice",
        "references": {
          "Notice": {
            "location": "import",
            "path": "smoothly"
          }
        }
      }
    }]; }
  static get elementRef() { return "element"; }
  static get listeners() { return [{
      "name": "popup",
      "method": "handlePopupToggle",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
