'use strict';

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

const index$1 = require('./index-df541290.js');
const index$4 = require('./index-0a8af373.js');
require('./index-5e992066.js');
const index$3 = require('./index-c12b8888.js');
require('./index-e8825dfe.js');
const index = require('./index-b7493032.js');
const index$2 = require('./index-3e5f1089.js');
require('./App-1e388484.js');
require('./index-0b6f1dd9.js');
require('./_commonjsHelpers-10109b76.js');
require('./index-fe328b96.js');
require('./index-b8688617.js');
require('./index-ad93d157.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-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) {
    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.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); }
};
IntergiroOrdersActions.style = styleCss;

exports.ig_orders_actions = IntergiroOrdersActions;
