import { Component, Element, Event, h, Prop, State } from "@stencil/core";
import { Error } from "gracely";
import { Notice } from "smoothly";
import { Connection } from "@payfunc/model-base";
import { Handler } from "../Handler";
import * as translation from "./translation";
export class CustomerError {
  componentWillLoad() {
    this.t = translation.create(this.element);
  }
  async sendLink() {
    var _a, _b, _c, _d;
    Connection.url = (_b = (_a = this.unpackedKey) === null || _a === void 0 ? void 0 : _a.iss) !== null && _b !== void 0 ? _b : "";
    Connection.key = (_d = (_c = this.unpackedKey) === null || _c === void 0 ? void 0 : _c.token) !== null && _d !== void 0 ? _d : "";
    this.notice.emit(Notice.execute(this.t("Sending email..."), async () => {
      var _a, _b;
      let result;
      const response = await Handler.client.customer.createLink((_b = (_a = this.unpackedKey) === null || _a === void 0 ? void 0 : _a.customer) !== null && _b !== void 0 ? _b : "");
      if (response && !Error.is(response))
        result = [true, this.t("Email was sent.")];
      else
        result = [false, this.t("Failed to send email.")];
      return result;
    }));
  }
  render() {
    return [
      h("smoothly-display", null, this.t(this.component
        ? "Your authorization is invalid or expired. Try refreshing the page or log out and then log in again. If that doesn't work, contact customer support."
        : "Your authorization is invalid or expired. Please check your email for a new link or press the button to send a new email.")),
      !this.component && (h("smoothly-submit", { color: "success", fill: "solid", onSubmit: () => this.sendLink() }, this.t("Send email"))),
    ];
  }
  static get is() { return "customer-error"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "unpackedKey": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Key | undefined",
        "resolved": "Key | undefined",
        "references": {
          "Key": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "component": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "\"app\" | \"order\" | \"subscription\" | \"balance\" | \"address\" | \"method\"",
        "resolved": "\"address\" | \"app\" | \"balance\" | \"method\" | \"order\" | \"subscription\" | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "component",
      "reflect": false
    }
  }; }
  static get states() { return {
    "t": {}
  }; }
  static get events() { return [{
      "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"; }
}
