import { Component, Element, Event, h, Listen, Prop, State } from "@stencil/core";
import * as translation from "../translation";
export class IntergiroMerchantTable {
  constructor() {
    this.selected = {};
    this.abbrivated = false;
    this.opened = {};
  }
  componentWillLoad() {
    this.t = translation.create(this.element);
  }
  handleMerchantSelected(e) {
    e.detail.checked
      ? (this.selected[e.detail.merchant.id] = e.detail.merchant)
      : delete this.selected[e.detail.merchant.id];
    this.selectedChanged.emit(this.selected);
  }
  handleChecked(e) {
    var _a;
    const selected = {};
    this.selectedChanged.emit(e.detail.selected ? (_a = this.merchants) === null || _a === void 0 ? void 0 : _a.map(m => (selected[m.id] = m)) : {});
    this.selected = selected;
  }
  toggleColumn(e) {
    const opened = Object.assign({}, this.opened);
    if (e.detail["id"] && opened[e.detail.id] == e.detail.column)
      delete opened[e.detail.id];
    else
      opened[e.detail.id] = e.detail.column;
    this.opened = opened;
    e.stopPropagation();
  }
  render() {
    return [
      !this.merchants ? (h("p", null, this.t("Loading information from server..."))) : this.merchants.length > 0 ? ([
        h("table", null,
          h("thead", null,
            h("tr", { color: "light" },
              !this.abbrivated && (h("th", null,
                h("smoothly-checkbox", { "select-all": true }))),
              h("th", null, this.t("number")),
              h("th", null, this.t("information")),
              h("th", null, this.t("settlement")),
              h("th", null, this.t("rules")))),
          this.merchants.map(m => {
            var _a;
            return [
              h("ig-merchant-table-row", { abbrivated: this.abbrivated, merchant: m, selected: !!this.selected[m.id], opened: this.opened[m.id] }),
              h("ig-merchant-details", { merchant: m, opened: (_a = this.opened[m.id]) !== null && _a !== void 0 ? _a : "" }),
            ];
          })),
      ]) : (h("p", null, this.t("There are no merchants to display."))),
    ];
  }
  static get is() { return "ig-merchant-table"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "merchants": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Merchant[]",
        "resolved": "Merchant[] | undefined",
        "references": {
          "Merchant": {
            "location": "import",
            "path": "@payfunc/model-acquirer"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "selected": {
      "type": "unknown",
      "mutable": true,
      "complexType": {
        "original": "{ [id: string]: Merchant | undefined }",
        "resolved": "{ [id: string]: Merchant | undefined; }",
        "references": {
          "Merchant": {
            "location": "import",
            "path": "@payfunc/model-acquirer"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "defaultValue": "{}"
    },
    "abbrivated": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "abbrivated",
      "reflect": false,
      "defaultValue": "false"
    }
  }; }
  static get states() { return {
    "t": {},
    "opened": {}
  }; }
  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": "merchantSelected",
      "method": "handleMerchantSelected",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "checked",
      "method": "handleChecked",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "columnClicked",
      "method": "toggleColumn",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
