import { Component, Element, Event, h, Listen, Prop, State } from "@stencil/core";
import * as translation from "./translation";
export class IntergiroCustomersTable {
  constructor() {
    this.selected = {};
    this.count = 100;
    this.opened = {};
  }
  componentWillLoad() {
    this.t = translation.create(this.element);
  }
  onChecked(event) {
    this.selected = {};
    if (event.detail.selected && this.customers)
      for (const customer of this.customers)
        this.selected[customer.id] = customer;
    this.selectedChanged.emit(this.selected);
    event.stopPropagation();
    event.preventDefault();
  }
  handleToggle(event) {
    const customers = Object.assign({}, this.selected);
    if (event.detail.checked)
      customers[event.detail.customer.id] = event.detail.customer;
    else
      delete customers[event.detail.customer.id];
    this.selectedChanged.emit((this.selected = customers));
  }
  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.customers)
      result = h("p", null, t("Loading customers from server."));
    else if (this.customers.length <= 0)
      result = h("p", null, t("There are no customers 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("Customer number")),
              h("th", { color: "light" }, t("Contact")),
              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.customers
            ? this.customers
              .slice(0, this.count)
              .map(customer => {
              var _a, _b;
              return [
                h("ig-customer-table-row", { customer: customer, client: this.client, whitelist: this.whitelist, orders: this.orders ? this.orders[customer.id] : undefined, opened: (_a = this.opened[customer.id]) !== null && _a !== void 0 ? _a : "", selected: this.selected.hasOwnProperty.call(this.selected, customer.id) }),
                h("ig-customer-details", { client: this.client, opened: (_b = this.opened[customer.id]) !== null && _b !== void 0 ? _b : "", orders: this.orders ? this.orders[customer.id] : undefined, whitelist: this.whitelist, customer: customer }),
              ];
            })
            : null,
          h("td", { colSpan: 8 }, this.count < this.customers.length && (h("button", { onClick: () => (this.count += 100) },
            h("smoothly-icon", { name: "chevron-down-circle", size: "large", toolTip: this.t("Load more customers") }))))),
      ];
    }
    return result;
  }
  static get is() { return "ig-customers-table"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "customers": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Customer[]",
        "resolved": "Customer[] | undefined",
        "references": {
          "Customer": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "client": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Pick<Client, \"customer\">",
        "resolved": "{ customer: Customer; }",
        "references": {
          "Pick": {
            "location": "global"
          },
          "Client": {
            "location": "import",
            "path": "@payfunc/merchant-client"
          }
        }
      },
      "required": false,
      "optional": false,
      "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]: Customer | undefined }",
        "resolved": "{ [id: string]: Customer | undefined; }",
        "references": {
          "Customer": {
            "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]: Customer | undefined }",
        "resolved": "{ [id: string]: Customer | undefined; }",
        "references": {
          "Customer": {
            "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": "customerSelected",
      "method": "handleToggle",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "columnClicked",
      "method": "toggleColumn",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
