import { Component, Element, Event, h, Listen, Prop, State } from "@stencil/core";
import { Error } from "gracely";
import { Notice } from "smoothly";
import { Customer, Order, Payment } from "@payfunc/model";
import { Handler } from "../Handler";
import * as translation from "./translation";
export class CustomerBalance {
  componentWillLoad() {
    this.t = translation.create(this.element);
  }
  equalize() {
    this.notice.emit(Notice.execute(this.t("Equalizing balance..."), async () => {
      let result;
      const response = await Handler.client.customer.equalizeBalance("me");
      if (Error.is(response) || response.errors > 0)
        result = [false, this.t("Failed to equalize balance.")];
      else {
        await this.getCustomer();
        result = [true, this.t("Balance was successfully equalized.")];
      }
      return result;
    }));
  }
  onAddFunds(event) {
    this.notice.emit(Notice.execute(this.t("Topping up balance..."), async () => {
      let result;
      const response = await Handler.client.order.create(event.detail);
      if (!Error.is(response) && Order.is(response)) {
        this.orders = this.orders ? [response, ...this.orders] : [response];
        await this.getCustomer();
        result = [true, this.t("Balance was successfully topped up.")];
      }
      else
        result = [false, this.t("Failed to top up balance.")];
      return result;
    }));
  }
  async getCustomer() {
    const customer = await Handler.client.customer.get("me");
    if (Customer.is(customer))
      this.customer = customer;
  }
  render() {
    Payment.Creatable;
    const orders = this.orders
      ? this.orders.filter(o => o.payment.type == "customer" || o.payment.type == "card" ? o.payment.charge == "balance" : false)
      : undefined;
    return [
      h("ig-balance-add", { customer: this.customer }),
      h("ig-customer-balance", { customer: this.customer }),
      h("ig-orders", { display: true, orders: orders }),
    ];
  }
  static get is() { return "customer-balance"; }
  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": true,
      "complexType": {
        "original": "Customer",
        "resolved": "Customer",
        "references": {
          "Customer": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "orders": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Order[]",
        "resolved": "Order[] | undefined",
        "references": {
          "Order": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "component": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "\"app\" | \"balance\"",
        "resolved": "\"app\" | \"balance\" | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "component",
      "reflect": true
    }
  }; }
  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"; }
  static get listeners() { return [{
      "name": "trigger",
      "method": "equalize",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "addFunds",
      "method": "onAddFunds",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
