import { Component, Element, Event, h, Listen, Prop, State } from "@stencil/core";
import { Error } from "gracely";
import { DateTime } from "isoly";
import * as tidily from "tidily";
import * as authly from "authly";
import { Notice } from "smoothly";
import { Connection, Contact, Item, Payment, Status } from "@payfunc/model";
import * as translation from "./translation";
export class IntergiroOrdersTableRow {
  constructor() {
    this.display = false;
  }
  async componentWillLoad() {
    this.t = translation.create(this.element);
    await this.getAudience();
  }
  onChecked(event) {
    this.orderSelected.emit({ order: this.order, checked: !this.selected });
    event.stopPropagation();
    event.preventDefault();
  }
  async getAudience() {
    var _a, _b, _c;
    if (Connection.key) {
      const unpacked = await ((_a = authly.Verifier.create()) === null || _a === void 0 ? void 0 : _a.verify(Connection.key));
      this.audience = unpacked
        ? typeof unpacked.aud == "string"
          ? unpacked.aud
          : (_c = (_b = unpacked.aud) === null || _b === void 0 ? void 0 : _b.reduce((p, c) => (p == "customer" ? p : c), "")) !== null && _c !== void 0 ? _c : ""
        : "";
    }
    else
      this.audience = "";
  }
  handleColumnClick(column) {
    this.columnClicked.emit({ id: this.order.id, column });
  }
  async sendReceiptEmail() {
    this.notice.emit(Notice.execute(this.t("Sending receipt..."), async () => {
      const response = await Connection.get(`order/${this.order.id}/receipt?email=true`);
      return !Error.is(response) ? [true, this.t("Successfully sent email")] : [false, this.t("Failed to send email")];
    }));
  }
  render() {
    var _a, _b, _c, _d;
    const t = this.t;
    const contact = Contact.is(this.order.customer) ? this.order.customer : undefined;
    return [
      !this.display ? (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.order.number)),
      !this.display ? (h("td", null,
        h("div", { class: "openable" + (this.opened == "contact" ? " opened" : ""), onClick: () => this.handleColumnClick("contact") }, (_a = Contact.getLabel(contact)) !== null && _a !== void 0 ? _a : (Payment.Defer.is(this.order.payment) ? this.order.payment.contact : t("unknown"))))) : (""),
      h("td", null,
        h("smoothly-display-amount", { class: "openable" + (this.opened == "items" ? " opened" : ""), onClick: () => this.handleColumnClick("items"), amount: Item.amount(this.order.items), currency: this.order.currency })),
      this.receipt ? (h("td", null, this.order.payment.type == "card" && [
        h("a", { href: Connection.baseUrl +
            "order/" +
            this.order.id +
            "/receipt?merchant=" +
            Connection.key +
            "&accept=application%2Fpdf", target: "_blank" },
          h("smoothly-icon", { class: "receipt", name: "receipt", size: "small", toolTip: t("Open receipt as pdf") })),
        this.audience != "customer" && (contact === null || contact === void 0 ? void 0 : contact.email) && (h("smoothly-icon", { class: "receipt", name: "mail", size: "small", toolTip: t("Send receipt as email to customer"), onClick: () => this.sendReceiptEmail() })),
      ])) : (""),
      h("td", null,
        h("div", { class: "openable" + (this.opened == "payment" ? " opened" : ""), onClick: () => this.handleColumnClick("payment") }, Payment.Card.is(this.order.payment)
          ? tidily.format(this.order.payment.iin + "******" + this.order.payment.last4, "card-number")
          : Payment.Defer.is(this.order.payment) && (contact === null || contact === void 0 ? void 0 : contact.email)
            ? this.order.payment.contact
            : Payment.Defer.is(this.order.payment) && this.order.payment.link
              ? this.order.payment.link.substring(0, 8) +
                "..." +
                this.order.payment.link.slice(this.order.payment.link.length - 16)
              : "")),
      h("td", null,
        h("smoothly-display-date-time", { class: "openable" + (this.opened == "created" ? " opened" : ""), onClick: () => this.handleColumnClick("created"), datetime: this.order.created ? DateTime.localize(DateTime.parse(this.order.created)) : "" })),
      h("td", null,
        h("div", null, this.order.status
          ? Array.isArray(this.order.status)
            ? (_c = (_b = this.order.status) === null || _b === void 0 ? void 0 : _b.map((status) => h("ig-status", { status: status }))) !== null && _c !== void 0 ? _c : []
            : (_d = Object.entries(this.order.status).map(status => Status.is(status[0]) && h("ig-status", { status: status[0] }))) !== null && _d !== void 0 ? _d : []
          : [])),
    ];
  }
  static get is() { return "ig-orders-table-row"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "selected": {
      "type": "boolean",
      "mutable": true,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "selected",
      "reflect": true
    },
    "order": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Order",
        "resolved": "Order",
        "references": {
          "Order": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "card": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Payment.Card",
        "resolved": "Card",
        "references": {
          "Payment": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "opened": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "opened",
      "reflect": false
    },
    "display": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "display",
      "reflect": false,
      "defaultValue": "false"
    },
    "receipt": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "receipt",
      "reflect": false
    }
  }; }
  static get states() { return {
    "t": {}
  }; }
  static get events() { return [{
      "method": "orderSelected",
      "name": "orderSelected",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "columnClicked",
      "name": "columnClicked",
      "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": "onChecked",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
