import { r as registerInstance, j as createEvent, h, k as Host, g as getElement } from './index-07845439.js';
import { E as Error } from './index-38089f62.js';
import './index-cd3eb450.js';
import { C as Connection, O as Order } from './index-e20749b7.js';
import './index-ce48c20e.js';
import { d as dist } from './index-63517df5.js';
import { N as Notice } from './index-f068b00b.js';
import './App-15d9fce3.js';
import './index-b6cf748e.js';
import './_commonjsHelpers-8fe71198.js';
import './index-265fba1f.js';
import './index-046ba252.js';
import './index-837ba18a.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-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:.3em 0 0 .3em;-webkit-box-shadow:var(--smoothly-shadow);box-shadow:var(--smoothly-shadow);-webkit-transition:right ease-in-out .3s;transition:right ease-in-out .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 = {};
  }
  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(Notice.execute(messages[0], async () => {
        var _a, _b;
        const response = await Connection.patch("order", body);
        let result;
        if (!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 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); }
};
IntergiroOrdersActions.style = styleCss;

export { IntergiroOrdersActions as ig_orders_actions };
