'use strict';

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

const index$1 = require('./index-223abef7.js');
const unknown = require('./unknown-f63ab5c0.js');
require('./App-1795c305.js');
require('./Token-09f9b5d6.js');
const Notice = require('./Notice-e970b490.js');
const index$2 = require('./index-7bb66b0e.js');
const index = require('./index-65f2d9fe.js');

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

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

const IntergiroCustomerActions = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.notice = index$1.createEvent(this, "notice", 7);
    this.selected = {};
  }
  componentWillLoad() {
    this.t = create$3(this.element);
  }
  retry() {
    this.notice.emit(Notice.Notice.execute(this.t("Retrying payment."), async () => {
      const response = await index$2.Connection.put("retry", {});
      return unknown.Error.is(response)
        ? [false, this.t("Your payment was declined.")]
        : [true, this.t("Payment succeeded.")];
    }));
  }
  equalize() {
    const idArray = Object.keys(this.selected);
    this.notice.emit(Notice.Notice.execute(this.t("Equalizing balance..."), async () => {
      let result;
      const response = await Promise.all(idArray.map(id => index$2.Connection.delete(`customer/${id}/balance`)));
      const [succeeded, failed] = response.reduce(([s, f], e) => (unknown.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 customers = Object.values(this.selected).filter(index$2.Customer.is);
    const retry = customers.every(c => c.status == "suspended" || c.status == "pending");
    return [
      index$1.h("div", { style: customers.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: "customer", selected: this.selected })) : (index$1.h("ig-subscription-create", { customers: 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); }
};
IntergiroCustomerActions.style = styleCss$3;

const sv$2 = {
  "Create customer": "Skapa kund",
  Close: "Stäng",
};

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

const styleCss$2 = ".sc-ig-customer-create-popup-h{position:-webkit-sticky;position:sticky;float:right;width:34em;max-width:85%}.sc-ig-customer-create-popup-h>smoothly-icon.sc-ig-customer-create-popup{position:relative;cursor:pointer;float:right;z-index:2}.sc-ig-customer-create-popup-h>ig-customer-create.sc-ig-customer-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-ig-customer-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;-webkit-transform:rotate(45deg);transform:rotate(45deg);z-index:2}.sc-ig-customer-create-popup-h .background.sc-ig-customer-create-popup{position:fixed;top:0;left:0;width:100vw;height:100vh;z-index:1}";

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

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

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

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

const IntergiroCustomerFooter = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
  }
  componentWillLoad() {
    this.t = create$1(this.element);
  }
  render() {
    var _a;
    const [active, created, pending, suspended] = this.customers
      ? this.customers.reduce(([a, c, p, s], customer) => {
        var _a, _b;
        const status = (_a = customer.status) !== null && _a !== void 0 ? _a : index$2.Customer.Status.getStatus(customer, this.orders ? (_b = this.orders[customer.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 (index$1.h("footer", null, index$1.h("td", null, index$1.h("span", null, this.t("total count: ")), index$1.h("span", null, this.customers ? (_a = this.customers) === null || _a === void 0 ? void 0 : _a.length : 0)), index$1.h("td", null, index$1.h("span", null, this.t("active: ")), index$1.h("span", null, active)), index$1.h("td", null, index$1.h("span", null, this.t("created: ")), index$1.h("span", null, created)), index$1.h("td", null, index$1.h("span", null, this.t("pending: ")), index$1.h("span", null, pending)), index$1.h("td", null, index$1.h("span", null, this.t("suspended: ")), index$1.h("span", null, suspended))));
  }
  get element() { return index$1.getElement(this); }
};
IntergiroCustomerFooter.style = styleCss$1;

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

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

const styleCss = ".sc-ig-customers-table-h{display:block;width:100%;max-width:90em;margin:auto;border:1px solid rgb(var(--smoothly-medium-color))}[hidden].sc-ig-customers-table-h{display:none}.sc-ig-customers-table-h>p.sc-ig-customers-table{padding-left:.5em}.sc-ig-customers-table-h>table.sc-ig-customers-table{width:100%}.sc-ig-customers-table-h>table.sc-ig-customers-table>thead.sc-ig-customers-table>tr.sc-ig-customers-table{display:table-row;font-weight:bold;height:2em}.sc-ig-customers-table-h>table.sc-ig-customers-table>thead.sc-ig-customers-table>tr.sc-ig-customers-table>*.sc-ig-customers-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-customers-table-h>table.sc-ig-customers-table>thead.sc-ig-customers-table>tr.sc-ig-customers-table>*.sc-ig-customers-table>smoothly-icon.sc-ig-customers-table{vertical-align:-50%;margin-left:.1em}.sc-ig-customers-table-h>table.sc-ig-customers-table>td.sc-ig-customers-table{text-align:center}.sc-ig-customers-table-h>table.sc-ig-customers-table>td.sc-ig-customers-table>button.sc-ig-customers-table>smoothly-icon.sc-ig-customers-table{cursor:pointer}";

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

exports.ig_customer_actions = IntergiroCustomerActions;
exports.ig_customer_create_popup = IntergiroCustomerCreatePopup;
exports.ig_customers_footer = IntergiroCustomerFooter;
exports.ig_customers_table = IntergiroCustomersTable;
