import { Component, Element, Event, h, Prop, State, Watch } from "@stencil/core";
import { DateTime } from "isoly";
import { getWarningMessage } from "../../../model";
import * as translation from "../../translation";
const resources = [
  (l, m, t, s) => l.match(/authorization\/?$/g) && m == "POST"
    ? `${t("authorization") + (s ? " " + s.amount + " " + s.currency : "")}`
    : undefined,
  (l, m, t, s) => l.match(/refund\/?$/g) && m == "POST" ? `${t("refund") + (s ? " " + s.amount + " " + s.currency : "")}` : undefined,
  (l, m, t, s) => l.match(/capture\/?$/g) && m == "POST"
    ? `${t("capture") + (s ? " " + s.amount + " " + s.currency : "")}`
    : undefined,
  (l, m, t) => (l.match(/authorization/g) && m == "DELETE" ? t("cancelled") : undefined),
  (l, m, t) => (l.match(/ch3d1.*check\/?$/g) && m == "POST" ? t("ch3d1 check") : undefined),
  (l, m, t) => (l.match(/ch3d1.*enrolled\/?$/g) && m == "POST" ? t("ch3d1 enrolled") : undefined),
  (l, m, t) => (l.match(/authorization\/merchant\/?$/g) && m == "POST" ? t("create merchant") : undefined),
  (l, m, t) => (l.match(/rule\/?$/g) && (m == "PUT" || m == "PATCH") ? t("change rule") : undefined),
  (l, m, t) => l.match(/authorization\/merchant\/?/g) && (m == "PUT" || m == "PATCH") ? t("change merchant") : undefined,
  (l, m, t) => (l.match(/card\/?$/g) && m == "POST" ? t("create card") : undefined),
  (l, m, t) => (l.match(/card\/secret\/?$/g) && m == "DELETE" ? t("regenerate secrets") : undefined),
  (l, m, t) => (l.match(/settlement\/?/g) && m == "POST" ? t("manually create settlements") : undefined),
  (l, m, t) => (l.match("Settlement") && m == "SCHEDULE" ? t("scheduled generation of settlements") : undefined),
  (l, m, t) => (l.match(/preauth\/?/g) && m == "POST" ? t("3d2 preauthorization step") : undefined),
  (l, m, t) => (l.match(/postauth\/?/g) && m == "POST" ? t("3d2 postauthorization step") : undefined),
  (l, m, t) => (l.match(/auth\/?/g) && m == "POST" ? t("3d2 authorization step") : undefined),
  (l, m, t) => l.match("Currency Conversion") && m == "SCHEDULE"
    ? t("scheduled updating of currency conversion rates")
    : undefined,
];
export class IntergiroLogTableRow {
  constructor() {
    this.opened = "";
    this.abbreviated = false;
    this.hideMerchant = false;
  }
  componentWillLoad() {
    this.t = translation.create(this.element);
    this.getResource();
  }
  handleColumnClick(column) {
    this.opened = column;
    this.columnClicked.emit({ index: this.index, column });
  }
  getResource() {
    var _a, _b, _c;
    const t = this.t;
    const search = (_b = (_a = this.log.entries.find(e => e.point == "PreAuthorization State")) === null || _a === void 0 ? void 0 : _a.data.state) !== null && _b !== void 0 ? _b : (_c = this.log.entries.find(e => e.point == "PostAuthorization State")) === null || _c === void 0 ? void 0 : _c.data.state;
    const level = this.log.entries[this.log.entries.length - 1].level;
    let resource;
    for (const check of resources)
      if ((resource = check(this.log.resource.location, this.log.resource.method, this.t, search === null || search === void 0 ? void 0 : search.authorization)))
        break;
    this.resource = resource
      ? resource + t(level == "warning" || level == "error" || level == "exception" ? " failed" : "")
      : this.log.resource.method + " " + this.log.resource.location;
  }
  render() {
    const t = this.t;
    const entry = this.log.entries[this.log.entries.length - 1];
    return [
      !(this.abbreviated || this.hideMerchant) && [
        h("td", null,
          h("section", { class: "openable" + (this.opened == "merchant" ? " opened" : ""), onClick: () => this.handleColumnClick("merchant") }, t(this.log.merchant))),
      ],
      !this.abbreviated && [
        h("td", null,
          h("section", { class: this.log.reference ? "openable" + (this.opened == "reference" ? " opened" : "") : "", onClick: () => this.log.reference && this.handleColumnClick("reference") }, this.log.reference ? t(this.log.reference.type) : t("none"))),
      ],
      h("td", null,
        h("section", { class: "openable" + (this.opened == "resource" ? " opened" : ""), onClick: () => this.handleColumnClick("resource") }, this.resource)),
      h("td", null,
        h("section", { class: "openable" + (this.opened == "entries" ? " opened" : ""), onClick: () => this.handleColumnClick("entries") }, t(entry.level == "warning" ? getWarningMessage(entry) : entry.level))),
      h("td", null,
        h("smoothly-display-date-time", { datetime: DateTime.localize(this.log.created) })),
    ];
  }
  static get is() { return "ig-log-table-row"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "log": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Log",
        "resolved": "Log",
        "references": {
          "Log": {
            "location": "import",
            "path": "@payfunc/model-log"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "index": {
      "type": "number",
      "mutable": false,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "index",
      "reflect": false
    },
    "opened": {
      "type": "string",
      "mutable": true,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "opened",
      "reflect": false,
      "defaultValue": "\"\""
    },
    "abbreviated": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "abbreviated",
      "reflect": false,
      "defaultValue": "false"
    },
    "hideMerchant": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "hide-merchant",
      "reflect": false,
      "defaultValue": "false"
    }
  }; }
  static get states() { return {
    "t": {},
    "resource": {}
  }; }
  static get events() { return [{
      "method": "columnClicked",
      "name": "columnClicked",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }]; }
  static get elementRef() { return "element"; }
  static get watchers() { return [{
      "propName": "log",
      "methodName": "getResource"
    }]; }
}
