import { __, sprintf } from "@wordpress/i18n";
import "@scss/store-api/cart-subscription.scss";

const filterRegistry = window.wc?.blocksCheckout?.registerCheckoutFilters;

if (typeof filterRegistry === "function") {
  const getTrialData = (extensions, args) =>
    extensions?.arraysubs_subscription ??
    args?.cartItem?.extensions?.arraysubs_subscription ??
    null;

  filterRegistry("arraysubs", {
    cartItemClass(defaultValue, extensions, args) {
      const trialData = getTrialData(extensions, args);

      if (!trialData?.has_trial) {
        return defaultValue;
      }

      return `${defaultValue} arraysubs-trial-cart-item`.trim();
    },

    cartItemPrice(defaultValue, extensions, args) {
      const trialData = getTrialData(extensions, args);

      if (args?.context !== "cart" || !trialData?.has_trial) {
        return defaultValue;
      }

      return sprintf(
        /* translators: %s: free-trial duration, for example "2 days". */
        __("<price/> today (free for %s)", "arraysubs"),
        trialData.trial_schedule,
      );
    },
  });
}
