import { r as registerInstance, c as createEvent, h, g as getElement } from './index-08b73e9e.js';
import { d as dist$1 } from './index-3cf720d3.js';
import { d as dist } from './index-a0a71287.js';
import { E as Error } from './unknown-97022c22.js';
import { d as dist$2 } from './index-f77ff597.js';
import './App-364e27cc.js';
import './Token-ba2c5d5c.js';
import { N as Notice } from './Notice-668f9bc7.js';
import './Item-f4cbd6f4.js';
import './index-945f930d.js';
import './Credentials-b84c0c5f.js';
import { s as sort } from './sort-0b97b2f1.js';
import { P as PayfuncPortal } from './index-fd4c17e7.js';
import './_commonjsHelpers-9943807e.js';
import './index-1f953d81.js';
import './TextDecoder-357ac2ce.js';
import './index-ff7475d6.js';

const sv$1 = {
  "Remove order": "Ta bort order",
  "Confirm removal": "Bekräfta borttagning",
};

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

const styleCss$1 = ".sc-portal-display-defer-h{display:table-row;border-top:1px solid transparent}.sc-portal-display-defer-h:last-of-type{border-bottom:1px solid transparent}.sc-portal-display-defer-h:hover{border-top:1px solid rgb(var(--smoothly-dark-color));border-bottom:1px solid rgb(var(--smoothly-dark-color));-webkit-box-shadow:var(--smoothly-shadow);box-shadow:var(--smoothly-shadow)}.sc-portal-display-defer-h>*.sc-portal-display-defer{display:table-cell;padding:.3em;padding-left:.8em}smoothly-icon.sc-portal-display-defer{background-color:transparent;display:inline-block;vertical-align:-50%;margin-top:-.1em;margin-bottom:-.1em;margin-left:.4em}";

const PortalDisplayDefer = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.paymentCancel = createEvent(this, "paymentCancel", 7);
  }
  componentWillLoad() {
    this.t = create$1(this.element);
  }
  componentDidLoad() {
    const icons = this.element.getElementsByTagName("svg");
    if (icons.length > 0) {
      const handle = (e) => {
        e.stopPropagation();
        e.preventDefault();
        if (!this.selected) {
          this.selected = true;
          this.timer = window.setTimeout(() => {
            this.selected = false;
          }, 2000);
        }
        else {
          if (this.timer) {
            window.clearTimeout(this.timer);
            this.timer = undefined;
          }
          this.paymentCancel.emit({ order: this.order });
          this.selected = false;
        }
      };
      icons[0].addEventListener("click", handle);
      icons[0].addEventListener("touch", handle);
      icons[1].addEventListener("click", handle);
      icons[1].addEventListener("touch", handle);
    }
  }
  render() {
    const t = this.t;
    const payment = this.order.payment;
    return [
      h("td", null),
      h("td", null, this.order.number),
      h("td", null, payment.contact),
      h("smoothly-display-amount", { amount: payment.amount, currency: this.order.currency }),
      h("td", null),
      h("smoothly-display-date-time", { datetime: dist$1.DateTime.localize(dist$1.DateTime.parse(payment.created)) }),
      h("td", null, h("smoothly-icon", { name: "trash", toolTip: t("Remove order"), style: { display: this.selected ? "none" : "" } }), h("smoothly-icon", { name: "warning", toolTip: t("Confirm removal"), style: { display: this.selected ? "" : "none" }, fill: "clear", color: "danger" })),
    ];
  }
  get element() { return getElement(this); }
};
PortalDisplayDefer.style = styleCss$1;

const sv = {
  "Unable to load deferred payments from server.": "Lista över uppskjutna betalningar kunde inte hämtas från servern.",
  "There are no deferred payments to display.": "Det finns inga uppskjutna betalningar att visa.",
  "Canceling deferred payment...": "Avbryter order...",
  "Deferred payment was canceled.": "Order avbröts.",
  "Failed to cancel deferred payment.": "Misslyckades avbryta order.",
  Cancel: "Avbryt",
  "Order number": "Ordernummer",
  Amount: "Summa",
  Contact: "Kontakt",
  Created: "Skapad",
};

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

const styleCss = "[hidden].sc-portal-display-defer-list-h{display:none}.sc-portal-display-defer-list-h>table.sc-portal-display-defer-list{display:table;text-align:left;width:100%;border-left:solid 1px rgb(var(--smoothly-medium-color));border-right:solid 1px rgb(var(--smoothly-medium-color));border-bottom:solid 1px rgb(var(--smoothly-medium-color))}.sc-portal-display-defer-list-h>table.sc-portal-display-defer-list>thead.sc-portal-display-defer-list>tr.sc-portal-display-defer-list{display:table-row;background-color:rgb(var(--smoothly-light-color));color:rgb(var(--smoothly-light-contrast));font-weight:bold;height:2em;border-top:solid 1px rgb(var(--smoothly-light-shadow));border-bottom:solid 1px rgb(var(--smoothly-light-shadow))}.sc-portal-display-defer-list-h>table.sc-portal-display-defer-list>thead.sc-portal-display-defer-list>tr.sc-portal-display-defer-list *.sc-portal-display-defer-list{display:table-cell;padding:.3em;padding-right:.5em;padding-left:.8em;vertical-align:middle}smoothly-trigger.sc-portal-display-defer-list{display:inline}p.sc-portal-display-defer-list{margin:1em}";

const PortalDisplayDeferList = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.notice = createEvent(this, "notice", 7);
    this.updateOrder = createEvent(this, "updateOrder", 7);
  }
  componentWillLoad() {
    this.t = create(this.element);
  }
  filterUpdated() {
    this.ordersFiltered = this.filterAndSort(this.orders);
  }
  filterAndSort(orders) {
    const result = this.filter && orders ? dist$2.filter(this.filter, orders) : orders;
    return result && sort(result, "created");
  }
  async handleCancel(event) {
    event.stopPropagation();
    const body = [{ id: event.detail.order.id, event: [{ type: "cancel" }] }];
    this.notice.emit(Notice.execute(this.t("Canceling deferred payment..."), async () => {
      var _a, _b;
      let result;
      const response = await ((_a = PayfuncPortal.client) === null || _a === void 0 ? void 0 : _a.order.update(body));
      if (Error.is(response))
        result = [false, this.t("Failed to cancel deferred payment.")];
      else {
        result = [true, this.t("Deferred payment was canceled.")];
        const orders = await ((_b = PayfuncPortal.client) === null || _b === void 0 ? void 0 : _b.order.list());
        this.updateOrder.emit(orders);
      }
      return result;
    }));
  }
  render() {
    const t = this.t;
    let result;
    if (!this.ordersFiltered)
      result = h("p", null, t("Unable to load deferred payments from server."));
    else if (this.ordersFiltered.length <= 0)
      result = h("p", null, t("There are no deferred payments to display."));
    else {
      result = [
        h("table", null, h("thead", null, h("tr", { color: "light" }, h("th", { style: { width: "5%" }, color: "light" }), h("th", { color: "light" }, t("Order number")), h("th", { color: "light" }, t("Contact")), h("th", { color: "light" }, t("Amount")), h("th", { color: "light" }), h("th", { color: "light" }, t("Created")), h("th", { color: "light" }))), this.ordersFiltered.map(order => (h("portal-display-defer", { order: order }))), h("th", { colSpan: 7 })),
      ];
    }
    return result;
  }
  get element() { return getElement(this); }
  static get watchers() { return {
    "filter": ["filterUpdated"],
    "orders": ["filterUpdated"]
  }; }
};
PortalDisplayDeferList.style = styleCss;

export { PortalDisplayDefer as portal_display_defer, PortalDisplayDeferList as portal_display_defer_list };
