import { r as registerInstance, h, c as createEvent, H as Host, g as getElement } from './index-08b73e9e.js';
import { d as dist } from './index-f77ff597.js';
import './Item-f4cbd6f4.js';
import { O as Order, I as Item, C as Connection } from './index-3299c1c0.js';
import './Credentials-b84c0c5f.js';
import './Token-ba2c5d5c.js';
import { d as dist$1 } from './index-a0a71287.js';
import { s as sort } from './sort-0b97b2f1.js';
import { E as Error } from './unknown-97022c22.js';
import './App-364e27cc.js';
import { N as Notice } from './Notice-668f9bc7.js';
import './index-eb157781.js';
import { P as PayfuncPortal } from './index-84a8301a.js';
import { d as dist$2 } from './index-3cf720d3.js';
import './_commonjsHelpers-9943807e.js';
import './index-1f953d81.js';
import './TextDecoder-357ac2ce.js';
import './index-8ba22472.js';
import './index-26d778c4.js';

const styleCss$2 = ".sc-ig-orders-h{display:block}[hidden].sc-ig-orders-h{display:none}.sc-ig-orders-h>div.sc-ig-orders>ig-orders-table.sc-ig-orders{border:1px solid rgb(var(--smoothly-medium-color))}.sc-ig-orders-h>div.sc-ig-orders>ig-orders-footer.sc-ig-orders{width:100%;max-width:90em;z-index:1;position:-webkit-sticky;position:sticky;bottom:0;color:rgb(var(--smoothly-light-contrast));background-color:rgb(var(--smoothly-light-color));padding:0.5em 0 0.5em 0}smoothly-trigger.sc-ig-orders{display:inline}.sc-ig-orders-h>div.sc-ig-orders>div.sc-ig-orders>smoothly-trigger.sc-ig-orders:hover{border-color:transparent}.toolbar.sc-ig-orders{z-index:1;padding:0.5em 0 0.9em 0.5em;overflow:hidden;position:fixed;height:10.1em;background-color:rgb(var(--smoothly-light-color));top:33%;right:0;border-radius:0.3em 0 0 0.3em;-webkit-box-shadow:var(--smoothly-shadow);box-shadow:var(--smoothly-shadow);-webkit-animation-name:slide;animation-name:slide;-webkit-animation-duration:0.3s;animation-duration:0.3s;-webkit-animation-timing-function:ease-out;animation-timing-function:ease-out}@-webkit-keyframes slide{0%{width:0}}@keyframes slide{0%{width:0}}#charge.sc-ig-orders{display:inline-block;border-radius:5px 0 0 0;background-color:rgb(var(--smoothly-success-color))}#cancel.sc-ig-orders,#refund.sc-ig-orders{display:inline-block;border-radius:0;background-color:rgb(var(--smoothly-danger-color))}#synchronize.sc-ig-orders{display:inline-block;border-radius:0;background-color:rgb(var(--smoothly-dark-color));border-bottom-left-radius:5px}p.sc-ig-orders{margin:1em}";

const IntergiroOrders = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.selected = {};
    this.display = false;
  }
  componentWillLoad() {
    if (!this.ordersFiltered)
      this.updateOrders();
  }
  handleFilterChanged(event) {
    this.filter = event.detail;
    event.stopPropagation();
  }
  updateOrders() {
    const result = this.orders && (this.filter ? dist.filter(this.filter, this.orders) : [...this.orders]);
    this.ordersFiltered = result && sort(result, "created");
  }
  handleSelectedChanged(event) {
    this.selected = event.detail;
    event.stopPropagation();
  }
  render() {
    return (h("div", null, h("ig-filter", { template: Order.template, filter: this.filter }), h("ig-orders-actions", { selected: this.selected }), h("ig-orders-table", { display: this.display, orders: this.ordersFiltered, selected: this.selected, receipt: this.receipt, embedded: this.embedded }), h("ig-orders-footer", { orders: this.ordersFiltered, selected: this.selected })));
  }
  static get watchers() { return {
    "orders": ["updateOrders"],
    "filter": ["updateOrders"]
  }; }
};
IntergiroOrders.style = styleCss$2;

const sv$1 = {
  "Failed to charge orders.": "Debitering misslyckades.",
  "Failed to cancel orders.": "Kunde inte avbryta.",
  "Failed to refund orders.": "Återbetalning misslyckades.",
  "Failed to synchronize orders.": "Synkronisering misslyckades.",
  "Orders successfully charged.": "Debitering genomförd.",
  "Orders successfully canceled.": "Ordrar avbrutna.",
  "Orders successfully refunded.": "Återbetalningen genomförd.",
  "Orders successfully synchronized.": "Synkronisering genomförd",
  Charge: "Debitera",
  Cancel: "Avbryt",
  Refund: "Återbetala",
  Synchronize: "Synkronisera",
  "Charging orders...": "Debiterar ordrar...",
  "Canceling orders...": "Avbryter ordrar...",
  "Refunding orders...": "Återbetalar ordrar...",
};

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

const styleCss$1 = ".sc-ig-orders-actions-h{display:-ms-flexbox;display:flex;-ms-flex-flow:column;flex-flow:column}[hidden].sc-ig-orders-actions-h{display:none}.sc-ig-orders-actions-h{z-index:2;padding:0.5em 0 0.5em 0.4em;overflow:hidden;position:fixed;top:10%;right:-3.3em;background-color:rgb(var(--smoothly-medium-tint));border-radius:0.3em 0 0 0.3em;-webkit-box-shadow:var(--smoothly-shadow);box-shadow:var(--smoothly-shadow);-webkit-transition:right ease-in-out 0.3s;transition:right ease-in-out 0.3s}.sc-ig-orders-actions-h>smoothly-trigger.sc-ig-orders-actions{border-radius:0;border:solid 1px rgb(var(--smoothly-color))}@-webkit-keyframes slide{0%{width:0}}@keyframes slide{0%{width:0}}.sc-ig-orders-actions-h>.sc-ig-orders-actions:first-child{border-top-left-radius:5px}.sc-ig-orders-actions-h>.sc-ig-orders-actions:last-child{border-bottom-left-radius:5px}";

const IntergiroOrdersActions = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.notice = createEvent(this, "notice", 7);
    this.selectedChanged = createEvent(this, "selectedChanged", 7);
    this.updateOrder = createEvent(this, "updateOrder", 7);
    this.selected = {};
    this.statuses = ["charge", "cancel", "refund", "synchronize"];
  }
  componentWillLoad() {
    this.t = create$1(this.element);
  }
  async handleTrigger(event) {
    if (this.statuses.some(s => s == event.detail.name)) {
      event.stopPropagation();
      const body = Object.values(this.selected).reduce((res, order) => order
        ? [
          ...res,
          {
            id: order.id,
            event: [{ type: event.detail.name }],
          },
        ]
        : res, []);
      const messages = this.getMessages(event.detail.name);
      this.notice.emit(Notice.execute(messages[0], async () => {
        var _a, _b, _c, _d;
        const response = await ((_a = PayfuncPortal.client) === null || _a === void 0 ? void 0 : _a.order.update(body));
        let result;
        if (!Error.is(response) && Array.isArray(response) && ((_c = (_b = response[0].event) === null || _b === void 0 ? void 0 : _b.pop()) === null || _c === void 0 ? void 0 : _c.type) != "fail") {
          this.selectedChanged.emit({});
          result = [true, messages[1]];
          const orders = await ((_d = PayfuncPortal.client) === null || _d === void 0 ? void 0 : _d.order.list());
          this.updateOrder.emit(orders);
        }
        else
          result = [false, messages[2]];
        return result;
      }));
    }
  }
  getMessages(action) {
    let messages;
    switch (action) {
      case "charge":
        messages = [
          this.t("Charging orders..."),
          this.t("Orders successfully charged."),
          this.t("Failed to charge orders."),
        ];
        break;
      case "cancel":
        messages = [
          this.t("Canceling orders..."),
          this.t("Orders successfully canceled."),
          this.t("Failed to cancel orders."),
        ];
        break;
      case "refund":
        messages = [
          this.t("Refunding orders..."),
          this.t("Orders successfully refunded."),
          this.t("Failed to refund orders."),
        ];
        break;
      default:
        messages = [
          this.t("Synchronizing orders..."),
          this.t("Orders successfully synchronized."),
          this.t("Failed to synchronize orders."),
        ];
        break;
    }
    return messages;
  }
  render() {
    const t = this.t;
    const orders = Object.values(this.selected).filter(Order.is);
    const possibleEvents = Order.possibleEvents(orders);
    const chargeable = Object.keys(this.selected).length > 0 ? possibleEvents.includes("charge") : false;
    const cancelable = Object.keys(this.selected).length > 0 ? possibleEvents.includes("cancel") : false;
    const refundable = Object.keys(this.selected).length > 0 ? possibleEvents.includes("refund") : false;
    return (h(Host, { style: Object.entries(this.selected).length > 0 ? { right: "0" } : {} }, h("smoothly-trigger", { color: "success", disabled: !chargeable, name: "charge" }, h("smoothly-icon", { toolTip: t("Charge"), name: "checkmark" })), h("smoothly-trigger", { color: "danger", disabled: !cancelable, name: "cancel" }, h("smoothly-icon", { toolTip: t("Cancel"), name: "close" })), h("smoothly-trigger", { color: "danger", disabled: !refundable, name: "refund" }, h("smoothly-icon", { toolTip: t("Refund"), name: "arrow-undo" }))));
  }
  get element() { return getElement(this); }
};
IntergiroOrdersActions.style = styleCss$1;

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$1.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$2.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 { IntergiroOrders as ig_orders, IntergiroOrdersActions as ig_orders_actions, IntergiroOrdersFooter as ig_orders_footer };
