'use strict';

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

const index$1 = require('./index-df541290.js');
const index$4 = require('./index-0a8af373.js');
require('./index-2f460f13.js');
const index$3 = require('./index-07607ed6.js');
const index = require('./index-aa81d689.js');
const index$2 = require('./index-50f629f5.js');
require('./App-1e388484.js');
require('./index-e4cf4aa6.js');
require('./_commonjsHelpers-5bbc2693.js');

const sv = {
  "Create order": "Skapa order",
  Cancel: "Avbryt",
  "Retrying payment.": "Försöker betalning igen.",
  "Your payment was declined.": "Betalningen nekades.",
  "Payment succeeded.": "Betalning genomförd.",
  "Retry payment": "Försök betala",
  "Create subscription": "Skapa prenumeration",
  "Equalize balance to zero": "Justera saldot till noll",
  "Equalizing balance...": "Justerar saldo...",
  "$0 of $1 balances were equalized.": "$0 av $1 saldon har justerats.",
  "Balance was equalized.": "Saldot har justerats.",
  "Failed to equalize $0 of $1 balances.": "Kunde inte justera $0 av $1 saldon.",
  "Failed to equalize balance.": "Kunde inte justera saldo.",
};

function create(language) {
  return index.dist.create({
    sv,
  }, language);
}

const styleCss = ".sc-ig-accounts-actions-h{display:flex;flex-flow:column}[hidden].sc-ig-accounts-actions-h{display:none}div.sc-ig-accounts-actions:first-of-type{z-index:3;padding:0.5em 0 0.5em 0.4em;overflow:hidden;position:fixed;top:8.5em;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-ig-accounts-actions-h>div.sc-ig-accounts-actions>smoothly-trigger.sc-ig-accounts-actions{border-radius:0;border:solid 1px rgb(var(--smoothly-color))}@keyframes slide{0%{width:0}}.sc-ig-accounts-actions-h>portal-create-order.sc-ig-accounts-actions,.sc-ig-accounts-actions-h>ig-subscription-create.sc-ig-accounts-actions{background-color:rgb(var(--smoothly-medium-contrast));border:solid 1px rgb(var(--smoothly-medium-shade));box-shadow:var(--smoothly-shadow);border-radius:.6em;position:fixed;z-index:3;right:5em;padding:1em}.sc-ig-accounts-actions-h>portal-create-order.sc-ig-accounts-actions{padding-bottom:.5em;top:6em}.sc-ig-accounts-actions-h>ig-subscription-create.sc-ig-accounts-actions{top:9em}.arrow.sc-ig-accounts-actions{background-color:rgb(var(--smoothly-medium-contrast));border-color:rgb(var(--smoothly-medium-shade));width:30px;height:30px;border-top-style:solid;border-top-width:1px;border-right-style:solid;border-right-width:1px;position:fixed;z-index:3;right:4em;transform:rotate(45deg)}.sc-ig-accounts-actions-h>portal-create-order.sc-ig-accounts-actions+.arrow.sc-ig-accounts-actions{top:9.5em}.sc-ig-accounts-actions-h>ig-subscription-create.sc-ig-accounts-actions+.arrow.sc-ig-accounts-actions{top:12em}.sc-ig-accounts-actions-h .background.sc-ig-accounts-actions{position:fixed;top:0;left:0;width:100vw;height:100vh;z-index:2;background:rgba(var(--smoothly-warning-contrast), 0.5)}";

const IntergiroAccountsActions = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.notice = index$1.createEvent(this, "notice", 7);
    this.selected = {};
  }
  componentWillLoad() {
    this.t = create(this.element);
  }
  retry() {
    this.notice.emit(index$2.Notice.execute(this.t("Retrying payment."), async () => {
      let result;
      const response = await index$3.Connection.put("retry", {});
      if (index$4.Error.is(response))
        result = [false, this.t("Your payment was declined.")];
      else {
        result = [true, this.t("Payment succeeded.")];
      }
      return result;
    }));
  }
  equalize() {
    const idArray = Object.keys(this.selected);
    this.notice.emit(index$2.Notice.execute(this.t("Equalizing balance..."), async () => {
      let result;
      const response = await Promise.all(idArray.map(id => index$3.Connection.delete(`account/${id}/balance`)));
      const [succeeded, failed] = response.reduce(([s, f], e) => (index$4.Error.is(e) ? [s, ++f] : [++s, f]), [0, 0]);
      if (succeeded > 0)
        result = [
          true,
          idArray.length > 1
            ? this.t("$0 of $1 balances were equalized.", succeeded, idArray.length)
            : this.t("Balance was equalized."),
        ];
      else
        result = [
          false,
          idArray.length > 1
            ? this.t("Failed to equalize $0 of $1 balances.", failed, idArray.length)
            : this.t("Failed to equalize balance."),
        ];
      return result;
    }));
  }
  toggle(action) {
    this.opened = action == this.opened ? "" : action;
  }
  render() {
    const accounts = Object.values(this.selected).filter(index$3.Account.is);
    const retry = accounts.every(a => a.status == "suspended" || a.status == "pending");
    return [
      index$1.h("div", { style: Object.entries(this.selected).length > 0 ? { right: "0" } : {} }, index$1.h("smoothly-trigger", { color: "success", name: "cancel", onClick: () => this.toggle("order") }, index$1.h("smoothly-icon", { toolTip: this.opened == "order" ? this.t("Cancel") : this.t("Create order"), name: this.opened == "order" ? "close" : "add" })), index$1.h("smoothly-trigger", { hidden: !this.whitelist, slot: "icon", color: "success", onClick: () => this.toggle("subscription") }, index$1.h("smoothly-icon", { toolTip: this.t("Create subscription"), name: "calendar-outline" })), index$1.h("smoothly-trigger", { color: "warning", hidden: !this.whitelist, slot: "icon", onClick: () => this.equalize() }, index$1.h("smoothly-icon", { toolTip: this.t("Equalize balance to zero"), name: "cash-outline" })), index$1.h("smoothly-trigger", { color: "danger", disabled: !retry, name: "retry", onClick: () => this.retry() }, index$1.h("smoothly-icon", { toolTip: this.t("Retry payment"), name: "refresh" }))),
      (this.opened == "order" || this.opened == "subscription") && Object.entries(this.selected).length > 0
        ? [
          this.opened == "order" ? (index$1.h("portal-create-order", { type: "account", selected: this.selected })) : (index$1.h("ig-subscription-create", { accounts: this.selected })),
          index$1.h("div", { class: "arrow" }),
          index$1.h("div", { class: "background", onClick: () => this.toggle(this.opened) }),
        ]
        : "",
    ];
  }
  get element() { return index$1.getElement(this); }
};
IntergiroAccountsActions.style = styleCss;

exports.ig_accounts_actions = IntergiroAccountsActions;
