'use strict';

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

const index$1 = require('./index-140bcbcd.js');
const index$2 = require('./index-bddf9814.js');
const index = require('./index-d3079b93.js');
const unknown = require('./unknown-38d3748c.js');
const index$3 = require('./index-f23d1d6b.js');
require('./App-0b3e3f31.js');
require('./Token-8c5ba5d9.js');
const Notice = require('./Notice-e970b490.js');
require('./Item-00ce8c24.js');
require('./index-76391849.js');
require('./Credentials-8e4749ab.js');
const sort = require('./sort-2cd3789e.js');
const index$4 = require('./index-5380418f.js');
require('./_commonjsHelpers-537d719a.js');
require('./index-09251373.js');
require('./TextDecoder-753156f3.js');
require('./index-188cc15d.js');

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

function create$1(language) {
  return index.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) {
    index$1.registerInstance(this, hostRef);
    this.paymentCancel = index$1.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 [
      index$1.h("td", null),
      index$1.h("td", null, this.order.number),
      index$1.h("td", null, payment.contact),
      index$1.h("smoothly-display-amount", { amount: payment.amount, currency: this.order.currency }),
      index$1.h("td", null),
      index$1.h("smoothly-display-date-time", { datetime: index$2.dist.DateTime.localize(index$2.dist.DateTime.parse(payment.created)) }),
      index$1.h("td", null, index$1.h("smoothly-icon", { name: "trash", toolTip: t("Remove order"), style: { display: this.selected ? "none" : "" } }), index$1.h("smoothly-icon", { name: "warning", toolTip: t("Confirm removal"), style: { display: this.selected ? "" : "none" }, fill: "clear", color: "danger" })),
    ];
  }
  get element() { return index$1.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 index.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) {
    index$1.registerInstance(this, hostRef);
    this.notice = index$1.createEvent(this, "notice", 7);
    this.updateOrder = index$1.createEvent(this, "updateOrder", 7);
  }
  componentWillLoad() {
    this.t = create(this.element);
  }
  filterUpdated() {
    this.ordersFiltered = this.filterAndSort(this.orders);
  }
  filterAndSort(orders) {
    const result = this.filter && orders ? index$3.dist.filter(this.filter, orders) : orders;
    return result && sort.sort(result, "created");
  }
  async handleCancel(event) {
    event.stopPropagation();
    const body = [{ id: event.detail.order.id, event: [{ type: "cancel" }] }];
    this.notice.emit(Notice.Notice.execute(this.t("Canceling deferred payment..."), async () => {
      var _a, _b;
      let result;
      const response = await ((_a = index$4.PayfuncPortal.client) === null || _a === void 0 ? void 0 : _a.order.update(body));
      if (unknown.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 = index$4.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 = index$1.h("p", null, t("Unable to load deferred payments from server."));
    else if (this.ordersFiltered.length <= 0)
      result = index$1.h("p", null, t("There are no deferred payments to display."));
    else {
      result = [
        index$1.h("table", null, index$1.h("thead", null, index$1.h("tr", { color: "light" }, index$1.h("th", { style: { width: "5%" }, color: "light" }), index$1.h("th", { color: "light" }, t("Order number")), index$1.h("th", { color: "light" }, t("Contact")), index$1.h("th", { color: "light" }, t("Amount")), index$1.h("th", { color: "light" }), index$1.h("th", { color: "light" }, t("Created")), index$1.h("th", { color: "light" }))), this.ordersFiltered.map(order => (index$1.h("portal-display-defer", { order: order }))), index$1.h("th", { colSpan: 7 })),
      ];
    }
    return result;
  }
  get element() { return index$1.getElement(this); }
  static get watchers() { return {
    "filter": ["filterUpdated"],
    "orders": ["filterUpdated"]
  }; }
};
PortalDisplayDeferList.style = styleCss;

exports.portal_display_defer = PortalDisplayDefer;
exports.portal_display_defer_list = PortalDisplayDeferList;
