import { Component, Element, Event, h, Listen, Prop, State } from "@stencil/core";
import { Error } from "gracely";
import { Notice } from "smoothly";
import { Connection } from "@payfunc/model";
import * as translation from "./translation";
export class PayfuncAccountDetails {
  constructor() {
    this.opened = "";
  }
  componentWillLoad() {
    this.t = translation.create(this.element);
  }
  async updateCustomer(event) {
    this.notice.emit(Notice.execute(this.t("Updating customer information..."), async () => {
      let result;
      this.account.customer = event.detail;
      const response = await Connection.put(`account/${this.account.id}/customer`, this.account.customer);
      if (Error.is(response))
        result = [false, this.t("Failed to update customer information.")];
      else {
        this.updateAccounts.emit();
        result = [true, this.t("Updated customer information.")];
      }
      return result;
    }));
  }
  render() {
    let result;
    switch (this.opened) {
      case "number":
        result = (h("payfunc-number-details", { number: this.account.number, identifier: this.account.id, name: "account" }));
        break;
      case "customer":
        result = h("payfunc-account-customer", { account: this.account });
        break;
      case "order":
        result = h("payfunc-orders-table", { orders: this.orders, display: true, receipt: this.whitelist });
        break;
      case "subscription":
        result = (h("payfunc-subscription-table", { subscriptions: this.account.subscription, account: this.account }));
        break;
      case "method":
        result = h("payfunc-account-method", { account: this.account });
        break;
      case "balance":
        result = h("payfunc-account-balance", { account: this.account, orders: this.orders });
        break;
      default:
        return "";
    }
    return h("td", { colSpan: 9 }, result);
  }
  static get is() { return "payfunc-account-details"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "opened": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "\"\" | \"balance\" | \"customer\" | \"method\" | \"number\" | \"order\" | \"subscription\"",
        "resolved": "\"\" | \"balance\" | \"customer\" | \"method\" | \"number\" | \"order\" | \"subscription\"",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "opened",
      "reflect": false,
      "defaultValue": "\"\""
    },
    "account": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Account",
        "resolved": "Account",
        "references": {
          "Account": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "orders": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Order[] | undefined",
        "resolved": "Order[] | undefined",
        "references": {
          "Order": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "whitelist": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "whitelist",
      "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": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "updateAccounts",
      "name": "updateAccounts",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }]; }
  static get elementRef() { return "element"; }
  static get listeners() { return [{
      "name": "updateCustomer",
      "method": "updateCustomer",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
