import { r as registerInstance, h, c as createEvent, g as getElement } from './index-98dfb63a.js';
import { h as Schedule, e as Contact, N as Name, C as Connection, a as Customer, I as Item } from './index-997ef4e9.js';
import { d as dist } from './index-649488b2.js';
import { E as Error } from './unknown-47cf9199.js';
import './App-c629bbee.js';
import './Token-0644b4fc.js';
import { N as Notice } from './Notice-668f9bc7.js';
import { d as dist$1 } from './index-0d479d1d.js';

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

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

const sv$4 = {
  Name: "Namn",
  Type: "Typ",
  Number: "Nummer",
  "Identity number": "Personnummer",
  Email: "E-post",
  "Phone number": "Telefonnummer",
  Address: "Adress",
};

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

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

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

const sv$3 = {
  "Failed to update contact information.": "Kunde inte uppdatera kontaktinformation.",
  "Updated contact information.": "Kontaktinformation uppdaterad.",
  "Updating contact information...": "Uppdaterar kontaktinformation...",
};

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

const styleCss$3 = ".sc-ig-customer-details-h{display:table-row}.sc-ig-customer-details-h>td.sc-ig-customer-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}ig-subscription-table.sc-ig-customer-details,ig-orders-table.sc-ig-customer-details{margin:auto;width:90%;border:solid 1px rgb(var(--smoothly-medium-shade))}ig-customer-contact.sc-ig-customer-details{max-width:70em;margin:auto;padding:1em;padding-left:4.5em}ig-customer-balance.sc-ig-customer-details{width:75%;margin:auto}";

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

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$2 = "[hidden].sc-ig-customer-method-h{display:none}.sc-ig-customer-method-h{display:-ms-flexbox;display:flex;padding:1em;padding-left:4.5em}";

const IntergiroCustomerMethod = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.reorder = createEvent(this, "reorder", 7);
    this.notice = createEvent(this, "notice", 7);
    this.updateCustomers = createEvent(this, "updateCustomers", 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(`customer/${this.customer.id}/methods`, e.detail);
      if (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 customers = await Connection.get(`customer/`);
        this.updateCustomers.emit(customers);
      }
      return result;
    }));
  }
  render() {
    return (h("ig-payment-methods", { onUpdate: (e) => this.onUpdate(e, "update"), onDelete: (e) => this.onUpdate(e, "delete"), deletable: true, methods: this.customer.method, mode: "table" }));
  }
  get element() { return getElement(this); }
};
IntergiroCustomerMethod.style = styleCss$2;

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

const styleCss$1 = ".sc-ig-customer-table-row-h{display:table-row;border-top:1px solid rgba(var(--smoothly-medium-color), 0);border-bottom:1px solid rgba(var(--smoothly-medium-color), 0)}.sc-ig-customer-table-row-h:hover{-webkit-box-shadow:var(--smoothly-shadow);box-shadow:var(--smoothly-shadow);border-top-color:rgb(var(--smoothly-medium-color));border-bottom-color:rgb(var(--smoothly-medium-color))}.sc-ig-customer-table-row-h>td.sc-ig-customer-table-row>.openable.sc-ig-customer-table-row{padding:.5em .3em;width:-webkit-max-content;width:-moz-max-content;width:max-content;min-width:3em;display:inline-block}.sc-ig-customer-table-row-h>td.sc-ig-customer-table-row>smoothly-checkbox.sc-ig-customer-table-row{display:inline-block;vertical-align:-40%;margin-left:.3em}.sc-ig-customer-table-row-h>td.sc-ig-customer-table-row>div.sc-ig-customer-table-row{min-width:3em}.sc-ig-customer-table-row-h>td.sc-ig-customer-table-row>.openable.sc-ig-customer-table-row:hover{background-color:rgb(var(--smoothly-dark-shade));cursor:pointer}.sc-ig-customer-table-row-h>td.sc-ig-customer-table-row>.openable.opened.sc-ig-customer-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}ig-status.sc-ig-customer-table-row{margin-right:.2em;padding-right:0.5em;-ms-flex-negative:5;flex-shrink:5;width:100%}.sc-ig-customer-table-row-h>td.sc-ig-customer-table-row:last-child>div.sc-ig-customer-table-row{width:100%;max-width:20em;display:-ms-flexbox;display:flex;-ms-flex-line-pack:stretch;align-content:stretch;-ms-flex-wrap:nowrap;flex-wrap:nowrap}.sendLogin.sc-ig-customer-table-row{cursor:pointer;vertical-align:-40%;display:inline-block}smoothly-trigger.sc-ig-customer-table-row{border:none;padding-left:0.35em}";

const IntergiroCustomerTableRow = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.columnClicked = createEvent(this, "columnClicked", 7);
    this.customerSelected = createEvent(this, "customerSelected", 7);
    this.notice = createEvent(this, "notice", 7);
    this.opened = "";
  }
  componentWillLoad() {
    this.t = create$1(this.element);
  }
  handleColumnClick(column) {
    var _a;
    this.opened = column;
    this.columnClicked.emit({ id: (_a = this.customer) === null || _a === void 0 ? void 0 : _a.id, column });
  }
  handleSelected(e) {
    this.customerSelected.emit({ customer: this.customer, checked: !this.selected });
    e.stopPropagation();
    e.preventDefault();
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g;
    return Customer.is(this.customer)
      ? [
        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.customer.number)),
        h("td", null, h("div", { class: "openable" + (this.opened == "contact" ? " opened" : ""), onClick: () => this.handleColumnClick("contact") }, (_a = Contact.getLabel(this.customer.contact)) !== 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.customer.subscription || this.customer.subscription.length < 1
          ? this.t("none")
          : this.customer.subscription.length > 1
            ? this.customer.subscription.length + " " + this.t("active")
            : this.t(typeof this.customer.subscription[0].schedule == "object"
              ? this.customer.subscription[0].schedule.frequency
              : this.customer.subscription[0].schedule) +
              " " +
              Item.amount(this.customer.subscription[0].items) +
              " " +
              this.customer.subscription[0].currency))) : ([]),
        h("td", null, h("div", { class: "openable" + (this.opened == "method" ? " opened" : ""), onClick: () => this.handleColumnClick("method") }, this.customer.method[0]
          ? dist$1.format(this.customer.method[0].iin + "******" + this.customer.method[0].last4, "card-number")
          : this.t("none"))),
        this.whitelist ? (h("td", null, h("div", { class: "openable" + (this.opened == "balance" ? " opened" : ""), style: { color: this.customer.total < 0 ? "red" : "" }, onClick: () => this.handleColumnClick("balance") }, dist$1.format(this.customer.total, "price", this.customer.currency)))) : ([]),
        h("td", null, h("smoothly-trigger", { disabled: !((_d = this.customer.contact) === null || _d === void 0 ? void 0 : _d.email) }, h("smoothly-icon", { onClick: ((_e = this.customer.contact) === null || _e === void 0 ? void 0 : _e.email) ? () => this.sendLogin() : undefined, class: "sendLogin", name: "mail", size: "small", toolTip: !((_f = this.customer.contact) === 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("ig-status", { status: (_g = this.customer.status) !== null && _g !== void 0 ? _g : Customer.Status.getStatus(this.customer, 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(`customer/${(_a = this.customer) === null || _a === void 0 ? void 0 : _a.id}/link?email=true`, {});
      return !Error.is(response)
        ? [true, this.t("Successfully sent email")]
        : [false, this.t("Failed to send email")];
    }));
  }
  get element() { return getElement(this); }
};
IntergiroCustomerTableRow.style = styleCss$1;

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

const styleCss = ".sc-ig-subscription-table-h{display:block;width:100%;max-width:90em;margin:auto}[hidden].sc-ig-subscription-table-h{display:none}.sc-ig-subscription-table-h>p.sc-ig-subscription-table{padding-left:1em}.sc-ig-subscription-table-h>table.sc-ig-subscription-table{width:100%}.sc-ig-subscription-table-h>table.sc-ig-subscription-table>thead.sc-ig-subscription-table>tr.sc-ig-subscription-table{display:table-row;font-weight:bold;height:2em}.sc-ig-subscription-table-h>table.sc-ig-subscription-table>thead.sc-ig-subscription-table>tr.sc-ig-subscription-table>*.sc-ig-subscription-table{display:table-cell;padding-right:.5em;padding-left:.3em;vertical-align:middle;position:-webkit-sticky;position:sticky;top:1.55cm;z-index:1;text-align:left}.sc-ig-subscription-table-h>table.sc-ig-subscription-table>td.sc-ig-subscription-table{text-align:center}";

const IntergiroSubscriptionTable = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.opened = {};
  }
  componentWillLoad() {
    this.t = create(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("ig-subscription-table-row", { opened: this.opened[s.id], subscription: s }),
            h("ig-subscription-details", { opened: (_a = this.opened[s.id]) !== null && _a !== void 0 ? _a : "", subscription: s, customer: this.customer }),
          ];
        })),
      ]) : (h("p", null, this.t("There are no subscriptions to display."))),
    ];
  }
  get element() { return getElement(this); }
};
IntergiroSubscriptionTable.style = styleCss;

export { IntergiroCustomerBalance as ig_customer_balance, IntergiroCustomerContact as ig_customer_contact, IntergiroCustomerDetails as ig_customer_details, IntergiroCustomerMethod as ig_customer_method, IntergiroCustomerTableRow as ig_customer_table_row, IntergiroSubscriptionTable as ig_subscription_table };
