import { Component, h, Prop } from "@stencil/core";
import { DateTime } from "luxon";
import { intl } from "../../../global/global";
import { createStyleSheet } from "../../../styling/JSS";
import { luxonLocale } from "../../../utils/utils";
import { TextSpanView } from "../../sqm-text-span/sqm-text-span-view";
export class ReferralTableRewardsCell {
  constructor() {
    this.statusText = "{status, select, AVAILABLE {Available} CANCELLED {Cancelled} PENDING {Pending} PENDING_REVIEW {Pending} PAYOUT_APPROVED {Payout Approved} PROCESSING {Payment Processing} PAYOUT_FAILED {Payout Failed} PAYOUT_CANCELLED {Payout Cancelled} PENDING_TAX_REVIEW {Pending} PENDING_NEW_TAX_FORM {Pending} PENDING_TAX_SUBMISSION {Pending} PENDING_PARTNER_CREATION {Pending} DENIED {Denied} EXPIRED {Expired} REDEEMED {Redeemed} other {Not available} }";
    this.statusLongText = "{status, select, AVAILABLE {Reward expiring on} CANCELLED {Reward cancelled on} PENDING {Available on} PENDING_REVIEW {Pending since} PAYOUT_APPROVED {Processing until {scheduledPayoutDate}. Payout is then scheduled based on your settings.} PAYOUT_FAILED {Payout failed due to a fulfillment issue and is currently being retried.} PAYOUT_CANCELLED {If you think this is a mistake, contact our Support team.} PENDING_TAX_REVIEW {Awaiting tax form review} PENDING_NEW_TAX_FORM {Invalid tax form. Submit a new form to receive your rewards.} PROCESSING {Processing until {scheduledPayoutDate}. Payout is then scheduled based on your settings.} PENDING_TAX_SUBMISSION {Submit your tax documents to receive your rewards} PENDING_PARTNER_CREATION {Complete your tax and cash payout setup to receive your rewards} DENIED {Denied on} EXPIRED {Reward expired on} other {Not available} }";
    this.pendingForText = "{status} for {date}";
    this.locale = "en";
  }
  render() {
    var _a;
    intl.locale = this.locale;
    const style = {
      "@keyframes slideRight": {
        from: { opacity: 0 },
        to: { opacity: 1 },
      },
      DetailsContainer: {
        width: "100%",
        display: "flex",
        "align-items": "center",
        "justify-content": "space-between",
        "margin-right": "var(--sl-spacing-small)",
        "flex-wrap": "wrap",
      },
      Details: {
        "max-width": "500px",
        "&::part(header)": {
          padding: "var(--sl-spacing-x-small)",
          cursor: `${this.hideDetails ? "default" : "pointer"}`,
        },
        "&::part(content)": {
          padding: "var(--sl-spacing-x-small) var(--sl-spacing-medium)",
        },
        "&::part(base)": {
          border: "var(--sqm-border-thickness) solid var(--sqm-border-color)",
          opacity: "1",
        },
        "&::part(summary-icon)": {
          display: `${this.hideDetails ? "none" : "flex"}`,
        },
        "&::part(summary-icon[open])": {
          transform: "rotate(-90deg)",
        },
      },
      BadgeContainer: {
        "& > :not(:last-child)": {
          "margin-right": "var(--sl-spacing-x-small)",
        },
      },
      BoldText: {
        "font-weight": "var(--sl-font-weight-semibold)",
      },
      RedeemBadge: {
        paddingLeft: "var(--sl-spacing-xxx-small)",
        "&::part(base)": {
          textAlign: "center",
          maxWidth: "170px",
          whiteSpace: "pre-line",
          background: "var(--sqm-informative-color-icon)",
          color: "var(--sl-color-white)",
        },
      },
      DangerBadge: {
        paddingLeft: "var(--sl-spacing-xxx-small)",
        "&::part(base)": {
          textAlign: "center",
          maxWidth: "170px",
          whiteSpace: "pre-line",
          background: "var(--sqm-danger-color-icon)",
          color: "var(--sl-color-white)",
        },
      },
      WarningBadge: {
        paddingLeft: "var(--sl-spacing-xxx-small)",
        "&::part(base)": {
          textAlign: "center",
          maxWidth: "170px",
          whiteSpace: "pre-line",
          background: "var(--sqm-warning-color-icon)",
          color: "var(--sl-color-white)",
        },
      },
      SuccessBadge: {
        paddingLeft: "var(--sl-spacing-xxx-small)",
        "&::part(base)": {
          textAlign: "center",
          maxWidth: "170px",
          whiteSpace: "pre-line",
          background: "var(--sqm-success-color-icon)",
          color: "var(--sl-color-white)",
        },
      },
    };
    const sheet = createStyleSheet(style);
    const styleString = sheet.toString();
    const getBadgeCSSClass = (badgeType) => {
      switch (badgeType) {
        case "primary":
          return sheet.classes.RedeemBadge;
        case "danger":
          return sheet.classes.DangerBadge;
        case "success":
          return sheet.classes.SuccessBadge;
        case "warning":
        case "info":
          return sheet.classes.WarningBadge;
        default:
          return sheet.classes.WarningBadge;
      }
    };
    const getState = (reward, taxConnection) => {
      var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
      const possibleStates = [
        "REDEEMED",
        "CANCELLED",
        "EXPIRED",
        "PENDING",
        "AVAILABLE",
        "PENDING_REVIEW",
        "DENIED",
        "PAYOUT_APPROVED",
        "PAYOUT_CANCELLED",
        "PAYOUT_FAILED",
        "PENDING_TAX_REVIEW",
        "PENDING_NEW_TAX_FORM",
        "PENDING_TAX_SUBMISSION",
        "PENDING_PARTNER_CREATION",
        "PROCESSING",
      ];
      if (((_b = (_a = reward.referral) === null || _a === void 0 ? void 0 : _a.fraudData) === null || _b === void 0 ? void 0 : _b.moderationStatus) !== "APPROVED") {
        if (((_d = (_c = reward.referral) === null || _c === void 0 ? void 0 : _c.fraudData) === null || _d === void 0 ? void 0 : _d.moderationStatus) === "PENDING")
          return "PENDING_REVIEW";
        if (((_f = (_e = reward.referral) === null || _e === void 0 ? void 0 : _e.fraudData) === null || _f === void 0 ? void 0 : _f.moderationStatus) === "DENIED")
          return "DENIED";
      }
      const partnerFundsStatus = (_g = reward.partnerFundsTransfer) === null || _g === void 0 ? void 0 : _g.status;
      if (reward.partnerFundsTransfer) {
        if (partnerFundsStatus === "REVERSED")
          return "PAYOUT_CANCELLED";
        if (partnerFundsStatus === "OVERDUE")
          return "PAYOUT_FAILED";
        if (reward.partnerFundsTransfer.dateScheduled &&
          reward.partnerFundsTransfer.dateScheduled > Date.now()) {
          return "PROCESSING";
        }
        if (partnerFundsStatus === "TRANSFERRED" ||
          partnerFundsStatus === "NOT_YET_DUE" ||
          (reward.partnerFundsTransfer.dateScheduled &&
            reward.partnerFundsTransfer.dateScheduled < Date.now() &&
            partnerFundsStatus !== "OVERDUE" &&
            partnerFundsStatus !== "REVERSED"))
          return "PAYOUT_APPROVED";
      }
      if ((_h = reward === null || reward === void 0 ? void 0 : reward.pendingReasons) === null || _h === void 0 ? void 0 : _h.includes("US_TAX")) {
        if (!(taxConnection === null || taxConnection === void 0 ? void 0 : taxConnection.taxHandlingEnabled))
          return "PENDING";
        if (!(taxConnection === null || taxConnection === void 0 ? void 0 : taxConnection.connected))
          return "PENDING_PARTNER_CREATION";
        if ((_j = taxConnection === null || taxConnection === void 0 ? void 0 : taxConnection.publisher) === null || _j === void 0 ? void 0 : _j.requiredTaxDocumentType) {
          if (!((_k = taxConnection === null || taxConnection === void 0 ? void 0 : taxConnection.publisher) === null || _k === void 0 ? void 0 : _k.currentTaxDocument))
            return "PENDING_TAX_SUBMISSION";
          const status = taxConnection.publisher.currentTaxDocument.status;
          if (status === "INACTIVE" ||
            status === "INVALID_W9_ELECTRONIC_DOCUMENT" ||
            status === "INVALID_W9_ELECTRONIC_DOCUMENT_CHECK_INTERNAL")
            return "PENDING_NEW_TAX_FORM";
          if (status === "NOT_VERIFIED")
            return "PENDING_TAX_REVIEW";
        }
        if (!((_l = taxConnection === null || taxConnection === void 0 ? void 0 : taxConnection.publisher) === null || _l === void 0 ? void 0 : _l.withdrawalSettings))
          return "PENDING_PARTNER_CREATION";
      }
      if ((_m = reward === null || reward === void 0 ? void 0 : reward.pendingReasons) === null || _m === void 0 ? void 0 : _m.includes("MISSING_PAYOUT_CONFIGURATION")) {
        return "PENDING_PARTNER_CREATION";
      }
      if (reward.statuses.length === 1)
        return reward.statuses[0];
      return possibleStates.find((state) => reward.statuses.includes(state) && state);
    };
    const getSLBadgeType = (state) => {
      switch (state) {
        case "REDEEMED":
        case "PAYOUT_APPROVED":
          return "primary";
        case "DENIED":
        case "EXPIRED":
        case "CANCELLED":
        case "PAYOUT_FAILED":
        case "PAYOUT_CANCELLED":
          return "danger";
        case "PENDING":
        case "PENDING_REVIEW":
        case "PENDING_TAX_REVIEW":
        case "PENDING_NEW_TAX_FORM":
        case "PENDING_TAX_SUBMISSION":
        case "PENDING_PARTNER_CREATION":
        case "PROCESSING":
          return "warning";
        case "AVAILABLE":
          return "success";
      }
    };
    const getTimeDiff = (endTime) => {
      // Current implementation only calculates the difference from current time
      return DateTime.fromMillis(endTime)
        .setLocale(luxonLocale(this.locale))
        .toRelative()
        .replace("in", "")
        .trim();
    };
    return (_a = this.rewards) === null || _a === void 0 ? void 0 : _a.map((reward) => {
      var _a, _b, _c, _d;
      const state = getState(reward, this.taxConnection);
      const slBadgeType = getSLBadgeType(state);
      const badgeText = intl.formatMessage({ id: "statusShortMessage", defaultMessage: this.statusText }, {
        status: state,
      });
      const statusText = intl.formatMessage({
        id: "statusLongMessage",
        defaultMessage: this.statusLongText,
      }, {
        status: state,
        scheduledPayoutDate: ((_a = reward.partnerFundsTransfer) === null || _a === void 0 ? void 0 : _a.dateScheduled) ? (_b = DateTime.fromMillis(reward.partnerFundsTransfer.dateScheduled)) === null || _b === void 0 ? void 0 : _b.setLocale(luxonLocale(this.locale)).toLocaleString(DateTime.DATE_MED) : null,
      });
      return (h("sl-details", { class: sheet.classes.Details, disabled: this.hideDetails },
        h("style", { type: "text/css" }, styleString),
        h("div", { slot: "summary", class: sheet.classes.DetailsContainer },
          h(TextSpanView, { type: "p" },
            h("span", { part: "sqm-cell-value", class: sheet.classes.BoldText }, reward.prettyValue)),
          h("div", { class: sheet.classes.BadgeContainer },
            state === "PENDING" && reward.dateScheduledFor ? (h("sl-badge", { class: getBadgeCSSClass(slBadgeType), type: slBadgeType, pill: true }, intl.formatMessage({
              id: "pendingForText",
              defaultMessage: this.pendingForText,
            }, {
              status: badgeText,
              date: getTimeDiff(reward.dateScheduledFor),
            }))) : (h("sl-badge", { class: getBadgeCSSClass(slBadgeType), type: slBadgeType, pill: true }, badgeText)),
            reward.dateExpires && state === "AVAILABLE" && (h("sl-badge", { class: getBadgeCSSClass(slBadgeType), type: "info", pill: true },
              this.expiringText,
              ` ${getTimeDiff(reward.dateExpires)}`)))),
        h("div", null,
          state === "PENDING_REVIEW" && ((_c = reward.referral) === null || _c === void 0 ? void 0 : _c.dateModerated) && (h("div", null,
            h(TextSpanView, { type: "p" },
              statusText,
              " ",
              h("span", { class: sheet.classes.BoldText, part: "sqm-cell-value" }, DateTime.fromMillis(reward.referral.dateModerated)
                .setLocale(luxonLocale(this.locale))
                .toLocaleString(DateTime.DATE_MED))))),
          state === "PAYOUT_APPROVED" && (h("div", null,
            h(TextSpanView, { type: "p" }, statusText))),
          state === "PROCESSING" && (h("div", null,
            h(TextSpanView, { type: "p" }, statusText))),
          state === "PAYOUT_FAILED" && (h("div", null,
            h(TextSpanView, { type: "p" }, statusText))),
          state === "PAYOUT_CANCELLED" && (h("div", null,
            h(TextSpanView, { type: "p" }, statusText))),
          state === "PENDING_TAX_REVIEW" && (h("div", null,
            h(TextSpanView, { type: "p" }, statusText))),
          state === "PENDING_NEW_TAX_FORM" && (h("div", null,
            h(TextSpanView, { type: "p" }, statusText))),
          state === "PENDING_TAX_SUBMISSION" && (h("div", null,
            h(TextSpanView, { type: "p" }, statusText))),
          state === "PENDING_PARTNER_CREATION" && (h("div", null,
            h(TextSpanView, { type: "p" }, statusText))),
          state === "DENIED" && ((_d = reward.referral) === null || _d === void 0 ? void 0 : _d.dateModerated) && (h("div", null,
            h(TextSpanView, { type: "p" },
              statusText,
              " ",
              h("span", { class: sheet.classes.BoldText, part: "sqm-cell-value" }, DateTime.fromMillis(reward.referral.dateModerated)
                .setLocale(luxonLocale(this.locale))
                .toLocaleString(DateTime.DATE_MED)),
              ".",
              this.deniedHelpText ? ` ${this.deniedHelpText}` : ``))),
          reward.dateGiven && (h("div", null,
            h(TextSpanView, { type: "p" },
              this.rewardReceivedText,
              " ",
              h("span", { class: sheet.classes.BoldText, part: "sqm-cell-value" }, DateTime.fromMillis(reward.dateGiven)
                .setLocale(luxonLocale(this.locale))
                .toLocaleString(DateTime.DATE_MED))))),
          state === "EXPIRED" && reward.dateExpires && (h("div", null,
            h(TextSpanView, { type: "p" },
              statusText,
              " ",
              h("span", { class: sheet.classes.BoldText, part: "sqm-cell-value" }, DateTime.fromMillis(reward.dateExpires)
                .setLocale(luxonLocale(this.locale))
                .toLocaleString(DateTime.DATE_MED))))),
          state === "CANCELLED" && reward.dateCancelled && (h("div", null,
            h(TextSpanView, { type: "p" },
              statusText,
              " ",
              h("span", { class: sheet.classes.BoldText, part: "sqm-cell-value" }, DateTime.fromMillis(reward.dateCancelled)
                .setLocale(luxonLocale(this.locale))
                .toLocaleString(DateTime.DATE_MED))))),
          state === "PENDING" && reward.dateScheduledFor && (h("div", null,
            h(TextSpanView, { type: "p" },
              statusText,
              " ",
              h("span", { class: sheet.classes.BoldText, part: "sqm-cell-value" }, DateTime.fromMillis(reward.dateScheduledFor)
                .setLocale(luxonLocale(this.locale))
                .toLocaleString(DateTime.DATE_MED))))),
          " ",
          state === "AVAILABLE" && reward.dateExpires && (h("div", null,
            h(TextSpanView, { type: "p" },
              statusText,
              " ",
              h("span", { class: sheet.classes.BoldText, part: "sqm-cell-value" }, DateTime.fromMillis(reward.dateExpires)
                .setLocale(luxonLocale(this.locale))
                .toLocaleString(DateTime.DATE_MED))))),
          reward.fuelTankCode && (h("div", null,
            this.fuelTankText,
            " ",
            h("span", { class: sheet.classes.BoldText, part: "sqm-cell-value" }, reward.fuelTankCode))))));
    });
  }
  static get is() { return "sqm-referral-table-rewards-cell"; }
  static get encapsulation() { return "shadow"; }
  static get properties() { return {
    "rewards": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Reward[]",
        "resolved": "Reward[]",
        "references": {
          "Reward": {
            "location": "import",
            "path": "../../../saasquatch"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "taxConnection": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "ImpactConnection",
        "resolved": "ImpactConnection",
        "references": {
          "ImpactConnection": {
            "location": "import",
            "path": "../../../saasquatch"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "hideDetails": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "hide-details",
      "reflect": false
    },
    "statusText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "status-text",
      "reflect": false,
      "defaultValue": "\"{status, select, AVAILABLE {Available} CANCELLED {Cancelled} PENDING {Pending} PENDING_REVIEW {Pending} PAYOUT_APPROVED {Payout Approved} PROCESSING {Payment Processing} PAYOUT_FAILED {Payout Failed} PAYOUT_CANCELLED {Payout Cancelled} PENDING_TAX_REVIEW {Pending} PENDING_NEW_TAX_FORM {Pending} PENDING_TAX_SUBMISSION {Pending} PENDING_PARTNER_CREATION {Pending} DENIED {Denied} EXPIRED {Expired} REDEEMED {Redeemed} other {Not available} }\""
    },
    "statusLongText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "status-long-text",
      "reflect": false,
      "defaultValue": "\"{status, select, AVAILABLE {Reward expiring on} CANCELLED {Reward cancelled on} PENDING {Available on} PENDING_REVIEW {Pending since} PAYOUT_APPROVED {Processing until {scheduledPayoutDate}. Payout is then scheduled based on your settings.} PAYOUT_FAILED {Payout failed due to a fulfillment issue and is currently being retried.} PAYOUT_CANCELLED {If you think this is a mistake, contact our Support team.} PENDING_TAX_REVIEW {Awaiting tax form review} PENDING_NEW_TAX_FORM {Invalid tax form. Submit a new form to receive your rewards.} PROCESSING {Processing until {scheduledPayoutDate}. Payout is then scheduled based on your settings.} PENDING_TAX_SUBMISSION {Submit your tax documents to receive your rewards} PENDING_PARTNER_CREATION {Complete your tax and cash payout setup to receive your rewards} DENIED {Denied on} EXPIRED {Reward expired on} other {Not available} }\""
    },
    "fuelTankText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "fuel-tank-text",
      "reflect": false
    },
    "rewardReceivedText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "reward-received-text",
      "reflect": false
    },
    "expiringText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "expiring-text",
      "reflect": false
    },
    "pendingForText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "pending-for-text",
      "reflect": false,
      "defaultValue": "\"{status} for {date}\""
    },
    "deniedHelpText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "denied-help-text",
      "reflect": false
    },
    "locale": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "locale",
      "reflect": false,
      "defaultValue": "\"en\""
    }
  }; }
}
