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 { Connection, Contact, Customer, Item } from "@payfunc/model";
import * as translation from "./translation";
export class IntergiroCustomerTableRow {
  constructor() {
    this.opened = "";
  }
  componentWillLoad() {
    this.t = translation.create(this.element);
  }
  handleColumnClick(column) {
    var _a;
    this.opened = column;
    this.columnClicked.emit({ id: (_a = this.customer) === null || _a === void 0 ? void 0 : _a.id, column });
  }
  handleSelected(e) {
    this.customerSelected.emit({ customer: this.customer, checked: !this.selected });
    e.stopPropagation();
    e.preventDefault();
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g;
    return Customer.is(this.customer)
      ? [
        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.customer.number)),
        h("td", null,
          h("div", { class: "openable" + (this.opened == "contact" ? " opened" : ""), onClick: () => this.handleColumnClick("contact") }, (_a = Contact.getLabel(this.customer.contact)) !== 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.customer.subscription || this.customer.subscription.length < 1
            ? this.t("none")
            : this.customer.subscription.length > 1
              ? this.customer.subscription.length + " " + this.t("active")
              : this.t(typeof this.customer.subscription[0].schedule == "object"
                ? this.customer.subscription[0].schedule.frequency
                : this.customer.subscription[0].schedule) +
                " " +
                Item.amount(this.customer.subscription[0].items) +
                " " +
                this.customer.subscription[0].currency))) : ([]),
        h("td", null,
          h("div", { class: "openable" + (this.opened == "method" ? " opened" : ""), onClick: () => this.handleColumnClick("method") }, this.customer.method[0]
            ? tidily.format(this.customer.method[0].iin + "******" + this.customer.method[0].last4, "card-number")
            : this.t("none"))),
        this.whitelist ? (h("td", null,
          h("div", { class: "openable" + (this.opened == "balance" ? " opened" : ""), style: { color: this.customer.total < 0 ? "red" : "" }, onClick: () => this.handleColumnClick("balance") }, tidily.format(this.customer.total, "price", this.customer.currency)))) : ([]),
        h("td", null,
          h("smoothly-trigger", { disabled: !((_d = this.customer.contact) === null || _d === void 0 ? void 0 : _d.email) },
            h("smoothly-icon", { onClick: ((_e = this.customer.contact) === null || _e === void 0 ? void 0 : _e.email) ? () => this.sendLogin() : undefined, class: "sendLogin", name: "mail", size: "small", toolTip: !((_f = this.customer.contact) === 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("ig-status", { status: (_g = this.customer.status) !== null && _g !== void 0 ? _g : Customer.Status.getStatus(this.customer, 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(`customer/${(_a = this.customer) === null || _a === void 0 ? void 0 : _a.id}/link?email=true`, {});
      return !gracely.Error.is(response)
        ? [true, this.t("Successfully sent email")]
        : [false, this.t("Failed to send email")];
    }));
  }
  static get is() { return "ig-customer-table-row"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "customer": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Customer",
        "resolved": "Customer | undefined",
        "references": {
          "Customer": {
            "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": "customerSelected",
      "name": "customerSelected",
      "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
    }]; }
}
