import { r as registerInstance, h, g as getElement } from './index-08b73e9e.js';
import { A as App } from './App-364e27cc.js';
import './Token-ba2c5d5c.js';
import { a as Connection } from './Item-f4cbd6f4.js';
import { M as Merchant, K as Key } from './index-3299c1c0.js';
import './Credentials-b84c0c5f.js';
import { d as dist } from './index-a0a71287.js';
import { C as Client } from './index-26d778c4.js';
import { H as Handler } from './Handler-40406565.js';
import { A as Authorization } from './index-2d6441ea.js';
import './ok-c9833bf3.js';
import './index-c2c33860.js';
import './index-8e333ab5.js';
import './index-f77ff597.js';

const sv = {
  Merchants: "Handlare",
  Merchant: "Handlare",
  Authorizations: "Auktorisationer",
  "Login failed. Make sure email and password are correct.": "Inloggning misslyckades. Kontrollera e-post och lösenord.",
  Email: "E-post",
  "Select Merchants": "Välj Handlare",
  Password: "Lösenord",
  "Log in": "Logga in",
  Log: "Logg",
  Settlements: "Utbetalningar",
  Admin: "Administratör",
  "Developer Admin": "Utveckling Administratör",
};

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

const styleCss = ".sc-intergiro-monitor-h{display:block}[hidden].sc-intergiro-monitor-h{display:none}.sc-intergiro-monitor-h>smoothly-notifier.sc-intergiro-monitor>ig-monitor-login.sc-intergiro-monitor header.sc-intergiro-monitor{z-index:3}@media screen and (max-width: 499px){.sc-intergiro-monitor-h>smoothly-notifier.sc-intergiro-monitor>ig-monitor-login.sc-intergiro-monitor header.sc-intergiro-monitor span.sc-intergiro-monitor{display:none}}";

const IntergiroMonitor = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.settlements = [];
    this.filteredSettlements = [];
  }
  async componentWillLoad() {
    var _a;
    this.t = create(this.element);
    IntergiroMonitor.client = await Client.open(Connection.url || this.server || "", (_a = Connection.key) !== null && _a !== void 0 ? _a : "", "authorization", "log", "me", "merchant", "settlement");
    this.handleOnUserChanged();
    Handler.timer = undefined;
  }
  async handleOnUserChanged(e) {
    var _a, _b, _c, _d;
    const user = (_a = e === null || e === void 0 ? void 0 : e.detail.user) !== null && _a !== void 0 ? _a : (_b = IntergiroMonitor.client) === null || _b === void 0 ? void 0 : _b.me.user;
    if (user && Merchant.is(user.merchant)) {
      const key = await Key.unpack(user.merchant.key.private, "agent", "private");
      let audience = key === null || key === void 0 ? void 0 : key.aud;
      audience = !Array.isArray(audience) ? audience : audience.some(aud => aud == "agent") ? "agent" : "private";
      this.active =
        audience == "agent" || audience == "private"
          ? {
            id: user.merchant.id,
            audience,
            name: user.merchant.name,
            developer: !!user.option.developer,
            type: (_c = user.option.type) !== null && _c !== void 0 ? _c : key === null || key === void 0 ? void 0 : key.type,
          }
          : undefined;
      ((_d = key === null || key === void 0 ? void 0 : key.card) === null || _d === void 0 ? void 0 : _d.url) && (Connection.url = key.card.url);
    }
    else
      Connection.url = this.server;
    this.loadData();
  }
  loadData() {
    var _a, _b;
    Handler.register((authorizations, logs, merchants, settlements, conversion) => {
      var _a, _b, _c, _d;
      this.logs = logs;
      this.merchants =
        ((_a = this.active) === null || _a === void 0 ? void 0 : _a.developer) || !((_b = this.active) === null || _b === void 0 ? void 0 : _b.type)
          ? merchants
          : ((_c = this.active) === null || _c === void 0 ? void 0 : _c.type) == "test"
            ? merchants === null || merchants === void 0 ? void 0 : merchants.filter(m => m.type == "test")
            : merchants === null || merchants === void 0 ? void 0 : merchants.filter(m => m.type == "live");
      this.filteredMerchants = this.merchants;
      this.authorizations = ((_d = this.active) === null || _d === void 0 ? void 0 : _d.developer)
        ? authorizations
        : authorizations === null || authorizations === void 0 ? void 0 : authorizations.filter(a => { var _a; return (_a = this.merchants) === null || _a === void 0 ? void 0 : _a.find(m => m.id == a.merchant.id); });
      this.filteredAuthorizations = this.authorizations;
      this.settlements = settlements;
      this.filteredSettlements = this.settlements;
      this.conversion = conversion;
    }, ((_a = this.active) === null || _a === void 0 ? void 0 : _a.developer) ? "developer" : (_b = this.active) === null || _b === void 0 ? void 0 : _b.type, IntergiroMonitor.client);
  }
  handleTrigger(event) {
    var _a;
    if (event.detail.name == "logout") {
      event.stopPropagation();
      event.preventDefault();
      this.active = undefined;
      (_a = IntergiroMonitor.client) === null || _a === void 0 ? void 0 : _a.me.logout();
      location.reload();
    }
  }
  handleMenuClose(event) {
    var _a, _b, _c;
    this.filteredMerchants =
      event.detail.length >= 1 ? (_a = this.merchants) === null || _a === void 0 ? void 0 : _a.filter(m => event.detail.find(d => d.value == m.id)) : this.merchants;
    this.filteredAuthorizations =
      event.detail.length >= 1
        ? (_b = this.authorizations) === null || _b === void 0 ? void 0 : _b.filter(e => event.detail.find(d => d.value == e.merchant.id))
        : this.authorizations;
    this.filteredSettlements =
      event.detail.length >= 1
        ? (_c = this.settlements) === null || _c === void 0 ? void 0 : _c.filter(l => event.detail.find(d => d.value == l.merchant))
        : this.settlements;
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j;
    const costPlus = ((_a = this.active) === null || _a === void 0 ? void 0 : _a.audience) == "agent" || ((_c = (_b = this.merchants) === null || _b === void 0 ? void 0 : _b.find(m => { var _a; return m.id == ((_a = this.active) === null || _a === void 0 ? void 0 : _a.id); })) === null || _c === void 0 ? void 0 : _c.reconciliation.costPlus);
    return (h("smoothly-notifier", null, h("ig-monitor-login", null, h(App, { label: "Intergiro Monitor" }, this.active && this.active.audience == "agent" && (h("span", { slot: "header" }, h("smoothly-picker", { label: this.t("Select Merchants"), "label-setting": "hide", "max-height": "58px", "max-menu-height": "50vh", multiple: true, style: { width: "500px" }, options: (_d = this.merchants) === null || _d === void 0 ? void 0 : _d.map(e => e && { name: e.name, value: e.id }) }))), this.active && h("ig-user-menu", { slot: "nav-end", "user-name": (_f = (_e = this.active) === null || _e === void 0 ? void 0 : _e.name) !== null && _f !== void 0 ? _f : "" }), ",", this.active && [
      h("smoothly-room", { path: "", to: "merchant" }),
      h("smoothly-room", { path: "merchant", label: this.t(this.active.audience == "private" ? "Merchant" : "Merchants") }, h("ig-merchants", { merchants: this.filteredMerchants, audience: this.active.audience })),
      h("smoothly-room", { path: "authorization", label: this.t("Authorizations") }, h("ig-authorizations", { authorizations: this.filteredAuthorizations, conversion: this.conversion, merchants: this.filteredMerchants, "cost-plus": costPlus })),
      this.active.developer && (h("smoothly-room", { path: "log", label: this.t("Log") }, h("ig-logs", { logs: this.logs, merchants: this.filteredMerchants }))),
      h("smoothly-room", { path: "settlement", label: this.t("Settlements") }, h("ig-monitor-settlements", { authorizations: (_h = (_g = this.filteredAuthorizations) === null || _g === void 0 ? void 0 : _g.filter(Authorization.is).map(Authorization.to)) !== null && _h !== void 0 ? _h : [], "cost-plus": costPlus, settlements: this.filteredSettlements, conversion: this.conversion, view: this.active.audience == "private" || ((_j = this.filteredMerchants) === null || _j === void 0 ? void 0 : _j.length) == 1 ? "single" : "multiple" })),
    ]))));
  }
  get element() { return getElement(this); }
};
IntergiroMonitor.style = styleCss;

export { IntergiroMonitor as I, create as c };
