import { Component, Element, Event, h, State } from "@stencil/core";
import * as gracely from "gracely";
import * as authly from "authly";
import { Notice } from "smoothly";
import { Connection } from "@payfunc/model";
import * as translation from "./translation";
export class PayfuncAccountCreate {
  componentWillLoad() {
    this.accountNumber = authly.Identifier.generate(12);
    this.t = translation.create(this.element);
  }
  async submitAccount(event) {
    const account = {
      number: event.detail.accountNumber,
      customer: {
        name: event.detail.name == "" ? undefined : event.detail.name,
        email: event.detail.email == "" ? undefined : event.detail.email,
      },
      method: [],
    };
    this.notice.emit(Notice.execute(this.t("Creating account..."), async () => {
      let result;
      this.close.emit();
      const response = await Connection.post(`account/`, account);
      if (!gracely.Error.is(response)) {
        result = [true, this.t("Account was created.")];
        const accounts = await Connection.get(`account/`);
        this.createAccount.emit(accounts);
      }
      else
        result = [false, this.t("Failed to create account.")];
      return result;
    }));
  }
  render() {
    return [
      h("form", null,
        h("section", null,
          h("smoothly-input", { name: "accountNumber", type: "text", value: this.accountNumber }, this.t("Account number")),
          h("smoothly-input", { name: "name", type: "text" }, this.t("Name"))),
        h("section", null,
          h("smoothly-input", { name: "email", type: "text" }, this.t("Email")),
          h("smoothly-submit", { fill: "clear", prevent: true, onSubmit: (e) => this.submitAccount(e) },
            h("smoothly-icon", { toolTip: this.t("Create account"), name: "add-circle", fill: "clear", size: "medium", color: "success" })))),
    ];
  }
  static get is() { return "payfunc-account-create"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get states() { return {
    "t": {}
  }; }
  static get events() { return [{
      "method": "createAccount",
      "name": "createAccount",
      "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"
          }
        }
      }
    }, {
      "method": "close",
      "name": "close",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }]; }
  static get elementRef() { return "element"; }
}
