import { r as registerInstance, c as createEvent, h, g as getElement } from './index-08b73e9e.js';
import { d as dist } from './index-a0a71287.js';
import { d as dist$1 } from './index-3cf720d3.js';
import { a as Connection } from './Item-f4cbd6f4.js';
import { S as Settlement, f as Status } from './index-3299c1c0.js';
import './Credentials-b84c0c5f.js';
import './Token-ba2c5d5c.js';
import { P as PayfuncPortal } from './index-84a8301a.js';
import './_commonjsHelpers-9943807e.js';
import './index-1f953d81.js';
import './TextDecoder-357ac2ce.js';
import './App-364e27cc.js';
import './index-26d778c4.js';
import './unknown-97022c22.js';

const sv$3 = {
  unknown: "okänd",
};

function create$3(language) {
  return dist.create({
    sv: sv$3,
  }, language);
}

const styleCss$3 = ".sc-ig-portal-settlement-h{display:table-row;height:2.5em;cursor:pointer}[opened].sc-ig-portal-settlement-h,.sc-ig-portal-settlement-h:hover{background-color:rgb(var(--smoothly-dark-shade));border-top:1px solid rgb(var(--smoothly-medium-color));border-bottom:1px solid rgb(var(--smoothly-medium-color))}.sc-ig-portal-settlement-h:hover{-webkit-box-shadow:var(--smoothly-shadow);box-shadow:var(--smoothly-shadow)}.sc-ig-portal-settlement-h>td.sc-ig-portal-settlement{vertical-align:middle}smoothly-checkbox.sc-ig-portal-settlement{display:inline-block;vertical-align:middle;margin-left:.4em}.sc-ig-portal-settlement-h>*.sc-ig-portal-settlement:not(:first-child){padding:.4em;min-width:3em}.sc-ig-portal-settlement-h>td.right.sc-ig-portal-settlement{text-align:right;padding-right:5em}";

const IntergiroPortalSettlement = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.settlementSelected = createEvent(this, "settlementSelected", 7);
    this.viewOrders = createEvent(this, "viewOrders", 7);
    this.opened = false;
  }
  componentWillLoad() {
    this.t = create$3(this.element);
  }
  select(e) {
    this.settlementSelected.emit({ settlement: this.settlement, checked: !this.selected });
    e.stopPropagation();
    e.preventDefault();
  }
  componentDidLoad() {
    const columns = this.element.querySelectorAll("td:not(.checkbox)");
    const handle = (e) => {
      this.opened = !this.opened;
      this.viewOrders.emit(this.settlement.reference);
      e.stopPropagation();
      e.preventDefault();
    };
    for (let i = 0; i < columns.length; i++)
      columns[i].addEventListener("click", handle);
  }
  render() {
    var _a;
    return [
      h("td", { class: "checkbox" }, h("smoothly-checkbox", { selected: this.selected })),
      h("td", null, this.settlement.reference),
      h("td", null, this.settlement.period.start),
      h("td", null, this.settlement.period.end),
      h("td", null, (_a = this.settlement.payout) !== null && _a !== void 0 ? _a : this.t("unknown")),
      h("td", { class: "right" }, h("smoothly-display-amount", { amount: this.settlement.gross, currency: this.settlement.currency })),
      h("td", { class: "right" }, h("smoothly-display-amount", { amount: typeof this.settlement.fee == "object" ? this.settlement.fee.total : this.settlement.fee, currency: this.settlement.currency })),
      this.interchange && (h("td", { class: "right" }, h("smoothly-display-amount", { amount: typeof this.settlement.fee == "object" ? this.settlement.fee.scheme : 0, currency: this.settlement.currency }))),
      h("td", { class: "right" }, h("smoothly-display-amount", { amount: this.settlement.net, currency: this.settlement.currency })),
    ];
  }
  get element() { return getElement(this); }
};
IntergiroPortalSettlement.style = styleCss$3;

const sv$2 = {
  gross: "brutto",
  net: "netto",
  fee: "avgift",
  "selected count": "antal utvalda",
  "total count": "totalt antal",
  "Download settlement report": "Ladda ner utbetalningsrapport",
};

function create$2(language) {
  return dist.create({
    sv: sv$2,
  }, language);
}

const styleCss$2 = ".sc-ig-portal-settlement-footer-h{display:block;margin:auto;border:1px solid rgb(var(--smoothly-medium-color));max-width:90em;z-index:1;position:-webkit-sticky;position:sticky;bottom:0;padding:.5em 0 .5em 0em}smoothly-display-amount.sc-ig-portal-settlement-footer,.sc-ig-portal-settlement-footer-h>td.sc-ig-portal-settlement-footer>span.sc-ig-portal-settlement-footer{display:inline-block;margin-right:0.6em;text-transform:capitalize}.sc-ig-portal-settlement-footer-h>td.sc-ig-portal-settlement-footer{display:inline-block;margin-left:.5em}.sc-ig-portal-settlement-footer-h>td.sc-ig-portal-settlement-footer:first-child{min-width:11.5em}.sc-ig-portal-settlement-footer-h>td.sc-ig-portal-settlement-footer>*.sc-ig-portal-settlement-footer:last-child{padding-right:2em}smoothly-popup.sc-ig-portal-settlement-footer{float:right;margin-right:1em;cursor:pointer}smoothly-popup.sc-ig-portal-settlement-footer smoothly-display.sc-ig-portal-settlement-footer{background-color:rgb(var(--smoothly-medium-contrast));padding:.4em}smoothly-popup.sc-ig-portal-settlement-footer smoothly-display.sc-ig-portal-settlement-footer:first-of-type{border-bottom:solid 1px rgb(var(--smoothly-medium-shade))}smoothly-popup.sc-ig-portal-settlement-footer>aside.sc-ig-portal-settlement-footer{background-color:rgb(var(--smoothly-medium-contrast))}";

const IntergiroPortalSettlementFooter = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.selected = {};
  }
  async componentWillLoad() {
    this.t = create$2(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 ") +
      dist$1.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) }))))),
    ]);
  }
  get element() { return getElement(this); }
};
IntergiroPortalSettlementFooter.style = styleCss$2;

const sv$1 = {
  "Order number": "Ordernummer",
  Created: "Skapad",
  Gross: "Brutto",
  Net: "Netto",
  Fee: "Avgift",
  Interchange: "Kortavgift",
  Status: "Status",
};

function create$1(language) {
  return dist.create({
    sv: sv$1,
  }, language);
}

const styleCss$1 = ".sc-ig-portal-settlement-orders-h{display:table-row}.sc-ig-portal-settlement-orders-h>td.sc-ig-portal-settlement-orders{border-top:solid 1px rgb(var(--smoothly-medium-shade));border-bottom:solid 1px rgb(var(--smoothly-medium-shade));background-color:rgb(var(--smoothly-dark-shade));padding:0.5em}.sc-ig-portal-settlement-orders-h>td.sc-ig-portal-settlement-orders>table.sc-ig-portal-settlement-orders{width:100%;max-width:65em;margin:auto;border:1px solid rgb(var(--smoothly-medium-color));background-color:rgb(var(--smoothly-dark-color))}.sc-ig-portal-settlement-orders-h>td.sc-ig-portal-settlement-orders>table.sc-ig-portal-settlement-orders>thead.sc-ig-portal-settlement-orders>tr.sc-ig-portal-settlement-orders{display:table-row;font-weight:bold;height:2em;width:100%}.sc-ig-portal-settlement-orders-h>td.sc-ig-portal-settlement-orders>table.sc-ig-portal-settlement-orders>tbody.sc-ig-portal-settlement-orders>td.right.sc-ig-portal-settlement-orders,.sc-ig-portal-settlement-orders-h>td.sc-ig-portal-settlement-orders>table.sc-ig-portal-settlement-orders>thead.sc-ig-portal-settlement-orders>tr.sc-ig-portal-settlement-orders>*.right.sc-ig-portal-settlement-orders{text-align:right;padding-right:3em}.sc-ig-portal-settlement-orders-h>td.sc-ig-portal-settlement-orders>table.sc-ig-portal-settlement-orders>thead.sc-ig-portal-settlement-orders>tr.sc-ig-portal-settlement-orders>*.sc-ig-portal-settlement-orders{display:table-cell;padding-right:.5em;padding-left:.3em;vertical-align:middle;position:-webkit-sticky;position:sticky;top:1.55cm;z-index:1;text-align:left}.sc-ig-portal-settlement-orders-h>td.sc-ig-portal-settlement-orders>table.sc-ig-portal-settlement-orders>tbody.sc-ig-portal-settlement-orders{display:table-row}.sc-ig-portal-settlement-orders-h>td.sc-ig-portal-settlement-orders>table.sc-ig-portal-settlement-orders>tbody.sc-ig-portal-settlement-orders:hover{border:1px solid rgb(var(--smoothly-medium-color))}ig-status.sc-ig-portal-settlement-orders,.sc-ig-portal-settlement-orders-h>td.sc-ig-portal-settlement-orders>table.sc-ig-portal-settlement-orders>tbody.sc-ig-portal-settlement-orders>td.sc-ig-portal-settlement-orders{padding-left:.3em;padding-top:.2em;padding-bottom:.2em}ig-status.sc-ig-portal-settlement-orders{margin-right:.2em;-ms-flex-negative:2;flex-shrink:2;top:0}.sc-ig-portal-settlement-orders-h>td.sc-ig-portal-settlement-orders>table.sc-ig-portal-settlement-orders>tbody.sc-ig-portal-settlement-orders>td.sc-ig-portal-settlement-orders:last-child{display:-ms-flexbox;display:flex;-ms-flex-line-pack:distribute;align-content:space-around}";

const IntergiroPortalSettlementOrders = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.opened = false;
  }
  componentWillLoad() {
    this.t = create$1(this.element);
  }
  render() {
    return [
      h("td", { colSpan: 8 + (this.interchange ? 1 : 0) }, h("table", null, h("thead", null, h("tr", null, h("th", { color: "light" }, this.t("Order number")), h("th", { color: "light" }, this.t("Created")), 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")), h("th", { color: "light" }, this.t("Status")))), this.settlement.orders.map(order => {
        var _a, _b, _c;
        return (h("tbody", null, h("td", null, h("a", { href: "/order#" + ((_a = order.number) === null || _a === void 0 ? void 0 : _a.replace(/\s/g, "").toLowerCase()) }, order.number)), h("td", null, dist$1.DateTime.localize(dist$1.DateTime.parse(order.created))), h("td", { class: "right" }, h("smoothly-display-amount", { amount: order.gross, currency: this.settlement.currency })), h("td", { class: "right" }, h("smoothly-display-amount", { amount: typeof order.fee == "object" ? order.fee.total : order.fee, currency: this.settlement.currency })), this.interchange && (h("td", { class: "right" }, h("smoothly-display-amount", { amount: typeof order.fee == "object" ? order.fee.scheme : 0, currency: this.settlement.currency }))), h("td", { class: "right" }, h("smoothly-display-amount", { amount: order.net, currency: this.settlement.currency })), h("td", null, order.status
          ? Array.isArray(order.status)
            ? (_b = order.status.map((status) => h("ig-status", { status: status }))) !== null && _b !== void 0 ? _b : []
            : (_c = Object.entries(order.status).map(status => Status.is(status[0]) && h("ig-status", { status: status[0] }))) !== null && _c !== void 0 ? _c : []
          : [])));
      }))),
    ];
  }
  get element() { return getElement(this); }
};
IntergiroPortalSettlementOrders.style = styleCss$1;

const sv = {
  "Reference number": "Referensnummer",
  "Start date": "Startdatum",
  "End date": "Slutdatum",
  "Payout date": "Utbetalningsdatum",
  Gross: "Brutto",
  Net: "Netto",
  Fee: "Avgift",
  Interchange: "Kortavgift",
  Status: "Status",
  "Loading settlements from server.": "Laddar utbetalningar från servern.",
  "There are no settlements to display.": "Det finns inga utbetalningar att visa.",
};

function create(language) {
  return dist.create({
    sv,
  }, language);
}

const styleCss = ".sc-ig-portal-settlement-table-h{display:block;width:100%;max-width:90em;margin:auto;border:1px solid rgb(var(--smoothly-medium-color))}.sc-ig-portal-settlement-table-h>p.sc-ig-portal-settlement-table{padding-left:.5em}.sc-ig-portal-settlement-table-h>table.sc-ig-portal-settlement-table{width:100%}.sc-ig-portal-settlement-table-h>table.sc-ig-portal-settlement-table>thead.sc-ig-portal-settlement-table>tr.sc-ig-portal-settlement-table>th.right.sc-ig-portal-settlement-table{text-align:right;padding-right:5em}.sc-ig-portal-settlement-table-h>table.sc-ig-portal-settlement-table>thead.sc-ig-portal-settlement-table>tr.sc-ig-portal-settlement-table>*.sc-ig-portal-settlement-table{display:table-cell;padding-right:.5em;padding-left:.3em;vertical-align:middle;position:-webkit-sticky;position:sticky;top:1.55cm;z-index:1;text-align:left}.sc-ig-portal-settlement-table-h>table.sc-ig-portal-settlement-table>thead.sc-ig-portal-settlement-table>tr.sc-ig-portal-settlement-table>*.sc-ig-portal-settlement-table>smoothly-checkbox.sc-ig-portal-settlement-table{vertical-align:-50%;margin-left:.1em;z-index:1}.sc-ig-portal-settlement-table-h>table.sc-ig-portal-settlement-table>td.sc-ig-portal-settlement-table{text-align:center}smoothly-icon.sc-ig-portal-settlement-table{cursor:pointer}";

const IntergiroPortalSettlementTable = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.selectedChanged = createEvent(this, "selectedChanged", 7);
    this.count = 100;
    this.selected = {};
    this.opened = [];
  }
  componentWillLoad() {
    this.t = 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;
  }
  get element() { return getElement(this); }
};
IntergiroPortalSettlementTable.style = styleCss;

export { IntergiroPortalSettlement as ig_portal_settlement, IntergiroPortalSettlementFooter as ig_portal_settlement_footer, IntergiroPortalSettlementOrders as ig_portal_settlement_orders, IntergiroPortalSettlementTable as ig_portal_settlement_table };
