import { r as registerInstance, h, c as createEvent, 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 { e as Schedule, c as Customer, N as Name, C as Connection, A as Account, I as Item } from './index-5a6f97d3.js';
import { d as dist } from './index-15e1c25d.js';
import { d as dist$1 } from './index-d78baa35.js';
import { d as dist$2 } from './index-06228124.js';

const styleCss = ".sc-payfunc-account-balance-h{display:block;padding-top:.5em}[hidden].sc-payfunc-account-balance-h{display:none}.sc-payfunc-account-balance-h>section.sc-payfunc-account-balance>payfunc-items-list.sc-payfunc-account-balance{padding-top:1em}.sc-payfunc-account-balance-h>section.sc-payfunc-account-balance:first-of-type{display:flex;justify-content:space-between}.sc-payfunc-account-balance-h>section.sc-payfunc-account-balance:last-of-type{display:block}payfunc-future-due-dates.sc-payfunc-account-balance{min-width:13em}payfunc-orders-table.sc-payfunc-account-balance{border:1px solid rgb(var(--smoothly-medium-color));margin-top:1em}main.sc-payfunc-account-balance{display:flex;flex-wrap:wrap}main.sc-payfunc-account-balance>*.sc-payfunc-account-balance{width:min-content}";

const PayfuncAccountBalance = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
  }
  componentWillLoad() {
    this.filterOrders();
  }
  filterOrders() {
    const orders = this.orders
      ? this.orders.filter(o => o.payment.type == "account" || o.payment.type == "card" ? o.payment.charge == "balance" : false)
      : undefined;
    this.ordersFiltered = orders;
  }
  render() {
    return [
      h("section", null, h("main", null, h("payfunc-schedule", { schedule: this.account.schedule }), this.account.limit ? h("payfunc-limit", { account: this.account }) : []), h("payfunc-future-due-dates", { dates: Schedule.calculateDueDates(this.account.schedule, 4) })),
      h("section", null, this.account.balance.length > 0 ? h("payfunc-items-list", { items: this.account.balance }) : [], this.ordersFiltered && this.ordersFiltered.length > 0 ? (h("payfunc-orders-table", { display: true, orders: this.ordersFiltered })) : ([])),
    ];
  }
  static get watchers() { return {
    "orders": ["filterOrders"]
  }; }
};
PayfuncAccountBalance.style = styleCss;

const sv = {
  Name: "Namn",
  Type: "Typ",
  Number: "Nummer",
  "Identity number": "Personnummer",
  Email: "E-post",
  "Phone number": "Telefonnummer",
  Address: "Adress",
  "Failed to update customer information.": "Kunde inte uppdatera kundinformation.",
  "Updated customer information.": "Kundinformation uppdaterad.",
  "Updating customer information...": "Uppdaterar kundinformation...",
};

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

const styleCss$1 = ".sc-payfunc-account-customer-h{display:flex;flex-wrap:wrap;justify-content:space-between}[hidden].sc-payfunc-account-customer-h{display:none}.sc-payfunc-account-customer-h>section.sc-payfunc-account-customer{width:50%;min-width:17em;display:flex;flex-direction:column}.sc-payfunc-account-customer-h>section.sc-payfunc-account-customer>label.sc-payfunc-account-customer{font-weight:bold}.sc-payfunc-account-customer-h>section.sc-payfunc-account-customer>payfunc-display-edit.sc-payfunc-account-customer{padding-top:.5em}.sc-payfunc-account-customer-h>section.sc-payfunc-account-customer>*.sc-payfunc-account-customer:not(label){min-height:5em;max-width:14em}.sc-payfunc-account-customer-h>section.sc-payfunc-account-customer>label.sc-payfunc-account-customer{padding-top:1em}";

const PayfuncAccountCustomer = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.updateCustomer = createEvent(this, "updateCustomer", 7);
    this.types = ["organization", "person"];
  }
  componentWillLoad() {
    this.t = create(this.element);
  }
  async update(property, value) {
    var _a;
    if (!value || Object.keys(value).length == 0)
      (_a = this.account.customer) === null || _a === void 0 ? true : delete _a[property];
    else {
      const update = { [property]: value };
      if (Customer.is(update))
        this.account.customer = Object.assign(Object.assign({}, this.account.customer), update);
    }
    this.updateCustomer.emit(this.account.customer);
  }
  render() {
    var _a, _b;
    const customer = this.account.customer;
    return [
      h("section", null, h("label", null, this.t("Name")), h("payfunc-display-edit", { type: "text", name: this.t("Name"), value: (customer === null || customer === void 0 ? void 0 : customer.name) ? Name.get(customer.name) : undefined, onChanged: (e) => this.update("name", e.detail) })),
      h("section", null, h("label", null, this.t("Type")), h("payfunc-display-edit", { type: "text", name: this.t("Type"), value: customer === null || customer === void 0 ? void 0 : customer.type, select: this.types, onChanged: (e) => this.update("type", e.detail) })),
      h("section", null, h("label", null, this.t("Number")), h("smoothly-display", { type: "text", value: customer === null || customer === void 0 ? void 0 : customer.number })),
      h("section", null, h("label", null, this.t("Identity number")), h("payfunc-display-edit", { type: "identity-number", name: this.t("Identity number"), value: customer === null || customer === void 0 ? void 0 : customer.identityNumber, onChanged: (e) => this.update("identityNumber", e.detail) })),
      h("section", null, h("label", null, this.t("Email")), h("payfunc-email-addresses", { value: (_a = customer === null || customer === void 0 ? void 0 : customer.email) !== null && _a !== void 0 ? _a : "", onChanged: (e) => this.update("email", e.detail) })),
      h("section", null, h("label", null, this.t("Phone number")), h("payfunc-phone-numbers", { value: (_b = customer === null || customer === void 0 ? void 0 : customer.phone) !== null && _b !== void 0 ? _b : "", onChanged: (e) => this.update("phone", e.detail) })),
      h("section", null, h("label", null, this.t("Address")), h("payfunc-addresses", { value: customer === null || customer === void 0 ? void 0 : customer.address, onChanged: (e) => this.update("address", e.detail) })),
    ];
  }
  get element() { return getElement(this); }
};
PayfuncAccountCustomer.style = styleCss$1;

const sv$1 = {
  "Failed to update customer information.": "Kunde inte uppdatera kundinformation.",
  "Updated customer information.": "Kundinformation uppdaterad.",
  "Updating customer information...": "Uppdaterar kundinformation...",
};

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

const styleCss$2 = ".sc-payfunc-account-details-h{display:table-row}.sc-payfunc-account-details-h>td.sc-payfunc-account-details{border-top:solid 1px rgb(var(--smoothly-medium-shade));border-bottom:solid 1px rgb(var(--smoothly-medium-shade));background-color:rgb(var(--smoothly-dark-shade));padding:0.5em}payfunc-subscription-table.sc-payfunc-account-details,payfunc-orders-table.sc-payfunc-account-details{margin:auto;width:90%;border:solid 1px rgb(var(--smoothly-medium-shade))}payfunc-account-customer.sc-payfunc-account-details{max-width:70em;margin:auto;padding:1em;padding-left:4.5em}payfunc-account-balance.sc-payfunc-account-details{width:75%;margin:auto}";

const PayfuncAccountDetails = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.notice = createEvent(this, "notice", 7);
    this.updateAccounts = createEvent(this, "updateAccounts", 7);
    this.opened = "";
  }
  componentWillLoad() {
    this.t = create$1(this.element);
  }
  async updateCustomer(event) {
    this.notice.emit(Notice.execute(this.t("Updating customer information..."), async () => {
      let result;
      this.account.customer = event.detail;
      const response = await Connection.put(`account/${this.account.id}/customer`, this.account.customer);
      if (dist$1.Error.is(response))
        result = [false, this.t("Failed to update customer information.")];
      else {
        this.updateAccounts.emit();
        result = [true, this.t("Updated customer information.")];
      }
      return result;
    }));
  }
  render() {
    let result;
    switch (this.opened) {
      case "number":
        result = (h("payfunc-number-details", { number: this.account.number, identifier: this.account.id, name: "account" }));
        break;
      case "customer":
        result = h("payfunc-account-customer", { account: this.account });
        break;
      case "order":
        result = h("payfunc-orders-table", { orders: this.orders, display: true, receipt: this.whitelist });
        break;
      case "subscription":
        result = (h("payfunc-subscription-table", { subscriptions: this.account.subscription, account: this.account }));
        break;
      case "method":
        result = h("payfunc-account-method", { account: this.account });
        break;
      case "balance":
        result = h("payfunc-account-balance", { account: this.account, orders: this.orders });
        break;
      default:
        return "";
    }
    return h("td", { colSpan: 9 }, result);
  }
  get element() { return getElement(this); }
};
PayfuncAccountDetails.style = styleCss$2;

const sv$2 = {
  "Deleting payment method...": "Raderar betalningsmetod...",
  "Updating payment methods...": "Uppdaterar betalningsmetoder...",
  "Failed to delete payment method.": "Kunde inte radera betalningsmetod.",
  "Failed to update payment methods.": "Kunde inte uppdatera betalningsmetoder.",
  "Payment method was deleted.": "Betalningsmetod raderades.",
  "Updated payment methods.": "Betalningsmetod uppdaterades.",
};

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

const styleCss$3 = "[hidden].sc-payfunc-account-method-h{display:none}.sc-payfunc-account-method-h{display:flex;padding:1em;padding-left:4.5em}";

const PayfuncAccountMethod = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.reorder = createEvent(this, "reorder", 7);
    this.notice = createEvent(this, "notice", 7);
    this.updateAccounts = createEvent(this, "updateAccounts", 7);
  }
  componentWillLoad() {
    this.t = create$2(this.element);
  }
  async onUpdate(e, event) {
    this.notice.emit(Notice.execute(this.t(event == "delete" ? "Deleting payment method..." : "Updating payment methods..."), async () => {
      let result;
      const response = await Connection.put(`account/${this.account.id}/methods`, e.detail);
      if (dist$1.Error.is(response))
        result = [
          false,
          this.t(event == "delete" ? "Failed to delete payment method." : "Failed to update payment methods."),
        ];
      else {
        result = [true, this.t(event == "delete" ? "Payment method was deleted." : "Updated payment methods.")];
        const accounts = await Connection.get(`account/`);
        this.updateAccounts.emit(accounts);
      }
      return result;
    }));
  }
  render() {
    return (h("payfunc-payment-methods", { onUpdate: (e) => this.onUpdate(e, "update"), onDelete: (e) => this.onUpdate(e, "delete"), deletable: true, methods: this.account.method, mode: "table" }));
  }
  get element() { return getElement(this); }
};
PayfuncAccountMethod.style = styleCss$3;

const sv$3 = {
  Deselect: "Avmarkera",
  Select: "Markera",
  unknown: "okänd",
  none: "ingen",
  active: "aktiva",
  "$0 orders": "$0 ordrar",
  "Send login link via email": "Skicka inloggningslänk via e-post",
  "Successfully sent email": "E-post skickat",
  "Failed to send email": "Misslyckades med att skicka e-post",
  "The customer has no email set": "Kunden har ingen e-post satt",
  "Sending email...": "Skickar e-post...",
  daily: "daglig",
  weekly: "veckovis",
  monthly: "månadsvis",
  quarterly: "kvartalsvis",
  yearly: "årlig",
};

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

const styleCss$4 = ".sc-payfunc-account-table-row-h{display:table-row}.sc-payfunc-account-table-row-h:hover{border-top:1px solid rgb(var(--smoothly-medium-color));border-bottom:1px solid rgb(var(--smoothly-medium-color));box-shadow:var(--smoothly-shadow)}.sc-payfunc-account-table-row-h>td.sc-payfunc-account-table-row>.openable.sc-payfunc-account-table-row{padding:.5em .3em;width:max-content;min-width:3em;display:inline-block}.sc-payfunc-account-table-row-h>td.sc-payfunc-account-table-row>smoothly-checkbox.sc-payfunc-account-table-row{display:inline-block;vertical-align:-40%;margin-left:.3em}.sc-payfunc-account-table-row-h>td.sc-payfunc-account-table-row>div.sc-payfunc-account-table-row{min-width:3em}.sc-payfunc-account-table-row-h>td.sc-payfunc-account-table-row>.openable.sc-payfunc-account-table-row:hover{background-color:rgb(var(--smoothly-dark-shade));cursor:pointer}.sc-payfunc-account-table-row-h>td.sc-payfunc-account-table-row>.openable.opened.sc-payfunc-account-table-row{border:solid 1px  rgb(var(--smoothly-medium-shade));background-color:rgb(var(--smoothly-dark-shade));border-bottom:3px solid rgb(var(--smoothly-dark-shade));border-radius:0 0 2px 2px;margin-bottom:-2px}payfunc-status.sc-payfunc-account-table-row{margin-right:.2em;padding-right:0.5em;flex-shrink:5;width:100%}.sc-payfunc-account-table-row-h>td.sc-payfunc-account-table-row:last-child>div.sc-payfunc-account-table-row{width:100%;max-width:20em;display:flex;align-content:stretch;flex-wrap:nowrap}.sendLogin.sc-payfunc-account-table-row{cursor:pointer;vertical-align:-40%;display:inline-block}smoothly-trigger.sc-payfunc-account-table-row{border:none;padding-left:0.35em}";

const PayfuncAccountTableRow = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.columnClicked = createEvent(this, "columnClicked", 7);
    this.accountSelected = createEvent(this, "accountSelected", 7);
    this.notice = createEvent(this, "notice", 7);
    this.opened = "";
  }
  componentWillLoad() {
    this.t = create$3(this.element);
  }
  handleColumnClick(column) {
    var _a;
    this.opened = column;
    this.columnClicked.emit({ id: (_a = this.account) === null || _a === void 0 ? void 0 : _a.id, column });
  }
  handleSelected(e) {
    this.accountSelected.emit({ account: this.account, checked: !this.selected });
    e.stopPropagation();
    e.preventDefault();
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g;
    return Account.is(this.account)
      ? [
        h("td", null, h("smoothly-checkbox", { selected: this.selected })),
        h("td", null, h("div", { class: "openable" + (this.opened == "number" ? " opened" : ""), onClick: () => this.handleColumnClick("number") }, this.account.number)),
        h("td", null, h("div", { class: "openable" + (this.opened == "customer" ? " opened" : ""), onClick: () => this.handleColumnClick("customer") }, (_a = Customer.getLabel(this.account.customer)) !== null && _a !== void 0 ? _a : this.t("unknown"))),
        h("td", null, h("div", { class: "openable" + (this.opened == "order" ? " opened" : ""), onClick: () => this.handleColumnClick("order") }, this.t("$0 orders", (_c = (_b = this.orders) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0))),
        this.whitelist ? (h("td", null, h("div", { class: "openable" + (this.opened == "subscription" ? " opened" : ""), onClick: () => this.handleColumnClick("subscription") }, !this.account.subscription || this.account.subscription.length < 1
          ? this.t("none")
          : this.account.subscription.length > 1
            ? this.account.subscription.length + " " + this.t("active")
            : this.t(typeof this.account.subscription[0].schedule == "object"
              ? this.account.subscription[0].schedule.frequency
              : this.account.subscription[0].schedule) +
              " " +
              Item.amount(this.account.subscription[0].items) +
              " " +
              this.account.subscription[0].currency))) : ([]),
        h("td", null, h("div", { class: "openable" + (this.opened == "method" ? " opened" : ""), onClick: () => this.handleColumnClick("method") }, this.account.method[0]
          ? dist$2.format(this.account.method[0].iin + "******" + this.account.method[0].last4, "card-number")
          : this.t("none"))),
        this.whitelist ? (h("td", null, h("div", { class: "openable" + (this.opened == "balance" ? " opened" : ""), style: { color: this.account.total < 0 ? "red" : "" }, onClick: () => this.handleColumnClick("balance") }, dist$2.format(this.account.total, "price", this.account.currency)))) : ([]),
        h("td", null, h("smoothly-trigger", { disabled: !((_d = this.account.customer) === null || _d === void 0 ? void 0 : _d.email) }, h("smoothly-icon", { onClick: ((_e = this.account.customer) === null || _e === void 0 ? void 0 : _e.email) ? () => this.sendLogin() : undefined, class: "sendLogin", name: "mail", size: "small", toolTip: !((_f = this.account.customer) === null || _f === void 0 ? void 0 : _f.email)
            ? this.t("The customer has no email set")
            : this.t("Send login link via email") }))),
        h("td", null, h("div", null, h("payfunc-status", { status: (_g = this.account.status) !== null && _g !== void 0 ? _g : Account.Status.getStatus(this.account, this.orders ? this.orders[0] : undefined) }))),
      ]
      : null;
  }
  async sendLogin() {
    this.notice.emit(Notice.execute(this.t("Sending email..."), async () => {
      var _a;
      const response = await Connection.post(`account/${(_a = this.account) === null || _a === void 0 ? void 0 : _a.id}/link?email=true`, {});
      let result;
      if (!dist$1.Error.is(response))
        result = [true, this.t("Successfully sent email")];
      else
        result = [false, this.t("Failed to send email")];
      return result;
    }));
  }
  get element() { return getElement(this); }
};
PayfuncAccountTableRow.style = styleCss$4;

const sv$4 = {
  Number: "Nummer",
  Amount: "Summa",
  Schedule: "Schema",
  Start: "Påbörjar",
  End: "Upphör",
  Due: "Nästa betalning",
  "There are no subscriptions to display.": "Det finns inga prenummerationer att visa.",
};

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

const styleCss$5 = ".sc-payfunc-subscription-table-h{display:block;width:100%;max-width:90em;margin:auto}[hidden].sc-payfunc-subscription-table-h{display:none}.sc-payfunc-subscription-table-h>p.sc-payfunc-subscription-table{padding-left:1em}.sc-payfunc-subscription-table-h>table.sc-payfunc-subscription-table{width:100%}.sc-payfunc-subscription-table-h>table.sc-payfunc-subscription-table>thead.sc-payfunc-subscription-table>tr.sc-payfunc-subscription-table{display:table-row;font-weight:bold;height:2em}.sc-payfunc-subscription-table-h>table.sc-payfunc-subscription-table>thead.sc-payfunc-subscription-table>tr.sc-payfunc-subscription-table>*.sc-payfunc-subscription-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-subscription-table-h>table.sc-payfunc-subscription-table>td.sc-payfunc-subscription-table{text-align:center}";

const PayfuncSubscriptionTable = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.opened = {};
  }
  componentWillLoad() {
    this.t = create$4(this.element);
  }
  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() {
    return [
      this.subscriptions ? ([
        h("table", null, h("thead", null, h("tr", null, h("th", { color: "light" }, this.t("Number")), h("th", { color: "light" }, this.t("Amount")), h("th", { color: "light" }, this.t("Schedule")), h("th", { color: "light" }, this.t("Start")), h("th", { color: "light" }, this.t("End")), h("th", { color: "light" }, this.t("Due")))), this.subscriptions.map(s => {
          var _a;
          return [
            h("payfunc-subscription-table-row", { opened: this.opened[s.id], subscription: s }),
            h("payfunc-subscription-details", { opened: (_a = this.opened[s.id]) !== null && _a !== void 0 ? _a : "", subscription: s, account: this.account }),
          ];
        })),
      ]) : (h("p", null, this.t("There are no subscriptions to display."))),
    ];
  }
  get element() { return getElement(this); }
};
PayfuncSubscriptionTable.style = styleCss$5;

export { PayfuncAccountBalance as payfunc_account_balance, PayfuncAccountCustomer as payfunc_account_customer, PayfuncAccountDetails as payfunc_account_details, PayfuncAccountMethod as payfunc_account_method, PayfuncAccountTableRow as payfunc_account_table_row, PayfuncSubscriptionTable as payfunc_subscription_table };
