import { Component, Element, Event, h, Listen, Prop, State } from "@stencil/core";
import * as gracely from "gracely";
import * as tidily from "tidily";
import { Notice } from "smoothly";
import { Account, Connection, Customer, Item } from "@payfunc/model";
import * as translation from "./translation";
export class PayfuncAccountTableRow {
  constructor() {
    this.opened = "";
  }
  componentWillLoad() {
    this.t = translation.create(this.element);
  }
  handleColumnClick(column) {
    var _a;
    this.opened = column;
    this.columnClicked.emit({ id: (_a = this.account) === null || _a === void 0 ? void 0 : _a.id, column });
  }
  handleSelected(e) {
    this.accountSelected.emit({ account: this.account, checked: !this.selected });
    e.stopPropagation();
    e.preventDefault();
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g;
    return Account.is(this.account)
      ? [
        h("td", null,
          h("smoothly-checkbox", { selected: this.selected })),
        h("td", null,
          h("div", { class: "openable" + (this.opened == "number" ? " opened" : ""), onClick: () => this.handleColumnClick("number") }, this.account.number)),
        h("td", null,
          h("div", { class: "openable" + (this.opened == "customer" ? " opened" : ""), onClick: () => this.handleColumnClick("customer") }, (_a = Customer.getLabel(this.account.customer)) !== null && _a !== void 0 ? _a : this.t("unknown"))),
        h("td", null,
          h("div", { class: "openable" + (this.opened == "order" ? " opened" : ""), onClick: () => this.handleColumnClick("order") }, this.t("$0 orders", (_c = (_b = this.orders) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0))),
        this.whitelist ? (h("td", null,
          h("div", { class: "openable" + (this.opened == "subscription" ? " opened" : ""), onClick: () => this.handleColumnClick("subscription") }, !this.account.subscription || this.account.subscription.length < 1
            ? this.t("none")
            : this.account.subscription.length > 1
              ? this.account.subscription.length + " " + this.t("active")
              : this.t(typeof this.account.subscription[0].schedule == "object"
                ? this.account.subscription[0].schedule.frequency
                : this.account.subscription[0].schedule) +
                " " +
                Item.amount(this.account.subscription[0].items) +
                " " +
                this.account.subscription[0].currency))) : ([]),
        h("td", null,
          h("div", { class: "openable" + (this.opened == "method" ? " opened" : ""), onClick: () => this.handleColumnClick("method") }, this.account.method[0]
            ? tidily.format(this.account.method[0].iin + "******" + this.account.method[0].last4, "card-number")
            : this.t("none"))),
        this.whitelist ? (h("td", null,
          h("div", { class: "openable" + (this.opened == "balance" ? " opened" : ""), style: { color: this.account.total < 0 ? "red" : "" }, onClick: () => this.handleColumnClick("balance") }, tidily.format(this.account.total, "price", this.account.currency)))) : ([]),
        h("td", null,
          h("smoothly-trigger", { disabled: !((_d = this.account.customer) === null || _d === void 0 ? void 0 : _d.email) },
            h("smoothly-icon", { onClick: ((_e = this.account.customer) === null || _e === void 0 ? void 0 : _e.email) ? () => this.sendLogin() : undefined, class: "sendLogin", name: "mail", size: "small", toolTip: !((_f = this.account.customer) === null || _f === void 0 ? void 0 : _f.email)
                ? this.t("The customer has no email set")
                : this.t("Send login link via email") }))),
        h("td", null,
          h("div", null,
            h("payfunc-status", { status: (_g = this.account.status) !== null && _g !== void 0 ? _g : Account.Status.getStatus(this.account, this.orders ? this.orders[0] : undefined) }))),
      ]
      : null;
  }
  async sendLogin() {
    this.notice.emit(Notice.execute(this.t("Sending email..."), async () => {
      var _a;
      const response = await Connection.post(`account/${(_a = this.account) === null || _a === void 0 ? void 0 : _a.id}/link?email=true`, {});
      let result;
      if (!gracely.Error.is(response))
        result = [true, this.t("Successfully sent email")];
      else
        result = [false, this.t("Failed to send email")];
      return result;
    }));
  }
  static get is() { return "payfunc-account-table-row"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "account": {
      "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": "Order[] | undefined",
        "resolved": "Order[] | undefined",
        "references": {
          "Order": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "selected": {
      "type": "boolean",
      "mutable": true,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "selected",
      "reflect": true
    },
    "opened": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "opened",
      "reflect": false,
      "defaultValue": "\"\""
    },
    "whitelist": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "whitelist",
      "reflect": false
    }
  }; }
  static get states() { return {
    "t": {}
  }; }
  static get events() { return [{
      "method": "columnClicked",
      "name": "columnClicked",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "accountSelected",
      "name": "accountSelected",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "notice",
      "name": "notice",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "Notice",
        "resolved": "Notice",
        "references": {
          "Notice": {
            "location": "import",
            "path": "smoothly"
          }
        }
      }
    }]; }
  static get elementRef() { return "element"; }
  static get listeners() { return [{
      "name": "checked",
      "method": "handleSelected",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
