import { Component, Element, Event, h, Host, Listen, Prop, State } from "@stencil/core";
import * as gracely from "gracely";
import { Notice } from "smoothly";
import { Connection, Order } from "@payfunc/model";
import "../orders-table-row";
import * as translation from "./translation";
export class PayfuncOrdersActions {
  constructor() {
    this.selected = {};
  }
  componentWillLoad() {
    this.t = translation.create(this.element);
  }
  async handleTrigger(event) {
    if (event.detail.name == "charge" ||
      event.detail.name == "cancel" ||
      event.detail.name == "refund" ||
      event.detail.name == "synchronize") {
      event.stopPropagation();
      const body = Object.values(this.selected).map(order => !order
        ? undefined
        : {
          id: order.id,
          event: [{ type: event.detail.name }],
        });
      const messages = this.getMessages(event.detail.name);
      this.notice.emit(Notice.execute(messages[0], async () => {
        var _a, _b;
        const response = await Connection.patch("order", body);
        let result;
        if (!gracely.Error.is(response) && Array.isArray(response) && ((_b = (_a = response[0].event) === null || _a === void 0 ? void 0 : _a.pop()) === null || _b === void 0 ? void 0 : _b.type) != "fail") {
          this.selectedChanged.emit({});
          result = [true, messages[1]];
          const orders = await Connection.get("order");
          this.updateOrder.emit(orders);
        }
        else
          result = [false, messages[2]];
        return result;
      }));
    }
  }
  getMessages(action) {
    let messages;
    switch (action) {
      case "charge":
        messages = [
          this.t("Charging orders..."),
          this.t("Orders successfully charged."),
          this.t("Failed to charge orders."),
        ];
        break;
      case "cancel":
        messages = [
          this.t("Canceling orders..."),
          this.t("Orders successfully canceled."),
          this.t("Failed to cancel orders."),
        ];
        break;
      case "refund":
        messages = [
          this.t("Refunding orders..."),
          this.t("Orders successfully refunded."),
          this.t("Failed to refund orders."),
        ];
        break;
      default:
        messages = [
          this.t("Synchronizing orders..."),
          this.t("Orders successfully synchronized."),
          this.t("Failed to synchronize orders."),
        ];
        break;
    }
    return messages;
  }
  render() {
    const t = this.t;
    const orders = Object.values(this.selected).filter(Order.is);
    const possibleEvents = Order.possibleEvents(orders);
    const chargeable = Object.keys(this.selected).length > 0 ? possibleEvents.includes("charge") : false;
    const cancelable = Object.keys(this.selected).length > 0 ? possibleEvents.includes("cancel") : false;
    const refundable = Object.keys(this.selected).length > 0 ? possibleEvents.includes("refund") : false;
    return (h(Host, { style: Object.entries(this.selected).length > 0 ? { right: "0" } : {} },
      h("smoothly-trigger", { color: "success", disabled: !chargeable, name: "charge" },
        h("smoothly-icon", { toolTip: t("Charge"), name: "checkmark" })),
      h("smoothly-trigger", { color: "danger", disabled: !cancelable, name: "cancel" },
        h("smoothly-icon", { toolTip: t("Cancel"), name: "close" })),
      h("smoothly-trigger", { color: "danger", disabled: !refundable, name: "refund" },
        h("smoothly-icon", { toolTip: t("Refund"), name: "arrow-undo" }))));
  }
  static get is() { return "payfunc-orders-actions"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "selected": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "{ [id: string]: Order | undefined }",
        "resolved": "{ [id: string]: Order | undefined; }",
        "references": {
          "Order": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "defaultValue": "{}"
    }
  }; }
  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"
          }
        }
      }
    }, {
      "method": "selectedChanged",
      "name": "selectedChanged",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "{ [id: string]: Order | undefined }",
        "resolved": "{ [id: string]: Order | undefined; }",
        "references": {
          "Order": {
            "location": "import",
            "path": "@payfunc/model"
          }
        }
      }
    }, {
      "method": "updateOrder",
      "name": "updateOrder",
      "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": "trigger",
      "method": "handleTrigger",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
