import { r as registerInstance, c as createEvent, h, g as getElement } from './index-65e437df.js';
import './App-bfd9e3ff.js';
import './_commonjsHelpers-7b8ed50b.js';
import './index-4de77e65.js';
import { N as Notice } from './Notice-668f9bc7.js';
import { C as Connection, A as Account } from './index-d229c0e9.js';
import { d as dist } from './index-15e1c25d.js';
import { d as dist$1 } from './index-d78baa35.js';

const sv = {
  "Create account": "Skapa konto",
  Close: "Stäng",
};

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

const styleCss = ".sc-payfunc-account-create-popup-h{position:sticky;float:right;width:34em;max-width:85%}.sc-payfunc-account-create-popup-h>smoothly-icon.sc-payfunc-account-create-popup{position:relative;cursor:pointer;float:right;z-index:2}.sc-payfunc-account-create-popup-h>payfunc-account-create.sc-payfunc-account-create-popup{background-color:rgb(var(--smoothly-medium-contrast));border:solid 1px rgb(var(--smoothly-medium-shade));border-radius:.6em;padding-left:1em;padding-right:1em;padding-top:1em;padding-bottom:.5em;position:absolute;right:-1.4em;bottom:4.2em;z-index:2}.arrow.sc-payfunc-account-create-popup{background-color:rgb(var(--smoothly-medium-contrast));border-color:rgb(var(--smoothly-medium-shade));width:30px;height:30px;border-bottom-style:solid;border-bottom-width:1px;border-right-style:solid;border-right-width:1px;position:absolute;right:.5em;bottom:3.2em;transform:rotate(45deg);z-index:2}.sc-payfunc-account-create-popup-h .background.sc-payfunc-account-create-popup{position:fixed;top:0;left:0;width:100vw;height:100vh;z-index:1}";

const PayfuncAccountCreatePopup = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.create = createEvent(this, "create", 7);
    this.opened = false;
    this.visible = false;
  }
  componentWillLoad() {
    this.t = create(this.element);
  }
  onCreate(e) {
    this.create.emit(e.detail);
  }
  onClose(e) {
    this.opened = false;
  }
  render() {
    return [
      this.opened
        ? [
          h("payfunc-account-create", { onClose: (e) => this.onClose(e), onCreateAccount: (e) => this.onCreate(e) }),
          h("div", { class: "arrow" }),
          h("div", { class: "background", onClick: () => (this.opened = false) }),
        ]
        : [],
      h("smoothly-icon", { name: this.opened ? "chevron-down-circle" : "add-circle", size: "large", toolTip: this.opened ? this.t("Close") : this.t("Create account"), onClick: () => (this.opened = !this.opened) }),
    ];
  }
  get element() { return getElement(this); }
};
PayfuncAccountCreatePopup.style = styleCss;

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

const styleCss$1 = ".sc-payfunc-accounts-actions-h{display:flex;flex-flow:column}[hidden].sc-payfunc-accounts-actions-h{display:none}div.sc-payfunc-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-payfunc-accounts-actions-h>div.sc-payfunc-accounts-actions>smoothly-trigger.sc-payfunc-accounts-actions{border-radius:0;border:solid 1px rgb(var(--smoothly-color))}@keyframes slide{0%{width:0}}.sc-payfunc-accounts-actions-h>portal-create-order.sc-payfunc-accounts-actions,.sc-payfunc-accounts-actions-h>payfunc-subscription-create.sc-payfunc-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-payfunc-accounts-actions-h>portal-create-order.sc-payfunc-accounts-actions{padding-bottom:.5em;top:6em}.sc-payfunc-accounts-actions-h>payfunc-subscription-create.sc-payfunc-accounts-actions{top:9em}.arrow.sc-payfunc-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-payfunc-accounts-actions-h>portal-create-order.sc-payfunc-accounts-actions+.arrow.sc-payfunc-accounts-actions{top:9.5em}.sc-payfunc-accounts-actions-h>payfunc-subscription-create.sc-payfunc-accounts-actions+.arrow.sc-payfunc-accounts-actions{top:12em}.sc-payfunc-accounts-actions-h .background.sc-payfunc-accounts-actions{position:fixed;top:0;left:0;width:100vw;height:100vh;z-index:2;background:rgba(var(--smoothly-warning-contrast), 0.5)}";

const PayfuncAccountsActions = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.notice = createEvent(this, "notice", 7);
    this.selected = {};
  }
  componentWillLoad() {
    this.t = create$1(this.element);
  }
  retry() {
    this.notice.emit(Notice.execute(this.t("Retrying payment."), async () => {
      let result;
      const response = await Connection.put("retry", {});
      if (dist$1.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(Notice.execute(this.t("Equalizing balance..."), async () => {
      let result;
      const response = await Promise.all(idArray.map(id => Connection.delete(`account/${id}/balance`)));
      const [succeeded, failed] = response.reduce(([s, f], e) => (dist$1.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(Account.is);
    const retry = accounts.every(a => a.status == "suspended" || a.status == "pending");
    return [
      h("div", { style: Object.entries(this.selected).length > 0 ? { right: "0" } : {} }, h("smoothly-trigger", { color: "success", name: "cancel", onClick: () => this.toggle("order") }, h("smoothly-icon", { toolTip: this.opened == "order" ? this.t("Cancel") : this.t("Create order"), name: this.opened == "order" ? "close" : "add" })), h("smoothly-trigger", { hidden: !this.whitelist, slot: "icon", color: "success", onClick: () => this.toggle("subscription") }, h("smoothly-icon", { toolTip: this.t("Create subscription"), name: "calendar-outline" })), h("smoothly-trigger", { color: "warning", hidden: !this.whitelist, slot: "icon", onClick: () => this.equalize() }, h("smoothly-icon", { toolTip: this.t("Equalize balance to zero"), name: "cash-outline" })), h("smoothly-trigger", { color: "danger", disabled: !retry, name: "retry", onClick: () => this.retry() }, 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" ? (h("portal-create-order", { type: "account", selected: this.selected })) : (h("payfunc-subscription-create", { accounts: this.selected })),
          h("div", { class: "arrow" }),
          h("div", { class: "background", onClick: () => this.toggle(this.opened) }),
        ]
        : "",
    ];
  }
  get element() { return getElement(this); }
};
PayfuncAccountsActions.style = styleCss$1;

const sv$2 = {
  "total count: ": "totalt antal: ",
  "active: ": "aktiva: ",
  "created: ": "skapade: ",
  "inactive: ": "inaktiva: ",
  "pending: ": "väntande: ",
  "suspended: ": "avstängda: ",
};

function create$2(language) {
  return dist.create({
    sv: sv$2,
  }, language);
}

const styleCss$2 = "footer.sc-payfunc-accounts-footer{display:block;margin:auto;border:1px solid rgb(var(--smoothly-medium-color));width:100%;max-width:90em;z-index:1;position:sticky;bottom:0;color:rgb(var(--smoothly-light-contrast));background-color:rgb(var(--smoothly-light-color));padding:.5em 0 .5em 0em}.sc-payfunc-accounts-footer-h>footer.sc-payfunc-accounts-footer>td.sc-payfunc-accounts-footer>span.sc-payfunc-accounts-footer{display:inline-block;margin-left:0.4em;margin-right:0.6em}.sc-payfunc-accounts-footer-h>footer.sc-payfunc-accounts-footer>td.sc-payfunc-accounts-footer{display:inline-block;padding-top:0.2em;margin-left:2em}";

const PayfuncAccountsFooter = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
  }
  componentWillLoad() {
    this.t = create$2(this.element);
  }
  render() {
    var _a;
    const [active, created, pending, suspended] = this.accounts
      ? this.accounts.reduce(([a, c, p, s], account) => {
        var _a, _b;
        const status = (_a = account.status) !== null && _a !== void 0 ? _a : Account.Status.getStatus(account, this.orders ? (_b = this.orders[account.id]) === null || _b === void 0 ? void 0 : _b.slice(0, 1).shift() : undefined);
        return status == "active"
          ? [++a, c, p, s]
          : status == "created"
            ? [a, ++c, p, s]
            : status == "pending"
              ? [a, c, p++, s]
              : [a, c, p, ++s];
      }, [0, 0, 0, 0])
      : [0, 0, 0, 0];
    return (h("footer", null, h("td", null, h("span", null, this.t("total count: ")), h("span", null, this.accounts ? (_a = this.accounts) === null || _a === void 0 ? void 0 : _a.length : 0)), h("td", null, h("span", null, this.t("active: ")), h("span", null, active)), h("td", null, h("span", null, this.t("created: ")), h("span", null, created)), h("td", null, h("span", null, this.t("pending: ")), h("span", null, pending)), h("td", null, h("span", null, this.t("suspended: ")), h("span", null, suspended))));
  }
  get element() { return getElement(this); }
};
PayfuncAccountsFooter.style = styleCss$2;

const sv$3 = {
  "Account number": "Kontonummer",
  Customer: "Kund",
  Method: "Metod",
  Orders: "Ordrar",
  Subscription: "Prenumeration",
  Balance: "Saldo",
  "Select all accounts": "Markera alla kunder",
  "Deselect all accounts": "Avmarkera alla kunder",
  "Loading accounts from server.": "Laddar konton från servern.",
  "There are no accounts to display.": "Det finns inga konton att visa.",
  "Load more accounts": "Ladda in fler konton",
};

function create$3(language) {
  return dist.create({
    sv: sv$3,
  }, language);
}

const styleCss$3 = ".sc-payfunc-accounts-table-h{display:block;width:100%;max-width:90em;margin:auto;border:1px solid rgb(var(--smoothly-medium-color))}[hidden].sc-payfunc-accounts-table-h{display:none}.sc-payfunc-accounts-table-h>p.sc-payfunc-accounts-table{padding-left:.5em}.sc-payfunc-accounts-table-h>table.sc-payfunc-accounts-table{width:100%}.sc-payfunc-accounts-table-h>table.sc-payfunc-accounts-table>thead.sc-payfunc-accounts-table>tr.sc-payfunc-accounts-table{display:table-row;font-weight:bold;height:2em}.sc-payfunc-accounts-table-h>table.sc-payfunc-accounts-table>thead.sc-payfunc-accounts-table>tr.sc-payfunc-accounts-table>*.sc-payfunc-accounts-table{display:table-cell;padding-right:.5em;padding-left:.3em;vertical-align:middle;position:sticky;top:1.55cm;z-index:1;text-align:left}.sc-payfunc-accounts-table-h>table.sc-payfunc-accounts-table>thead.sc-payfunc-accounts-table>tr.sc-payfunc-accounts-table>*.sc-payfunc-accounts-table>smoothly-icon.sc-payfunc-accounts-table{vertical-align:-50%;margin-left:.1em}.sc-payfunc-accounts-table-h>table.sc-payfunc-accounts-table>td.sc-payfunc-accounts-table{text-align:center}.sc-payfunc-accounts-table-h>table.sc-payfunc-accounts-table>td.sc-payfunc-accounts-table>button.sc-payfunc-accounts-table>smoothly-icon.sc-payfunc-accounts-table{cursor:pointer}";

const PayfuncAccountsTable = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.selectedChanged = createEvent(this, "selectedChanged", 7);
    this.selected = {};
    this.count = 100;
    this.opened = {};
  }
  componentWillLoad() {
    this.t = create$3(this.element);
  }
  onChecked(event) {
    this.selected = {};
    if (event.detail.selected && this.accounts)
      for (const account of this.accounts)
        this.selected[account.id] = account;
    this.selectedChanged.emit(this.selected);
    event.stopPropagation();
    event.preventDefault();
  }
  handleToggle(event) {
    const accounts = Object.assign({}, this.selected);
    if (event.detail.checked)
      accounts[event.detail.account.id] = event.detail.account;
    else
      delete accounts[event.detail.account.id];
    this.selectedChanged.emit((this.selected = accounts));
  }
  toggleColumn(e) {
    const opened = Object.assign({}, this.opened);
    if (e.detail["id"] && opened[e.detail.id] == e.detail.column)
      delete opened[e.detail.id];
    else
      opened[e.detail.id] = e.detail.column;
    this.opened = opened;
  }
  render() {
    const t = this.t;
    let result;
    if (!this.accounts)
      result = h("p", null, t("Loading accounts from server."));
    else if (this.accounts.length <= 0)
      result = h("p", null, t("There are no accounts to display."));
    else {
      result = [
        h("table", null, h("thead", null, h("tr", null, h("th", { style: { width: "5%" }, color: "light" }, h("smoothly-checkbox", { "select-all": true })), h("th", { color: "light" }, t("Account number")), h("th", { color: "light" }, t("Customer")), h("th", { color: "light" }, t("Orders")), this.whitelist ? h("th", { color: "light" }, t("Subscription")) : [], h("th", { color: "light" }, t("Method")), this.whitelist ? h("th", { color: "light" }, t("Balance")) : [], h("th", { color: "light" }, t("Login")), h("th", { color: "light" }, t("Status")))), this.accounts
          ? this.accounts
            .slice(0, this.count)
            .map(account => {
            var _a, _b;
            return [
              h("payfunc-account-table-row", { account: account, whitelist: this.whitelist, orders: this.orders ? this.orders[account.id] : undefined, opened: (_a = this.opened[account.id]) !== null && _a !== void 0 ? _a : "", selected: this.selected.hasOwnProperty.call(this.selected, account.id) }),
              h("payfunc-account-details", { opened: (_b = this.opened[account.id]) !== null && _b !== void 0 ? _b : "", orders: this.orders ? this.orders[account.id] : undefined, whitelist: this.whitelist, account: account }),
            ];
          })
          : null, h("td", { colSpan: 8 }, this.count < this.accounts.length ? (h("button", { onClick: () => (this.count += 100) }, h("smoothly-icon", { name: "chevron-down-circle", size: "large", toolTip: this.t("Load more accounts") }))) : (""))),
      ];
    }
    return result;
  }
  get element() { return getElement(this); }
};
PayfuncAccountsTable.style = styleCss$3;

export { PayfuncAccountCreatePopup as payfunc_account_create_popup, PayfuncAccountsActions as payfunc_accounts_actions, PayfuncAccountsFooter as payfunc_accounts_footer, PayfuncAccountsTable as payfunc_accounts_table };
