import { r as registerInstance, h, g as getElement } from './index-07845439.js';
import { d as dist$1 } from './index-046ba252.js';
import { I as Item, C as Connection, O as Order } from './index-e20749b7.js';
import { d as dist } from './index-63517df5.js';
import './_commonjsHelpers-8fe71198.js';
import './index-b6cf748e.js';
import './index-265fba1f.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 dist.create({
    sv,
  }, language);
}

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

const IntergiroOrdersFooter = class {
  constructor(hostRef) {
    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] += Item.amount(order.items);
        else
          sum[order.currency] = 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] += Item.amount(order.items);
        else
          sum[order.currency] = Item.amount(order.items);
    return sum;
  }
  downloadCsv() {
    const merchant = !Connection.user
      ? undefined
      : Array.isArray(Connection.user.merchant)
        ? Connection.user.merchant[0]
        : Connection.user.merchant;
    const filename = (merchant ? merchant.name + " " : "orders ") + dist$1.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 ? 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 = (h("footer", null, this.selected && Object.keys(this.selected).length > 0 ? (h("td", null, h("span", null, " ", t("Selected Count"), ":"), h("span", null, Object.keys(this.selected).length), h("span", { class: "tabbed" }, t("Selected Amount"), ":"), Object.keys(this.sumSelected()).map(key => (h("smoothly-display-amount", { amount: this.sumSelected()[key], currency: key }))))) : (h("td", null, h("span", null, t("Total Count"), ":"), h("span", null, this.orders.length), h("span", { class: "tabbed" }, t("Total Amount"), ":"), Object.keys(this.sumAll()).map(key => (h("smoothly-display-amount", { amount: this.sumAll()[key], currency: key }))))), h("button", { onClick: () => this.downloadCsv() }, h("smoothly-icon", { color: "light", toolTip: t("Download order report"), name: "cloud-download" }))));
    }
    return result;
  }
  get element() { return getElement(this); }
};
IntergiroOrdersFooter.style = styleCss;

export { IntergiroOrdersFooter as ig_orders_footer };
