'use strict';

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

const index$1 = require('./index-0b1e2673.js');
require('./App-1647a125.js');
require('./_commonjsHelpers-937cd5b3.js');
const index$2 = require('./index-1a9e1965.js');
require('./index-451ca284.js');
const index$3 = require('./index-ab186017.js');
const index = require('./index-36cf3884.js');
const index$4 = require('./index-061d0ccd.js');
require('./index-10a0b195.js');
require('./index-a802b56a.js');
require('./index-8a61808f.js');
require('./index-d117c6f9.js');

const sv = {
  "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(language) {
  return index.dist.create({
    sv,
  }, language);
}

const styleCss = ".sc-payfunc-orders-actions-h{display:flex;flex-flow:column}[hidden].sc-payfunc-orders-actions-h{display:none}.sc-payfunc-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:.3em 0 0 .3em;box-shadow:var(--smoothly-shadow);transition:right ease-in-out .3s}.sc-payfunc-orders-actions-h>smoothly-trigger.sc-payfunc-orders-actions{border-radius:0;border:solid 1px rgb(var(--smoothly-color))}@keyframes slide{0%{width:0}}.sc-payfunc-orders-actions-h>.sc-payfunc-orders-actions:first-child{border-top-left-radius:5px}.sc-payfunc-orders-actions-h>.sc-payfunc-orders-actions:last-child{border-bottom-left-radius:5px}";

const PayfuncOrdersActions = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.notice = index$1.createEvent(this, "notice", 7);
    this.selectedChanged = index$1.createEvent(this, "selectedChanged", 7);
    this.updateOrder = index$1.createEvent(this, "updateOrder", 7);
    this.selected = {};
  }
  componentWillLoad() {
    this.t = create(this.element);
  }
  async handleTrigger(event) {
    if (event.detail.name == "charge" ||
      event.detail.name == "cancel" ||
      event.detail.name == "refund" ||
      event.detail.name == "synchronize") {
      event.stopPropagation();
      const body = Object.values(this.selected).map(order => !order
        ? undefined
        : {
          id: order.id,
          event: [{ type: event.detail.name }],
        });
      const messages = this.getMessages(event.detail.name);
      this.notice.emit(index$2.Notice.execute(messages[0], async () => {
        var _a, _b;
        const response = await index$3.Connection.patch("order", body);
        let result;
        if (!index$4.dist.Error.is(response) && Array.isArray(response) && ((_b = (_a = response[0].event) === null || _a === void 0 ? void 0 : _a.pop()) === null || _b === void 0 ? void 0 : _b.type) != "fail") {
          this.selectedChanged.emit({});
          result = [true, messages[1]];
          const orders = await index$3.Connection.get("order");
          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(index$3.Order.is);
    const possibleEvents = index$3.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 (index$1.h(index$1.Host, { style: Object.entries(this.selected).length > 0 ? { right: "0" } : {} }, index$1.h("smoothly-trigger", { color: "success", disabled: !chargeable, name: "charge" }, index$1.h("smoothly-icon", { toolTip: t("Charge"), name: "checkmark" })), index$1.h("smoothly-trigger", { color: "danger", disabled: !cancelable, name: "cancel" }, index$1.h("smoothly-icon", { toolTip: t("Cancel"), name: "close" })), index$1.h("smoothly-trigger", { color: "danger", disabled: !refundable, name: "refund" }, index$1.h("smoothly-icon", { toolTip: t("Refund"), name: "arrow-undo" }))));
  }
  get element() { return index$1.getElement(this); }
};
PayfuncOrdersActions.style = styleCss;

exports.payfunc_orders_actions = PayfuncOrdersActions;
