"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getUpgradeMetaByProjectId = exports.getUpgradeMetaByOrgId = void 0;
const callControlPlaneApi_1 = require("./callContolPlane/callControlPlaneApi");
const logger_1 = __importDefault(require("../logger"));
const getUpgradeMetaByOrgId = async (orgId, params = {}, log = false) => {
    const restrictions = await (0, callControlPlaneApi_1.callControlPlane)(`/trusys/billing-plans/${orgId}`, {
        method: 'GET',
        params: {
            orgId: orgId,
            ...params,
        },
    }, '');
    if (log) {
        logger_1.default.info(`[getUpgradeMetaByOrgId] Raw restrictions response: ${JSON.stringify(restrictions)}`);
        logger_1.default.info(`[getUpgradeMetaByOrgId] restrictions?.status: ${restrictions?.status}, restrictions?.message: ${restrictions?.message}`);
        logger_1.default.info(`[getUpgradeMetaByOrgId] restrictions?.response: ${JSON.stringify(restrictions?.response)}`);
    }
    const upgradeMetadata = {
        upgradeRequired: false,
        upgradeTitle: '',
        topUpRequired: false,
        upgradeMessage: '',
        currentPlan: '',
        currentPlanId: '',
    };
    const globalBanner = {
        show: false,
        data: {
            content: '',
            link: '',
            linkText: '',
            type: '',
            action: '',
            actionType: '',
            actionContent: '',
        },
    };
    if (restrictions?.status === 1 && restrictions?.message === 'success') {
        const { restrictionsData, currentPlan, currentPlanId, isEnterprise, orgId, orgName, wallet, isStarter, thresholds, utilizations, notifications, usages, bucketInfo, } = restrictions?.response ?? {};
        if (log) {
            logger_1.default.info(`[getUpgradeMetaByOrgId] After destructuring - notifications: ${JSON.stringify(notifications)}, usages: ${JSON.stringify(usages)}, utilizations: ${JSON.stringify(utilizations)}`);
            logger_1.default.info(`[getUpgradeMetaByOrgId] notifications type: ${typeof notifications}, usages type: ${typeof usages}`);
        }
        upgradeMetadata.currentPlan = currentPlan ?? '';
        upgradeMetadata.currentPlanId = currentPlanId ?? '';
        const result = {
            upgradeMetadata,
            restrictionsData,
            isEnterprise,
            orgId,
            orgName,
            globalBanner,
            wallet,
            isStarter,
            thresholds,
            utilizations,
            notifications,
            usages,
            bucketInfo,
        };
        if (log) {
            logger_1.default.info(`[getUpgradeMetaByOrgId] Final result - notifications: ${JSON.stringify(result.notifications)}, usages: ${JSON.stringify(result.usages)}`);
        }
        return result;
    }
    if (log) {
        logger_1.default.warn(`[getUpgradeMetaByOrgId] API call did not succeed - status: ${restrictions?.status}, message: ${restrictions?.message}`);
        logger_1.default.warn(`[getUpgradeMetaByOrgId] Returning default values`);
    }
    return {
        upgradeMetadata,
        restrictionsData: null,
        isEnterprise: false,
        orgId: '',
        orgName: '',
        globalBanner,
        wallet: {
            credits: 0,
            currency: '',
        },
        isStarter: true, // default to this
        thresholds: {
            globalNotificationThreshold: 80,
            globalNotificationThresholdType: 'percentage',
            walletNotificationThreshold: 100,
            walletNotificationThresholdType: 'amount',
        },
        utilizations: {},
        notifications: {
            canSendLowWalletBalance: false,
            canSendMetricUsageExceeded: false,
            canSendSpanUsageExceeded: false,
            canSendWalletUsageExceeded: false,
            canSendMetricRenewal: false,
            canSendSpanRenewal: false,
            canSendGuardrailsUsageExceeded: false,
            canSendGuardrailsRenewal: false,
            lastSentLowWalletBalance: null,
            lastSentMetricUsageExceeded: null,
            lastSentSpanUsageExceeded: null,
            lastSentWalletUsageExceeded: null,
            lastSentMetricRenewal: null,
            lastSentSpanRenewal: null,
            lastSentGuardrailsUsageExceeded: null,
        },
        usages: {
            metric: 0,
            span: 0,
            guardrails: 0,
        },
    };
};
exports.getUpgradeMetaByOrgId = getUpgradeMetaByOrgId;
const getUpgradeMetaByProjectId = async (projectId) => {
    const restrictions = await (0, callControlPlaneApi_1.callControlPlane)(`/trusys/billing-plans-by-project/${projectId}`, {
        method: 'GET',
        params: {
            projectId: projectId,
        },
    }, '');
    const upgradeMetadata = {
        upgradeRequired: false,
        topUpRequired: false,
        upgradeMessage: '',
        currentPlan: '',
        currentPlanId: '',
    };
    if (restrictions?.status === 1 && restrictions?.message === 'success') {
        const { restrictionsData, currentPlan, currentPlanId, isEnterprise, orgId, orgName, } = restrictions?.response ?? {};
        upgradeMetadata.currentPlan = currentPlan ?? '';
        upgradeMetadata.currentPlanId = currentPlanId ?? '';
        return {
            isEnterprise,
            upgradeMetadata,
            restrictionsData,
            orgId,
            orgName,
        };
    }
    return {
        upgradeMetadata,
        restrictionsData: null,
        isEnterprise: false,
        orgId: '',
        orgName: '',
    };
};
exports.getUpgradeMetaByProjectId = getUpgradeMetaByProjectId;
//# sourceMappingURL=get-upgrade-meta.js.map