import { Component, Element, h, Listen, Prop, State } from "@stencil/core";
import { App } from "smoothly";
import { Key, Merchant as ModelMerchant } from "@payfunc/model";
import { Connection } from "@payfunc/model-base";
import { Client } from "@payfunc/merchant-client";
import { Handler } from "./Handler";
import { ModelState } from "./model";
import * as translation from "./translation";
export class IntergiroMonitor {
  constructor() {
    this.settlements = [];
    this.filteredSettlements = [];
  }
  async componentWillLoad() {
    var _a;
    this.t = translation.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 && ModelMerchant.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(ModelState.Authorization.is).map(ModelState.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" })),
          ]))));
  }
  static get is() { return "intergiro-monitor"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "server": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "server",
      "reflect": false
    }
  }; }
  static get states() { return {
    "t": {},
    "merchants": {},
    "filteredMerchants": {},
    "authorizations": {},
    "filteredAuthorizations": {},
    "logs": {},
    "settlements": {},
    "active": {},
    "filteredSettlements": {},
    "conversion": {}
  }; }
  static get elementRef() { return "element"; }
  static get listeners() { return [{
      "name": "userChanged",
      "method": "handleOnUserChanged",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "trigger",
      "method": "handleTrigger",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "menuClose",
      "method": "handleMenuClose",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
