import BadDataException from "../Exception/BadDataException";
export var PlanType;
(function (PlanType) {
    PlanType["Free"] = "Free";
    PlanType["Growth"] = "Growth";
    PlanType["Enterprise"] = "Enterprise";
    PlanType["Scale"] = "Scale";
})(PlanType || (PlanType = {}));
export default class SubscriptionPlan {
    constructor(monthlyPlanId, yearlyPlanId, name, monthlySubscriptionAmountInUSD, yearlySubscriptionAmountInUSD, order, trialPeriodInDays) {
        this.monthlyPlanId = "";
        this.yearlyPlanId = "";
        this.name = "";
        this.monthlySubscriptionAmountInUSD = 0;
        this.yearlySubscriptionAmountInUSD = 0;
        this.order = -1;
        this.trialPeriodInDays = 0;
        this.monthlyPlanId = monthlyPlanId;
        this.yearlyPlanId = yearlyPlanId;
        this.name = name;
        this.monthlySubscriptionAmountInUSD = monthlySubscriptionAmountInUSD;
        this.yearlySubscriptionAmountInUSD = yearlySubscriptionAmountInUSD;
        this.order = order;
        this.trialPeriodInDays = trialPeriodInDays;
    }
    getMonthlyPlanId() {
        return this.monthlyPlanId;
    }
    getYearlyPlanId() {
        return this.yearlyPlanId;
    }
    getPlanOrder() {
        return this.order;
    }
    getTrialPeriod() {
        return this.trialPeriodInDays;
    }
    getName() {
        return this.name;
    }
    static isFreePlan(planId, env) {
        const plan = this.getSubscriptionPlanById(planId, env);
        if (plan) {
            if (plan.getMonthlyPlanId() === planId &&
                plan.getMonthlySubscriptionAmountInUSD() === 0) {
                return true;
            }
            if (plan.getYearlyPlanId() === planId &&
                plan.getYearlySubscriptionAmountInUSD() === 0) {
                return true;
            }
        }
        return false;
    }
    static isCustomPricingPlan(planId, env) {
        const plan = this.getSubscriptionPlanById(planId, env);
        if (plan) {
            if (plan.getMonthlyPlanId() === planId && plan.isCustomPricing()) {
                return true;
            }
        }
        return false;
    }
    getYearlySubscriptionAmountInUSD() {
        return this.yearlySubscriptionAmountInUSD;
    }
    getMonthlySubscriptionAmountInUSD() {
        return this.monthlySubscriptionAmountInUSD;
    }
    isCustomPricing() {
        return this.monthlySubscriptionAmountInUSD === -1;
    }
    static getSubscriptionPlans(env) {
        const plans = [];
        for (const key in env || process.env) {
            if (key.startsWith("SUBSCRIPTION_PLAN_")) {
                const content = (env || process.env)[key] || "";
                const values = content.split(",");
                if (values.length > 0) {
                    plans.push(new SubscriptionPlan(values[1], values[2], values[0], parseInt(values[3]), parseInt(values[4]), parseInt(values[5]), parseInt(values[6])));
                }
            }
        }
        plans.sort((a, b) => {
            return a.order - b.order;
        });
        return plans;
    }
    static getSubscriptionPlanById(planId, env) {
        const plans = this.getSubscriptionPlans(env);
        return plans.find((plan) => {
            return (plan.getMonthlyPlanId() === planId || plan.getYearlyPlanId() === planId);
        });
    }
    static isValidPlanId(planId, env) {
        return Boolean(this.getSubscriptionPlanById(planId, env));
    }
    static getPlanType(planId, env) {
        const plan = this.getSubscriptionPlanById(planId, env);
        if (!plan) {
            throw new BadDataException("Plan ID is invalid");
        }
        return plan.getName();
    }
    static getSubscriptionPlanFromPlanSelect(planSelect, env) {
        const plan = this.getSubscriptionPlans(env).find((plan) => {
            return plan.getName() === planSelect;
        });
        if (!plan) {
            throw new BadDataException("Invalid Plan");
        }
        return plan;
    }
    static isFeatureAccessibleOnCurrentPlan(featurePlan, currentPlan, env) {
        const featureSubscriptionPlan = this.getSubscriptionPlanFromPlanSelect(featurePlan, env);
        const currentSubscriptionPlan = this.getSubscriptionPlanFromPlanSelect(currentPlan, env);
        if (featureSubscriptionPlan.getPlanOrder() >
            currentSubscriptionPlan.getPlanOrder()) {
            return false;
        }
        return true;
    }
    static isYearlyPlan(planId, env) {
        const plan = this.getSubscriptionPlanById(planId, env);
        return (plan === null || plan === void 0 ? void 0 : plan.getYearlyPlanId()) === planId;
    }
    static isUnpaid(subscriptionStatus) {
        if (subscriptionStatus === "incomplete" ||
            subscriptionStatus === "incomplete_expired" ||
            subscriptionStatus === "past_due" ||
            subscriptionStatus === "canceled" ||
            subscriptionStatus === "unpaid") {
            return true;
        }
        return false;
    }
}
//# sourceMappingURL=SubscriptionPlan.js.map