import { PLAN_TYPES, PLAN_SERVICES, PLANS, ORGANIZATION_FLAGS } from '../constants';

const { PLAN, ADDON } = PLAN_TYPES;
const { MAIL } = PLAN_SERVICES;
const { PLUS, VPNPLUS } = PLANS;

/**
 * Get plan from current subscription
 * @param {Array} subscription.Plans
 * @param {Integer} service
 * @returns {Object} plan
 */
export const getPlan = ({ Plans = [] } = {}, service = MAIL) => {
    return Plans.find(({ Services, Type }) => Type === PLAN && Services & service) || {};
};

export const getPlans = ({ Plans = [] } = {}) => Plans.filter(({ Type }) => Type === PLAN);
export const getAddons = ({ Plans = [] } = {}) => Plans.filter(({ Type }) => Type === ADDON);

/**
 *
 * @param {Object} subscription
 * @param {Integer} service
 * @returns {String} plan name
 */
export const getPlanName = (subscription = {}, service = MAIL) => {
    const { Name = '' } = getPlan(subscription, service);
    return Name;
};

/**
 * Check if a subscription is eligible to BUNDLE coupon
 * @param {Object} subscription
 * @returns {Boolean} is eligible to BUNDLE
 */
export const isBundleEligible = (subscription = {}) => {
    const { Plans = [], CouponCode = '' } = subscription;

    if (CouponCode) {
        return false;
    }

    if (!Plans.length) {
        return true;
    }

    const plans = getPlans(subscription);

    if (plans.length > 1) {
        return false;
    }

    const [{ Name = '' }] = plans;

    return [PLUS, VPNPLUS].includes(Name);
};

export const hasLifetime = (subscription = {}) => {
    const { CouponCode = '' } = subscription;
    return CouponCode === 'LIFETIME';
};

@@ -40,26 +40,13 @@
 NS, 
-ORGANIZATION_FLAGS
+CYCLE
  %7D f
@@ -1579,12 +1579,437 @@
 FETIME';%0A%7D;%0A
+%0A/**%0A *%0A * @param %7BString%7D name plan or addon name%0A * @param %7BArray%7D plans coming for Plans API%0A * @param %7BObject%7D subscription%0A * @returns %7BNumber%7D price%0A */%0Aexport const getMonthlyBaseAmount = (name = '', plans = %5B%5D, subscription = %7B%7D) =%3E %7B%0A    const base = plans.find((%7B Name %7D) =%3E Name === name);%0A    return subscription.Plans.filter((%7B Name %7D) =%3E Name === name).reduce((acc) =%3E acc + base.Pricing%5BCYCLE.MONTHLY%5D, 0);%0A%7D;%0A

Add getMonthlyBaseAmount helper