import { Component, Element, Event, h, Prop, State } from "@stencil/core";
import { Error } from "gracely";
import { Notice } from "smoothly";
import { Connection, Schedule } from "@payfunc/model";
import * as translation from "./translation";
export class IntergiroSubscriptionDetails {
  constructor() {
    this.opened = "";
  }
  componentWillLoad() {
    this.t = translation.create(this.element);
  }
  cancel() {
    this.notice.emit(Notice.execute(this.t("Cancelling subscription..."), async () => {
      const response = await Connection.delete(`customer/${this.customer.id}/subscription/${this.subscription.id}`);
      return !Error.is(response)
        ? [true, this.t("Subscription was cancelled.")]
        : [false, this.t("Failed to cancel subscription.")];
    }));
  }
  update(value, property) {
    const update = { [property]: value };
    this.subscription = Object.assign(Object.assign({}, this.subscription), update);
    this.notice.emit(Notice.execute(this.t("Updating subscription..."), async () => {
      const response = await Connection.put(`customer/${this.customer.id}/subscription/${this.subscription.id}`, this.subscription);
      return !Error.is(response)
        ? [true, this.t("Subscription was updated.")]
        : [false, this.t("Failed to update subscription.")];
    }));
  }
  render() {
    var _a;
    let result;
    switch (this.opened) {
      case "number":
        result = (h("ig-number-details", { number: this.subscription.number, identifier: this.subscription.id, onNumberUpdated: (e) => this.update(e.detail, "number"), name: "subscription" }));
        break;
      case "amount":
        result = (h("ig-items-list", { onChanged: (e) => this.update(e.detail, "items"), editable: true, class: typeof this.subscription.items == "number" ? "number" : "", items: this.subscription.items, currency: this.subscription.currency }));
        break;
      case "schedule":
        result = h("ig-schedule", { schedule: this.subscription.schedule });
        break;
      case "end":
        result = [
          h("label", null, this.t("End")),
          h("section", null,
            h("ig-display-edit", { deletable: false, name: this.t("End date"), onChanged: (e) => this.update(e.detail, "end"), value: (_a = this.subscription.end) !== null && _a !== void 0 ? _a : this.t("ongoing") }),
            h("smoothly-icon", { onClick: () => this.cancel(), name: "trash", color: "default", fill: "solid", toolTip: this.t("Cancel subscription") })),
        ];
        break;
      case "start":
        result = [
          h("label", null, this.t("Start")),
          h("ig-display-edit", { deletable: false, name: this.t("Start date"), onChanged: (e) => this.update(e.detail, "start"), value: this.subscription.start }),
        ];
        break;
      case "due":
        result = (h("ig-future-due-dates", { dates: Schedule.calculateDueDates(this.subscription.schedule, 5, this.subscription.start, this.subscription.end) }));
        break;
      default:
        return "";
    }
    return h("td", { colSpan: 8 }, result);
  }
  static get is() { return "ig-subscription-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": "\"\" | \"number\" | \"amount\" | \"schedule\" | \"start\" | \"end\" | \"due\"",
        "resolved": "\"\" | \"amount\" | \"due\" | \"end\" | \"number\" | \"schedule\" | \"start\"",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "opened",
      "reflect": false,
      "defaultValue": "\"\""
    },
    "subscription": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Subscription",
        "resolved": "Subscription",
        "references": {
          "Subscription": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "customer": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Customer",
        "resolved": "Customer",
        "references": {
          "Customer": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  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"; }
}
