import { Component, Element, Event, h, Host, Listen, Prop, State } from "@stencil/core";
import * as gracely from "gracely";
import { Notice } from "smoothly";
import { Order } from "@payfunc/model";
import "../orders-table/orders-table-row";
import { PayfuncPortal } from "../../../../portal";
import * as translation from "./translation";
export class IntergiroOrdersActions {
  constructor() {
    this.selected = {};
    this.statuses = ["charge", "cancel", "refund", "synchronize"];
  }
  componentWillLoad() {
    this.t = translation.create(this.element);
  }
  async handleTrigger(event) {
    if (this.statuses.some(s => s == event.detail.name)) {
      event.stopPropagation();
      const body = Object.values(this.selected).reduce((res, order) => order
        ? [
          ...res,
          {
            id: order.id,
            event: [{ type: event.detail.name }],
          },
        ]
        : res, []);
      const messages = this.getMessages(event.detail.name);
      this.notice.emit(Notice.execute(messages[0], async () => {
        var _a, _b, _c, _d;
        const response = await ((_a = PayfuncPortal.client) === null || _a === void 0 ? void 0 : _a.order.update(body));
        let result;
        if (!gracely.Error.is(response) && Array.isArray(response) && ((_c = (_b = response[0].event) === null || _b === void 0 ? void 0 : _b.pop()) === null || _c === void 0 ? void 0 : _c.type) != "fail") {
          this.selectedChanged.emit({});
          result = [true, messages[1]];
          const orders = await ((_d = PayfuncPortal.client) === null || _d === void 0 ? void 0 : _d.order.list());
          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 "ig-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
    }]; }
}
