import { Component, Element, Event, h, Listen, Prop, State } from "@stencil/core";
import * as translation from "./translation";
export class PayfuncAccountsTable {
  constructor() {
    this.selected = {};
    this.count = 100;
    this.opened = {};
  }
  componentWillLoad() {
    this.t = translation.create(this.element);
  }
  onChecked(event) {
    this.selected = {};
    if (event.detail.selected && this.accounts)
      for (const account of this.accounts)
        this.selected[account.id] = account;
    this.selectedChanged.emit(this.selected);
    event.stopPropagation();
    event.preventDefault();
  }
  handleToggle(event) {
    const accounts = Object.assign({}, this.selected);
    if (event.detail.checked)
      accounts[event.detail.account.id] = event.detail.account;
    else
      delete accounts[event.detail.account.id];
    this.selectedChanged.emit((this.selected = accounts));
  }
  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;
  }
  render() {
    const t = this.t;
    let result;
    if (!this.accounts)
      result = h("p", null, t("Loading accounts from server."));
    else if (this.accounts.length <= 0)
      result = h("p", null, t("There are no accounts to display."));
    else {
      result = [
        h("table", null,
          h("thead", null,
            h("tr", null,
              h("th", { style: { width: "5%" }, color: "light" },
                h("smoothly-checkbox", { "select-all": true })),
              h("th", { color: "light" }, t("Account number")),
              h("th", { color: "light" }, t("Customer")),
              h("th", { color: "light" }, t("Orders")),
              this.whitelist ? h("th", { color: "light" }, t("Subscription")) : [],
              h("th", { color: "light" }, t("Method")),
              this.whitelist ? h("th", { color: "light" }, t("Balance")) : [],
              h("th", { color: "light" }, t("Login")),
              h("th", { color: "light" }, t("Status")))),
          this.accounts
            ? this.accounts
              .slice(0, this.count)
              .map(account => {
              var _a, _b;
              return [
                h("payfunc-account-table-row", { account: account, whitelist: this.whitelist, orders: this.orders ? this.orders[account.id] : undefined, opened: (_a = this.opened[account.id]) !== null && _a !== void 0 ? _a : "", selected: this.selected.hasOwnProperty.call(this.selected, account.id) }),
                h("payfunc-account-details", { opened: (_b = this.opened[account.id]) !== null && _b !== void 0 ? _b : "", orders: this.orders ? this.orders[account.id] : undefined, whitelist: this.whitelist, account: account }),
              ];
            })
            : null,
          h("td", { colSpan: 8 }, this.count < this.accounts.length ? (h("button", { onClick: () => (this.count += 100) },
            h("smoothly-icon", { name: "chevron-down-circle", size: "large", toolTip: this.t("Load more accounts") }))) : (""))),
      ];
    }
    return result;
  }
  static get is() { return "payfunc-accounts-table"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "accounts": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Account[]",
        "resolved": "Account[] | undefined",
        "references": {
          "Account": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "orders": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "{ [id: string]: Order[] | undefined }",
        "resolved": "undefined | { [id: string]: Order[] | undefined; }",
        "references": {
          "Order": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "whitelist": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "whitelist",
      "reflect": false
    },
    "selected": {
      "type": "unknown",
      "mutable": true,
      "complexType": {
        "original": "{ [id: string]: Account | undefined }",
        "resolved": "{ [id: string]: Account | undefined; }",
        "references": {
          "Account": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "defaultValue": "{}"
    }
  }; }
  static get states() { return {
    "count": {},
    "opened": {},
    "t": {}
  }; }
  static get events() { return [{
      "method": "selectedChanged",
      "name": "selectedChanged",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "{ [id: string]: Account | undefined }",
        "resolved": "{ [id: string]: Account | undefined; }",
        "references": {
          "Account": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      }
    }]; }
  static get elementRef() { return "element"; }
  static get listeners() { return [{
      "name": "checked",
      "method": "onChecked",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "accountSelected",
      "method": "handleToggle",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "columnClicked",
      "method": "toggleColumn",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
