import { Component, Element, h, Prop, State } from "@stencil/core";
import { DateTime } from "isoly";
import { Settlement } from "@payfunc/model";
import { Connection } from "@payfunc/model-base";
import { PayfuncPortal } from "../index";
import * as translation from "./translation";
export class IntergiroPortalSettlementFooter {
  constructor() {
    this.selected = {};
  }
  async componentWillLoad() {
    this.t = translation.create(this.element);
  }
  sum(value, selected) {
    var _a;
    let result = {};
    if (value == "gross" || value == "fee" || value == "net")
      for (const settlement of this.settlements) {
        let amount = settlement[value];
        amount = typeof amount == "object" ? amount.total : amount;
        if (this.selected[settlement.reference] || !selected)
          result[settlement.currency] = ((_a = result[settlement.currency]) !== null && _a !== void 0 ? _a : 0) + amount;
      }
    else
      result = (value == "selected count" ? Object.keys(this.selected) : this.settlements).length;
    return result;
  }
  downloadCsv() {
    var _a;
    const user = (_a = PayfuncPortal.client) === null || _a === void 0 ? void 0 : _a.me.user;
    const merchant = !user ? undefined : Array.isArray(user.merchant) ? user.merchant[0] : user.merchant;
    const settlements = Object.keys(this.selected).length > 0
      ? Object.values(this.selected).filter(settlement => settlement != undefined)
      : this.settlements;
    const filename = (merchant ? merchant.name + " " : " ") +
      (settlements.length == 1 ? "settlement " + settlements[0].reference + " " : "settlements ") +
      DateTime.now().substring(0, 10) +
      ".csv";
    const text = Array.isArray(settlements)
      ? Settlement.toCsv(settlements.length == 1 ? settlements[0] : settlements, false, this.interchange)
      : "";
    const link = document.createElement("a");
    link.setAttribute("href", "data:text/csv;charset=utf-8," + encodeURIComponent(text));
    link.setAttribute("download", filename);
    link.style.display = "none";
    document.body.appendChild(link);
    link.click();
    link.remove();
  }
  downloadPdf() {
    this.inputElement.value = JSON.stringify(Object.keys(this.selected).length > 0
      ? Object.values(this.selected).filter(settlement => settlement != undefined)
      : this.settlements);
    this.submitElement.submit();
  }
  render() {
    const selected = this.selected && Object.keys(this.selected).length > 0;
    const countLabel = selected ? "selected count" : "total count";
    return (this.settlements && [
      h("td", null,
        h("span", null,
          countLabel,
          ":"),
        h("span", null, this.sum(countLabel, selected))),
      this.settlements.length > 0 &&
        ["gross", "fee", "net"].map(l => (h("td", null,
          h("span", null,
            this.t(l),
            ":"),
          Object.entries(this.sum(l, selected)).map(v => (h("smoothly-display-amount", { amount: v[1], currency: v[0] })))))),
      h("smoothly-popup", { direction: "up" },
        h("smoothly-icon", { color: "light", toolTip: this.t("Download settlement report"), name: "cloud-download" }),
        h("div", { color: "dark", slot: "popup" },
          h("smoothly-display", { onClick: () => this.downloadCsv() }, "CSV"),
          h("smoothly-display", { onClick: () => this.downloadPdf() },
            "PDF",
            h("form", { action: `${Connection.url}document/settlement?merchant=${Connection.key}${this.interchange ? "&interchange=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-portal-settlement-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": "Settlement[]",
        "resolved": "Settlement[]",
        "references": {
          "Settlement": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "selected": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "{ [reference: string]: Settlement | undefined }",
        "resolved": "{ [reference: string]: Settlement | undefined; }",
        "references": {
          "Settlement": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "defaultValue": "{}"
    },
    "interchange": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "true",
        "resolved": "boolean | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "interchange",
      "reflect": false
    }
  }; }
  static get states() { return {
    "t": {}
  }; }
  static get elementRef() { return "element"; }
}
