import { r as registerInstance, j as createEvent, h, k as Host, g as getElement } from './index-aeac5cad.js';
import './App-418c0118.js';
import './_commonjsHelpers-91fffb88.js';
import { N as Notice } from './index-4920b0b3.js';
import './index-66cd4113.js';
import { C as Connection, O as Order } from './index-ec2a4942.js';
import { d as dist } from './index-fdd60c34.js';
import { d as dist$1 } from './index-909faba2.js';
import './index-6b5931f4.js';
import './index-ff2f9e0e.js';
import './index-31b724c4.js';
import './index-4377c15e.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 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) {
    registerInstance(this, hostRef);
    this.notice = createEvent(this, "notice", 7);
    this.selectedChanged = createEvent(this, "selectedChanged", 7);
    this.updateOrder = 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: [
            event.detail.name == "charge"
              ? { type: "charge" }
              : event.detail.name == "cancel"
                ? { type: "cancel" }
                : event.detail.name == "refund"
                  ? { type: "refund" }
                  : { type: "synchronize" },
          ],
        });
      const messages = this.getMessages(event.detail.name);
      this.notice.emit(Notice.execute(messages[0], async () => {
        const response = await Connection.patch("order", body);
        let result;
        if (!dist$1.Error.is(response)) {
          this.selectedChanged.emit({});
          result = [true, messages[1]];
          const orders = await 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(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); }
};
PayfuncOrdersActions.style = styleCss;

export { PayfuncOrdersActions as payfunc_orders_actions };
