var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { IsBillingEnabled, getAllEnvVars, } from "../../../../Server/EnvironmentConfig";
import DatabaseRequestType from "../../BaseDatabase/DatabaseRequestType";
import SubscriptionPlan from "../../../../Types/Billing/SubscriptionPlan";
import PaymentRequiredException from "../../../../Types/Exception/PaymentRequiredException";
import CaptureSpan from "../../../Utils/Telemetry/CaptureSpan";
export default class BillingPermissions {
    static checkBillingPermissions(modelType, props, type) {
        /// Check billing permissions.
        if (IsBillingEnabled && props.currentPlan) {
            const model = new modelType();
            if (props.isSubscriptionUnpaid &&
                !model.allowAccessIfSubscriptionIsUnpaid) {
                throw new PaymentRequiredException("Your current subscription is in an unpaid state. Looks like your payment method failed. Please add a new payment method in Project Settings > Invoices to pay unpaid invoices.");
            }
            if (type === DatabaseRequestType.Create && model.createBillingPlan) {
                if (!SubscriptionPlan.isFeatureAccessibleOnCurrentPlan(model.createBillingPlan, props.currentPlan, getAllEnvVars())) {
                    throw new PaymentRequiredException("Please upgrade your plan to " +
                        model.createBillingPlan +
                        " to access this feature");
                }
            }
            if (type === DatabaseRequestType.Update && model.updateBillingPlan) {
                if (!SubscriptionPlan.isFeatureAccessibleOnCurrentPlan(model.updateBillingPlan, props.currentPlan, getAllEnvVars())) {
                    throw new PaymentRequiredException("Please upgrade your plan to " +
                        model.createBillingPlan +
                        " to access this feature");
                }
            }
            if (type === DatabaseRequestType.Delete && model.deleteBillingPlan) {
                if (!SubscriptionPlan.isFeatureAccessibleOnCurrentPlan(model.deleteBillingPlan, props.currentPlan, getAllEnvVars())) {
                    throw new PaymentRequiredException("Please upgrade your plan to " +
                        model.createBillingPlan +
                        " to access this feature");
                }
            }
            if (type === DatabaseRequestType.Read && model.readBillingPlan) {
                if (!SubscriptionPlan.isFeatureAccessibleOnCurrentPlan(model.readBillingPlan, props.currentPlan, getAllEnvVars())) {
                    throw new PaymentRequiredException("Please upgrade your plan to " +
                        model.createBillingPlan +
                        " to access this feature");
                }
            }
        }
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object, String]),
    __metadata("design:returntype", void 0)
], BillingPermissions, "checkBillingPermissions", null);
//# sourceMappingURL=BillingPermission.js.map