Skip to content

The open alternative to Salesforce, designed for AI.

1.4 KB
Raw Blame
import { type SettingsBillingPlanCell } from '@/settings/billing/types/SettingsBillingPlanCell';
import { type SettingsBillingPlanInterval } from '@/settings/billing/types/SettingsBillingPlanComparison';
import { type BillingPlanKey } from '~/generated-metadata/graphql';

type GetBillingPlanCellParams = {
  currentInterval: SettingsBillingPlanInterval;
  currentPlanKey: BillingPlanKey;
  interval: SettingsBillingPlanInterval;
  planKey: BillingPlanKey;
  upcomingInterval: SettingsBillingPlanInterval;
  upcomingPlanKey: BillingPlanKey;
};

export const getBillingPlanCell = ({
  currentInterval,
  currentPlanKey,
  interval,
  planKey,
  upcomingInterval,
  upcomingPlanKey,
}: GetBillingPlanCellParams): SettingsBillingPlanCell => {
  if (planKey === currentPlanKey && interval === currentInterval) {
    return { kind: 'current' };
  }

  if (planKey === upcomingPlanKey && interval === upcomingInterval) {
    return { kind: 'scheduled' };
  }

  const isIntervalSwitchScheduled = upcomingInterval !== currentInterval;

  if (planKey === upcomingPlanKey) {
    return {
      kind: 'change',
      change: isIntervalSwitchScheduled
        ? { type: 'CANCEL_INTERVAL_SWITCH' }
        : { type: 'SWITCH_INTERVAL', targetInterval: interval },
    };
  }

  if (planKey === currentPlanKey) {
    return { kind: 'change', change: { type: 'CANCEL_PLAN_SWITCH' } };
  }

  return {
    kind: 'change',
    change: { type: 'SWITCH_PLAN', targetPlanKey: planKey },
  };
};