'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index$1 = require('./index-0b1e2673.js');
require('./_commonjsHelpers-937cd5b3.js');
const index$2 = require('./index-2eb96242.js');
const index = require('./index-36cf3884.js');
const index$3 = require('./index-8a61808f.js');

const sv = {
  "Selected Count": "Antal utvalda",
  "Selected Amount": "Summa för utvalda",
  "Total Amount": "Totalsumma",
  "Total Count": "Totalt antal",
  "Download order report": "Ladda ner orderrapport",
};

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

const styleCss = ".sc-payfunc-orders-footer-h{display:block;margin:auto;border:1px solid rgb(var(--smoothly-medium-color))}[hidden].sc-payfunc-orders-footer-h{display:none}.sc-payfunc-orders-footer-h>footer.sc-payfunc-orders-footer>button.sc-payfunc-orders-footer{float:right;margin-right:0.5em;margin-bottom:-0.2em}.sc-payfunc-orders-footer-h>footer.sc-payfunc-orders-footer>button.sc-payfunc-orders-footer:focus{outline:none}.sc-payfunc-orders-footer-h>footer.sc-payfunc-orders-footer>td.sc-payfunc-orders-footer{display:inline-block;padding-top:0.2em}.sc-payfunc-orders-footer-h>footer.sc-payfunc-orders-footer>td.sc-payfunc-orders-footer>smoothly-display-amount.sc-payfunc-orders-footer,.sc-payfunc-orders-footer-h>footer.sc-payfunc-orders-footer>td.sc-payfunc-orders-footer>span.sc-payfunc-orders-footer{display:inline-block;margin-left:0.4em;margin-right:0.6em}.sc-payfunc-orders-footer-h>footer.sc-payfunc-orders-footer>td.sc-payfunc-orders-footer>span.tabbed.sc-payfunc-orders-footer{margin-left:2em}";

const PayfuncOrdersFooter = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.allSelected = false;
    this.opened = {};
  }
  componentWillLoad() {
    this.t = create(this.element);
  }
  sumSelected() {
    const sum = {};
    for (const id of Object.keys(this.selected)) {
      const order = this.selected[id];
      if (order)
        if (Object.keys(sum).includes(order.currency))
          sum[order.currency] += index$2.Item.amount(order.items);
        else
          sum[order.currency] = index$2.Item.amount(order.items);
    }
    return sum;
  }
  sumAll() {
    const sum = {};
    if (this.orders)
      for (const order of this.orders)
        if (Object.keys(sum).includes(order.currency))
          sum[order.currency] += index$2.Item.amount(order.items);
        else
          sum[order.currency] = index$2.Item.amount(order.items);
    return sum;
  }
  downloadCsv() {
    const merchant = !index$2.Connection.user
      ? undefined
      : Array.isArray(index$2.Connection.user.merchant)
        ? index$2.Connection.user.merchant[0]
        : index$2.Connection.user.merchant;
    const filename = (merchant ? merchant.name + " " : "orders ") + index$3.dist.DateTime.now().substring(0, 10) + ".csv";
    const orders = Object.keys(this.selected).length > 0
      ? Object.values(this.selected).filter(order => order != undefined)
      : this.orders;
    const text = orders ? index$2.Order.toCsv(orders) : "";
    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();
  }
  render() {
    const t = this.t;
    let result;
    if (!this.orders)
      result = [];
    else {
      result = (index$1.h("footer", null, this.selected && Object.keys(this.selected).length > 0 ? (index$1.h("td", null, index$1.h("span", null, " ", t("Selected Count"), ":"), index$1.h("span", null, Object.keys(this.selected).length), index$1.h("span", { class: "tabbed" }, t("Selected Amount"), ":"), Object.keys(this.sumSelected()).map(key => (index$1.h("smoothly-display-amount", { amount: this.sumSelected()[key], currency: key }))))) : (index$1.h("td", null, index$1.h("span", null, t("Total Count"), ":"), index$1.h("span", null, this.orders.length), index$1.h("span", { class: "tabbed" }, t("Total Amount"), ":"), Object.keys(this.sumAll()).map(key => (index$1.h("smoothly-display-amount", { amount: this.sumAll()[key], currency: key }))))), index$1.h("button", { onClick: () => this.downloadCsv() }, index$1.h("smoothly-icon", { color: "light", toolTip: t("Download order report"), name: "cloud-download" }))));
    }
    return result;
  }
  get element() { return index$1.getElement(this); }
};
PayfuncOrdersFooter.style = styleCss;

exports.payfunc_orders_footer = PayfuncOrdersFooter;
