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 IntergiroCustomerCreate {
  componentWillLoad() {
    this.customerNumber = authly.Identifier.generate(12);
    this.t = translation.create(this.element);
  }
  async submitCustomer(event) {
    const customer = {
      number: event.detail.number,
      contact: {
        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 customer..."), async () => {
      let result;
      this.close.emit();
      const response = await Connection.post(`customer/`, customer);
      if (!gracely.Error.is(response)) {
        result = [true, this.t("Customer was created.")];
        const customers = await Connection.get(`customer/`);
        this.createCustomer.emit(customers);
      }
      else
        result = [false, this.t("Failed to create customer.")];
      return result;
    }));
  }
  render() {
    return [
      h("form", null,
        h("section", null,
          h("smoothly-input", { name: "number", type: "text", value: this.customerNumber }, this.t("Customer 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.submitCustomer(e) },
            h("smoothly-icon", { toolTip: this.t("Create customer"), name: "add-circle", fill: "clear", size: "medium", color: "success" })))),
    ];
  }
  static get is() { return "ig-customer-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": "createCustomer",
      "name": "createCustomer",
      "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"; }
}
