import { Component, Element, Event, h, Listen, Prop, State } from "@stencil/core";
import { ModelState } from "../../model";
import * as translation from "../translation";
export class IntergiroAuthorizationTable {
  constructor() {
    this.abbreviated = false;
    this.selected = {};
    this.opened = {};
    this.count = 100;
  }
  componentWillLoad() {
    this.t = translation.create(this.element);
  }
  toggleColumn(e) {
    const opened = Object.assign({}, this.opened);
    opened[e.detail.index] == e.detail.column
      ? delete opened[e.detail.index]
      : (opened[e.detail.index] = e.detail.column);
    this.opened = opened;
    e.stopPropagation();
  }
  handleMerchantSelected(e) {
    e.detail.checked
      ? (this.selected[e.detail.authorization.authorization.id] = e.detail.authorization)
      : delete this.selected[e.detail.authorization.authorization.id];
    this.selectedChanged.emit(this.selected);
  }
  handleChecked(e) {
    var _a;
    const selected = {};
    this.selectedChanged.emit(e.detail.selected
      ? (_a = this.authorizations) === null || _a === void 0 ? void 0 : _a.map(a => ModelState.Authorization.is(a) && (selected[a.authorization.id] = a))
      : {});
    this.selected = selected;
  }
  render() {
    return [
      !this.authorizations ? (h("p", null, this.t("Loading authorizations from server..."))) : this.authorizations.length > 0 ? (h("table", null,
        h("thead", null,
          h("tr", null,
            !this.abbreviated && (h("th", { color: "light" },
              h("smoothly-checkbox", { "select-all": true }))),
            h("th", { color: "light" }, this.t("number")),
            !this.abbreviated && h("th", { color: "light" }, this.t("merchant")),
            h("th", { color: "light", class: "right" }, this.t("amount")),
            h("th", { color: "light" }, this.t("card")),
            h("th", { color: "light" }, this.t("history")),
            h("th", { color: "light", class: "smaller" }, this.t("flags")),
            h("th", { color: "light" }, this.t("status")))),
        this.authorizations.slice(0, this.count).map((a, index) => {
          const merchant = this.merchants.find(m => m.id == a.merchant.id);
          return [
            h("ig-authorization-table-row", { state: a, selected: ModelState.Authorization.is(a) && !!this.selected[a.authorization.id], merchant: merchant === null || merchant === void 0 ? void 0 : merchant.name, opened: this.opened[index], abbreviated: this.abbreviated, index: index }),
            h("ig-authorization-details", { state: a, merchant: merchant, abbreviated: this.abbreviated, "cost-plus": this.costPlus, opened: this.opened[index] }),
          ];
        }),
        this.count < this.authorizations.length && (h("td", { colSpan: this.abbreviated ? 5 : 6 },
          h("button", null,
            h("smoothly-icon", { onClick: () => (this.count += 100), name: "chevron-down-circle", size: "large", toolTip: this.t("Load more authorizations") })))))) : (h("p", null, this.t("There are no authorizations to display."))),
    ];
  }
  static get is() { return "ig-authorization-table"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "authorizations": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "(ModelState.Authorization | ModelState.FailedAuthorization)[]",
        "resolved": "(Authorization | FailedAuthorization)[] | undefined",
        "references": {
          "ModelState": {
            "location": "import",
            "path": "../../model"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "merchants": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Merchant[]",
        "resolved": "Merchant[]",
        "references": {
          "Merchant": {
            "location": "import",
            "path": "@payfunc/model-acquirer"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "costPlus": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "cost-plus",
      "reflect": false
    },
    "abbreviated": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "abbreviated",
      "reflect": false,
      "defaultValue": "false"
    },
    "selected": {
      "type": "unknown",
      "mutable": true,
      "complexType": {
        "original": "{ [id: string]: ModelState.Authorization | undefined }",
        "resolved": "{ [id: string]: Authorization | undefined; }",
        "references": {
          "ModelState": {
            "location": "import",
            "path": "../../model"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "defaultValue": "{}"
    }
  }; }
  static get states() { return {
    "opened": {},
    "t": {},
    "count": {}
  }; }
  static get events() { return [{
      "method": "selectedChanged",
      "name": "selectedChanged",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }]; }
  static get elementRef() { return "element"; }
  static get listeners() { return [{
      "name": "columnClicked",
      "method": "toggleColumn",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "authorizationSelected",
      "method": "handleMerchantSelected",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "checked",
      "method": "handleChecked",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
