import { r as registerInstance, c as createEvent, h, g as getElement } from './index-08b73e9e.js';
import { d as dist } from './index-f77ff597.js';
import './Item-f4cbd6f4.js';
import { c as Customer, S as Settlement, U as User } from './index-945f930d.js';
import './Credentials-b84c0c5f.js';
import './Token-ba2c5d5c.js';
import { d as dist$1 } from './index-a0a71287.js';
import './App-364e27cc.js';
import { C as ClientIdentifier } from './ClientIdentifier-4a71c283.js';
import { s as sort } from './sort-0b97b2f1.js';
import { E as Error } from './unknown-97022c22.js';
import { N as Notice } from './Notice-668f9bc7.js';
import { P as PayfuncPortal } from './index-fd4c17e7.js';
import './_commonjsHelpers-9943807e.js';
import './index-1f953d81.js';
import './TextDecoder-357ac2ce.js';
import './index-3cf720d3.js';
import './index-ff7475d6.js';

const styleCss$3 = ".sc-ig-customers-h{display:block;width:100%;max-width:90em;margin:auto}ig-customer-create-popup.sc-ig-customers{bottom:3.5em;margin-right:2.5em}";

const IntergiroCustomers = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.updateCustomers = createEvent(this, "updateCustomers", 7);
    this.selected = {};
  }
  componentWillLoad() {
    if (!this.customersFiltered)
      this.filterCustomers();
  }
  handleFilterChanged(event) {
    this.filter = event.detail;
    event.stopPropagation();
  }
  filterCustomers() {
    const result = this.customers && (this.filter ? dist.filter(this.filter, this.customers) : [...this.customers]);
    this.customersFiltered = result;
  }
  handleSelectedChanged(event) {
    this.selected = event.detail;
  }
  onCreate(e) {
    this.updateCustomers.emit(e.detail);
  }
  render() {
    return [
      h("ig-filter", { template: Customer.template, filter: this.filter }),
      h("ig-customer-actions", { whitelist: this.whitelist, selected: this.selected }),
      h("ig-customers-table", { client: this.client, customers: this.customersFiltered, orders: this.orders, selected: this.selected, whitelist: this.whitelist }),
      h("ig-customer-create-popup", { hidden: true, onCreate: (e) => this.onCreate(e) }),
      h("ig-customers-footer", { customers: this.customersFiltered }),
    ];
  }
  static get watchers() { return {
    "customers": ["filterCustomers"],
    "filter": ["filterCustomers"]
  }; }
};
IntergiroCustomers.style = styleCss$3;

const styleCss$2 = "";

const IntergiroPortalSettlements = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.selected = {};
  }
  componentWillLoad() {
    this.settlementsChanged();
  }
  settlementsChanged() {
    var _a;
    if (!this.settlementsFiltered)
      this.settlementsFiltered = (_a = this.settlements) === null || _a === void 0 ? void 0 : _a.sort((a, b) => (a.period.start < b.period.start ? 1 : -1));
  }
  handleFilterChanged(event) {
    this.settlementsFiltered = event.detail;
    event.stopPropagation();
  }
  handleSelectedChanged(event) {
    this.selected = event.detail;
  }
  render() {
    return [
      h("ig-filter", { value: this.settlements, template: Settlement.template }),
      h("ig-portal-settlement-table", { settlements: this.settlementsFiltered, interchange: this.interchange }),
      h("ig-portal-settlement-footer", { color: "light", selected: this.selected, interchange: this.interchange, settlements: this.settlementsFiltered }),
    ];
  }
  static get watchers() { return {
    "settlements": ["settlementsChanged"]
  }; }
};
IntergiroPortalSettlements.style = styleCss$2;

const styleCss$1 = ".sc-portal-defer-view-h{display:block;width:100%;max-width:90em;margin:auto}[hidden].sc-portal-defer-view-h{display:none}";

const PortalDeferView = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.filter = {
      client: dist.is(ClientIdentifier.value),
      status: dist.some(dist.or("created", "ordered", "denied", "pending")),
    };
    this.deferFilter = {
      client: dist.is(ClientIdentifier.value),
      status: dist.some(dist.or("deferred")),
    };
  }
  componentWillLoad() {
    const result = this.orders && dist.filter(dist.and(dist.parse("deferred")).generalize(), this.orders);
    this.filteredOrders = result && sort(result, "created");
  }
  render() {
    return [
      h("portal-create-order", { orders: this.orders, emailOption: this.emailOption, currency: this.currency }),
      h("portal-display-defer-list", { orders: this.orders, filter: this.deferFilter }),
      h("ig-orders", { orders: this.filteredOrders }),
    ];
  }
};
PortalDeferView.style = styleCss$1;

const sv = {
  "Login failed. Make sure email and password are correct.": "Inloggning misslyckades. Kontrollera e-post och lösenord.",
  Email: "E-post",
  Password: "Lösenord",
  "Log in": "Logga in",
};

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

const styleCss = ".sc-portal-login-h{display:block}[hidden].sc-portal-login-h{display:none}.sc-portal-login-h>smoothly-dialog.sc-portal-login{top:60px;z-index:2}*.sc-portal-login{z-index:0}header.sc-portal-login{background-color:rgb(var(--smoothly-default-shade))}form.sc-portal-login{position:relative}smoothly-input.sc-portal-login{border:1px solid rgb(var(--smoothly-medium-color))}smoothly-submit.sc-portal-login{margin-top:0.4em}";

const PortalLogin = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.notice = createEvent(this, "notice", 7);
    this.userChanged = createEvent(this, "userChanged", 7);
  }
  componentWillLoad() {
    var _a;
    this.t = create(this.element);
    this.done = !!((_a = PayfuncPortal.client) === null || _a === void 0 ? void 0 : _a.me.user);
  }
  async attemptLogin(e) {
    if (PayfuncPortal.client)
      PayfuncPortal.client.me.onUnauthorized = async () => {
        var _a, _b, _c;
        const result = await ((_a = PayfuncPortal.client) === null || _a === void 0 ? void 0 : _a.me.login((_b = e.detail.user) !== null && _b !== void 0 ? _b : "", (_c = e.detail.password) !== null && _c !== void 0 ? _c : ""));
        if (User.is(result)) {
          this.userChanged.emit({ user: result });
          this.done = true;
        }
        else
          this.notice.emit(Notice.failed(this.t("Login failed. Make sure email and password are correct.")));
        return !Error.is(result);
      };
  }
  render() {
    const t = this.t;
    return [
      h("smoothly-dialog", { open: this.done != true, header: "PayFunc Portal" }, h("form", null, h("smoothly-input", { style: { borderBottom: "none" }, type: "email", name: "user" }, t("Email")), h("smoothly-input", { type: "password", name: "password" }, t("Password")), h("smoothly-submit", { color: "success", prevent: true, expand: "block", onSubmit: (e) => this.attemptLogin(e) }, t("Log in")))),
      h("slot", null),
    ];
  }
  get element() { return getElement(this); }
};
PortalLogin.style = styleCss;

export { IntergiroCustomers as ig_customers, IntergiroPortalSettlements as ig_portal_settlements, PortalDeferView as portal_defer_view, PortalLogin as portal_login };
