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";
const style = {
  Badge: {
    "&::part(base)": {
      fontSize: "var(--sl-font-size-small)",
      padding: "4px 8px",
      paddingLeft: "10px",
      whiteSpace: "pre-line",
    },
  },
  RedeemBadge: {
    "&::part(base)": {
      padding: "4px 8px",
      fontSize: "var(--sl-font-size-small)",
      textAlign: "center",
      whiteSpace: "pre-line",
      background: "var(--sqm-informative-color-icon)",
      color: "var(--sl-color-white)",
    },
  },
  DangerBadge: {
    "&::part(base)": {
      padding: "4px 8px",
      fontSize: "var(--sl-font-size-small)",
      textAlign: "center",
      whiteSpace: "pre-line",
      background: "var(--sqm-danger-color-icon)",
      color: "var(--sl-color-white)",
    },
  },
  WarningBadge: {
    "&::part(base)": {
      padding: "4px 8px",
      fontSize: "var(--sl-font-size-small)",
      textAlign: "center",
      whiteSpace: "pre-line",
      background: "var(--sqm-warning-color-icon)",
      color: "var(--sl-color-white)",
    },
  },
  SuccessBadge: {
    "&::part(base)": {
      padding: "4px 8px",
      fontSize: "var(--sl-font-size-small)",
      textAlign: "center",
      whiteSpace: "pre-line",
      background: "var(--sqm-success-color-icon)",
      color: "var(--sl-color-white)",
    },
  },
  Date: {
    fontSize: "var(--sl-font-size-small)",
    color: "var(--sqm-text-subdued)",
    margin: "0",
  },
};
const sheet = createStyleSheet(style);
const styleString = sheet.toString();
export class RewardTableStatusCell {
  constructor() {
    this.expiryText = "Expires";
    this.locale = "en";
    this.pendingUsTax = "W-9 required";
    this.pendingTaxReview = "Awaiting tax form review.";
    this.pendingNewTaxForm = "Invalid tax form. Submit a new form to receive your rewards.";
    this.pendingTaxSubmission = "Submit your tax documents to receive your rewards.";
    this.pendingPartnerCreation = "Complete your tax and cash payout setup to receive your rewards.";
    this.pendingScheduled = "Until";
    this.pendingUnhandled = "Fulfillment error";
    this.pendingReviewText = "Awaiting review";
    this.deniedText = "Detected self-referral";
    this.payoutFailed = "Payout failed due to a fulfillment issue and is currently being retried.";
    this.payoutApproved = "Payout approved and scheduled for payment based on your settings.";
    this.payoutCancelled = "If you think this is a mistake, contact our Support team.";
    this.payoutProcessing = "Processing until {date}. Payout is then scheduled based on your settings.";
  }
  rewardStatus(reward) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j;
    const hasExpired = (_a = reward.statuses) === null || _a === void 0 ? void 0 : _a.includes("EXPIRED");
    const isPending = (_b = reward.statuses) === null || _b === void 0 ? void 0 : _b.includes("PENDING");
    const integrationOrFueltankReward = reward.type === "INTEGRATION" || reward.type === "FUELTANK";
    const fraudStatus = (_d = (_c = reward.referral) === null || _c === void 0 ? void 0 : _c.fraudData) === null || _d === void 0 ? void 0 : _d.moderationStatus;
    if (fraudStatus === "DENIED")
      return "DENIED";
    if (fraudStatus === "PENDING")
      return "PENDING_REVIEW";
    const partnerTransferStatus = (_e = reward.partnerFundsTransfer) === null || _e === void 0 ? void 0 : _e.status;
    if (reward.partnerFundsTransfer) {
      if (partnerTransferStatus === "REVERSED")
        return "PAYOUT_CANCELLED";
      if (partnerTransferStatus === "OVERDUE")
        return "PAYOUT_FAILED";
      if (reward.partnerFundsTransfer.dateScheduled &&
        reward.partnerFundsTransfer.dateScheduled > Date.now()) {
        return "PROCESSING";
      }
      if (partnerTransferStatus === "TRANSFERRED" ||
        partnerTransferStatus === "NOT_YET_DUE" ||
        (reward.partnerFundsTransfer.dateScheduled &&
          reward.partnerFundsTransfer.dateScheduled < Date.now() &&
          partnerTransferStatus !== "OVERDUE" &&
          partnerTransferStatus !== "REVERSED"))
        return "PAYOUT_APPROVED";
    }
    if (reward.dateCancelled)
      return "CANCELLED";
    if (hasExpired)
      return "EXPIRED";
    if (isPending)
      return "PENDING";
    if (reward.type === "CREDIT") {
      return ((_f = reward.statuses) === null || _f === void 0 ? void 0 : _f.includes("REDEEMED")) ? "REDEEMED" : "AVAILABLE";
    }
    if (reward.type === "PCT_DISCOUNT" && ((_g = reward.statuses) === null || _g === void 0 ? void 0 : _g.includes("AVAILABLE"))) {
      return "AVAILABLE";
    }
    if (integrationOrFueltankReward && isPending) {
      return "PENDING";
    }
    if (integrationOrFueltankReward && ((_h = reward.statuses) === null || _h === void 0 ? void 0 : _h.includes("CANCELLED"))) {
      return "CANCELLED";
    }
    if (integrationOrFueltankReward && ((_j = reward.statuses) === null || _j === void 0 ? void 0 : _j.includes("AVAILABLE"))) {
      return "AVAILABLE";
    }
    return "";
  }
  getTaxPendingReasons(reward, taxConnection) {
    var _a, _b, _c, _d, _e;
    if ((_a = reward === null || reward === void 0 ? void 0 : reward.pendingReasons) === null || _a === void 0 ? void 0 : _a.includes("US_TAX")) {
      if (!(taxConnection === null || taxConnection === void 0 ? void 0 : taxConnection.taxHandlingEnabled))
        return this.pendingUsTax;
      if (!(taxConnection === null || taxConnection === void 0 ? void 0 : taxConnection.connected))
        return this.pendingPartnerCreation;
      if ((_b = taxConnection === null || taxConnection === void 0 ? void 0 : taxConnection.publisher) === null || _b === void 0 ? void 0 : _b.requiredTaxDocumentType) {
        if (!((_c = taxConnection === null || taxConnection === void 0 ? void 0 : taxConnection.publisher) === null || _c === void 0 ? void 0 : _c.currentTaxDocument))
          return this.pendingTaxSubmission;
        const status = taxConnection.publisher.currentTaxDocument.status;
        if (status === "INACTIVE")
          return this.pendingNewTaxForm;
        if (status === "NOT_VERIFIED")
          return this.pendingTaxReview;
      }
      if (!((_d = taxConnection === null || taxConnection === void 0 ? void 0 : taxConnection.publisher) === null || _d === void 0 ? void 0 : _d.withdrawalSettings))
        return this.pendingPartnerCreation;
    }
    if ((_e = reward === null || reward === void 0 ? void 0 : reward.pendingReasons) === null || _e === void 0 ? void 0 : _e.includes("MISSING_PAYOUT_CONFIGURATION")) {
      return this.pendingPartnerCreation;
    }
    return "";
  }
  getBadgeType(rewardStatus) {
    switch (rewardStatus) {
      case "AVAILABLE":
        return "success";
      case "REDEEMED":
      case "PAYOUT_APPROVED":
        return "primary";
      case "PENDING":
      case "PENDING_REVIEW":
      case "PROCESSING":
        return "warning";
      default:
        return "danger";
    }
  }
  getPayoutStatusText(taxStatus) {
    var _a;
    switch (taxStatus) {
      case "US_TAX":
        return this.pendingUsTax;
      case "PAYOUT_APPROVED":
        return this.payoutApproved;
      case "PAYOUT_FAILED":
        return this.payoutFailed;
      case "PAYOUT_CANCELLED":
        return this.payoutCancelled;
      case "PENDING_TAX_REVIEW":
        return this.pendingTaxReview;
      case "PENDING_NEW_TAX_FORM":
        return this.pendingNewTaxForm;
      case "PENDING_TAX_SUBMISSION":
        return this.pendingTaxSubmission;
      case "PENDING_PARTNER_CREATION":
        return this.pendingPartnerCreation;
      case "PROCESSING":
        return intl.formatMessage({
          id: "payoutProcessingText",
          defaultMessage: this.payoutProcessing,
        }, {
          date: (_a = DateTime.fromMillis(this.reward.partnerFundsTransfer.dateScheduled)) === null || _a === void 0 ? void 0 : _a.setLocale(luxonLocale(this.locale)).toLocaleString(DateTime.DATE_MED),
        });
    }
  }
  render() {
    var _a;
    intl.locale = this.locale;
    const rewardStatus = this.rewardStatus(this.reward);
    const statusText = intl.formatMessage({ id: "statusMessage", defaultMessage: this.statusText }, {
      status: rewardStatus,
    });
    const badgeType = this.getBadgeType(rewardStatus);
    const dateShown = this.reward.dateCancelled ||
      this.reward.dateExpires ||
      this.reward.dateRedeemed ||
      "";
    const date = dateShown &&
      `${rewardStatus === "AVAILABLE" && this.reward.dateExpires
        ? this.expiryText + " "
        : ""}${(_a = DateTime.fromMillis(dateShown)) === null || _a === void 0 ? void 0 : _a.setLocale(luxonLocale(luxonLocale(this.locale))).toLocaleString(DateTime.DATE_MED)}`;
    const pendingReasons = rewardStatus === "PENDING"
      ? this.getTaxPendingReasons(this.reward, this.taxConnection) ||
        getRewardPendingReasons(this)
      : null;
    const fraudStatusText = rewardStatus === "PENDING_REVIEW"
      ? this.pendingReviewText
      : rewardStatus === "DENIED"
        ? this.deniedText
        : null;
    const getBadgeCSSClass = () => {
      switch (rewardStatus) {
        case "AVAILABLE":
          return sheet.classes.SuccessBadge;
        case "REDEEMED":
        case "PAYOUT_APPROVED":
          return sheet.classes.RedeemBadge;
        case "PENDING":
        case "PENDING_REVIEW":
        case "PROCESSING":
          return sheet.classes.WarningBadge;
        default:
          return sheet.classes.DangerBadge;
      }
    };
    const badgeCSSClass = getBadgeCSSClass();
    return (h("div", { style: { display: "contents" } },
      h("style", { type: "text/css" }, styleString),
      h("sl-badge", { type: badgeType, pill: true, class: badgeCSSClass }, statusText),
      h("p", { class: sheet.classes.Date }, fraudStatusText ||
        pendingReasons ||
        this.getPayoutStatusText(rewardStatus) ||
        date)));
    function getRewardPendingReasons(prop) {
      var _a;
      const pendingCodeMap = {
        SCHEDULED: prop.reward.dateScheduledFor &&
          prop.pendingScheduled +
            " " + ((_a = DateTime.fromMillis(prop.reward.dateScheduledFor)) === null || _a === void 0 ? void 0 : _a.setLocale(luxonLocale(luxonLocale(prop.locale || "en"))).toLocaleString(DateTime.DATE_MED)),
        UNHANDLED_ERROR: prop.pendingUnhandled,
        SUSPECTED_FRAUD: prop.pendingReview,
      };
      const taxReason = prop.getTaxPendingReasons(prop.reward, prop.taxConnection);
      return [
        ...(taxReason ? [taxReason] : []),
        ...prop.reward.pendingReasons.map((s) => {
          var _a;
          return (_a = pendingCodeMap[s]) !== null && _a !== void 0 ? _a : s;
        }),
      ].join(", ");
    }
  }
  static get is() { return "sqm-rewards-table-status-cell"; }
  static get encapsulation() { return "shadow"; }
  static get properties() { return {
    "statusText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "status-text",
      "reflect": false
    },
    "reward": {
      "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": ""
      }
    },
    "expiryText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "expiry-text",
      "reflect": false,
      "defaultValue": "\"Expires\""
    },
    "locale": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "locale",
      "reflect": false,
      "defaultValue": "\"en\""
    },
    "pendingUsTax": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "pending-us-tax",
      "reflect": false,
      "defaultValue": "\"W-9 required\""
    },
    "pendingTaxReview": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "pending-tax-review",
      "reflect": false,
      "defaultValue": "\"Awaiting tax form review.\""
    },
    "pendingNewTaxForm": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "pending-new-tax-form",
      "reflect": false,
      "defaultValue": "\"Invalid tax form. Submit a new form to receive your rewards.\""
    },
    "pendingTaxSubmission": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "pending-tax-submission",
      "reflect": false,
      "defaultValue": "\"Submit your tax documents to receive your rewards.\""
    },
    "pendingPartnerCreation": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "pending-partner-creation",
      "reflect": false,
      "defaultValue": "\"Complete your tax and cash payout setup to receive your rewards.\""
    },
    "pendingScheduled": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "pending-scheduled",
      "reflect": false,
      "defaultValue": "\"Until\""
    },
    "pendingUnhandled": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "pending-unhandled",
      "reflect": false,
      "defaultValue": "\"Fulfillment error\""
    },
    "pendingReviewText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "pending-review-text",
      "reflect": false,
      "defaultValue": "\"Awaiting review\""
    },
    "deniedText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "denied-text",
      "reflect": false,
      "defaultValue": "\"Detected self-referral\""
    },
    "payoutFailed": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "payout-failed",
      "reflect": false,
      "defaultValue": "\"Payout failed due to a fulfillment issue and is currently being retried.\""
    },
    "payoutApproved": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "payout-approved",
      "reflect": false,
      "defaultValue": "\"Payout approved and scheduled for payment based on your settings.\""
    },
    "payoutCancelled": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "payout-cancelled",
      "reflect": false,
      "defaultValue": "\"If you think this is a mistake, contact our Support team.\""
    },
    "payoutProcessing": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "payout-processing",
      "reflect": false,
      "defaultValue": "\"Processing until {date}. Payout is then scheduled based on your settings.\""
    }
  }; }
}
