import { useParentQueryValue } from "@saasquatch/component-boilerplate";
import { DateTime } from "luxon";
import { intl } from "../../../global/global";
import { USER_QUERY_NAMESPACE } from "../data";
export function usePayoutDetailsCard(props) {
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
  const { data, loading } = useParentQueryValue(USER_QUERY_NAMESPACE);
  const publisher = (_b = (_a = data === null || data === void 0 ? void 0 : data.user) === null || _a === void 0 ? void 0 : _a.impactConnection) === null || _b === void 0 ? void 0 : _b.publisher;
  function getPayoutDetailedStatusText(paymentDay) {
    var _a;
    const currentDay = DateTime.now().day;
    if (paymentDay === "1") {
      return currentDay === 1
        ? DateTime.now().toFormat("LLL dd, yyyy")
        : DateTime.now()
          .plus({ month: 1 })
          .startOf("month")
          .toFormat("LLL dd, yyyy");
    }
    else if (paymentDay === "15") {
      return currentDay <= 15
        ? DateTime.now()
          .plus({ day: 15 - currentDay })
          .toFormat("LLL dd, yyyy")
        : DateTime.now()
          .plus({ month: 1 })
          .startOf("month")
          .plus({ day: 14 })
          .toFormat("LLL dd, yyyy");
    }
    else {
      intl.formatMessage({
        id: `nextPayoutBalance`,
        defaultMessage: props.thresholdPayoutText,
      }, {
        thresholdBalance: (_a = publisher === null || publisher === void 0 ? void 0 : publisher.withdrawalSettings) === null || _a === void 0 ? void 0 : _a.paymentThreshold,
      });
    }
  }
  const paymentDay = getPayoutDetailedStatusText((_c = publisher === null || publisher === void 0 ? void 0 : publisher.withdrawalSettings) === null || _c === void 0 ? void 0 : _c.paymentDay);
  const isPayoutToday = DateTime.now().toFormat("LLL dd, yyyy") === paymentDay;
  return {
    states: {
      loading,
      thresholdBalance: ((_d = publisher === null || publisher === void 0 ? void 0 : publisher.withdrawalSettings) === null || _d === void 0 ? void 0 : _d.paymentThreshold) ? `${publisher === null || publisher === void 0 ? void 0 : publisher.currency}${(_e = publisher === null || publisher === void 0 ? void 0 : publisher.withdrawalSettings) === null || _e === void 0 ? void 0 : _e.paymentThreshold}`
        : undefined,
      balance: (_f = publisher === null || publisher === void 0 ? void 0 : publisher.payoutsAccount) === null || _f === void 0 ? void 0 : _f.balance,
      badgeStatus: ((_g = publisher === null || publisher === void 0 ? void 0 : publisher.withdrawalSettings) === null || _g === void 0 ? void 0 : _g.paymentSchedulingType) !== "FIXED_DAY"
        ? "thresholdPayout"
        : isPayoutToday
          ? "payoutToday"
          : "nextPayout",
      payoutType: (_h = publisher === null || publisher === void 0 ? void 0 : publisher.withdrawalSettings) === null || _h === void 0 ? void 0 : _h.paymentMethod,
      error: (_j = publisher === null || publisher === void 0 ? void 0 : publisher.payoutsAccount) === null || _j === void 0 ? void 0 : _j.hold,
      paypalEmailAddress: (_k = publisher === null || publisher === void 0 ? void 0 : publisher.withdrawalSettings) === null || _k === void 0 ? void 0 : _k.paypalEmailAddress,
      cardNumberPreview: (_l = publisher === null || publisher === void 0 ? void 0 : publisher.withdrawalSettings) === null || _l === void 0 ? void 0 : _l.bankAccountNumber,
      nextPayoutDate: paymentDay,
    },
    text: props.getTextProps(),
  };
}
