import { Component, Element, Event, h, State } from "@stencil/core";
import { Notice } from "smoothly";
import { User } from "@payfunc/model";
import { Merchant } from "@payfunc/model";
import { Key } from "@payfunc/model";
import { Connection } from "@payfunc/model-base";
import { IntergiroMonitor } from "..";
import * as translation from "../translation";
export class MonitorLogin {
  componentWillLoad() {
    var _a, _b;
    this.t = translation.create(this.element);
    this.done = !!((_a = IntergiroMonitor.client) === null || _a === void 0 ? void 0 : _a.me.user);
    if (!Connection.key)
      (_b = IntergiroMonitor.client) === null || _b === void 0 ? void 0 : _b.me.logout;
  }
  async attemptLogin(e) {
    var _a, _b, _c, _d, _e;
    if (IntergiroMonitor.client) {
      const user = await ((_a = IntergiroMonitor.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 : ""));
      const key = User.is(user) && Merchant.is(user.merchant) && (await Key.unpack(user.merchant.key.private, "agent", "private"));
      if (User.is(user) && Key.is(key) && (key.aud == "agent" || ((_e = (_d = key.card) === null || _d === void 0 ? void 0 : _d.mid) === null || _e === void 0 ? void 0 : _e.startsWith("8103")))) {
        this.userChanged.emit({ user: user });
        this.done = true;
      }
      else
        this.notice.emit(Notice.failed(this.t("Login failed. Make sure email and password are correct.")));
    }
  }
  render() {
    const t = this.t;
    return [
      h("smoothly-dialog", { open: !this.done, header: "Intergiro Monitor" },
        h("form", null,
          h("smoothly-input", { 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),
    ];
  }
  static get is() { return "ig-monitor-login"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get states() { return {
    "t": {},
    "done": {}
  }; }
  static get events() { return [{
      "method": "notice",
      "name": "notice",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "Notice",
        "resolved": "Notice",
        "references": {
          "Notice": {
            "location": "import",
            "path": "smoothly"
          }
        }
      }
    }, {
      "method": "userChanged",
      "name": "userChanged",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "{ user: User }",
        "resolved": "{ user: User; }",
        "references": {
          "User": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      }
    }]; }
  static get elementRef() { return "element"; }
}
