'use strict';

const index$1 = require('./index-140bcbcd.js');
const App = require('./App-0b3e3f31.js');
require('./Token-8c5ba5d9.js');
const Item = require('./Item-00ce8c24.js');
const index$3 = require('./index-76391849.js');
require('./Credentials-8e4749ab.js');
const index = require('./index-d3079b93.js');
const index$2 = require('./index-188cc15d.js');
const Handler = require('./Handler-61133692.js');
const index$4 = require('./index-863a0811.js');
require('./ok-97a5fd0b.js');
require('./index-c910887e.js');
require('./index-0b2930ca.js');
require('./index-f23d1d6b.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 index.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) {
    index$1.registerInstance(this, hostRef);
    this.settlements = [];
    this.filteredSettlements = [];
  }
  async componentWillLoad() {
    var _a;
    this.t = create(this.element);
    IntergiroMonitor.client = await index$2.Client.open(Item.Connection.url || this.server || "", (_a = Item.Connection.key) !== null && _a !== void 0 ? _a : "", "authorization", "log", "me", "merchant", "settlement");
    this.handleOnUserChanged();
    Handler.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 && index$3.Merchant.is(user.merchant)) {
      const key = await index$3.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) && (Item.Connection.url = key.card.url);
    }
    else
      Item.Connection.url = this.server;
    this.loadData();
  }
  loadData() {
    var _a, _b;
    Handler.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 (index$1.h("smoothly-notifier", null, index$1.h("ig-monitor-login", null, index$1.h(App.App, { label: "Intergiro Monitor" }, this.active && this.active.audience == "agent" && (index$1.h("span", { slot: "header" }, index$1.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 && index$1.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 && [
      index$1.h("smoothly-room", { path: "", to: "merchant" }),
      index$1.h("smoothly-room", { path: "merchant", label: this.t(this.active.audience == "private" ? "Merchant" : "Merchants") }, index$1.h("ig-merchants", { merchants: this.filteredMerchants, audience: this.active.audience })),
      index$1.h("smoothly-room", { path: "authorization", label: this.t("Authorizations") }, index$1.h("ig-authorizations", { authorizations: this.filteredAuthorizations, conversion: this.conversion, merchants: this.filteredMerchants, "cost-plus": costPlus })),
      this.active.developer && (index$1.h("smoothly-room", { path: "log", label: this.t("Log") }, index$1.h("ig-logs", { logs: this.logs, merchants: this.filteredMerchants }))),
      index$1.h("smoothly-room", { path: "settlement", label: this.t("Settlements") }, index$1.h("ig-monitor-settlements", { authorizations: (_h = (_g = this.filteredAuthorizations) === null || _g === void 0 ? void 0 : _g.filter(index$4.Authorization.is).map(index$4.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 index$1.getElement(this); }
};
IntergiroMonitor.style = styleCss;

exports.IntergiroMonitor = IntergiroMonitor;
exports.create = create;
