import { BILLING_ENABLED, getAllEnvVars } from "../Config";
import LocalStorage from "./LocalStorage";
import BaseModel from "../../Models/DatabaseModels/DatabaseBaseModel/DatabaseBaseModel";
import SubscriptionPlan from "../../Types/Billing/SubscriptionPlan";
import ObjectID from "../../Types/ObjectID";
import Project from "../../Models/DatabaseModels/Project";
import { SubscriptionStatusUtil, } from "../../Types/Billing/SubscriptionStatus";
import Navigation from "./Navigation";
import SessionStorage from "./SessionStorage";
import Telemetry from "./Telemetry/Telemetry";
export default class ProjectUtil {
    static getCurrentProjectId() {
        // if this is not available in the url, check the session storage
        const currentProjectId = SessionStorage.getItem(`current_project_id`);
        if (currentProjectId && ObjectID.isValidUUID(currentProjectId)) {
            return new ObjectID(currentProjectId);
        }
        let projectId = Navigation.getFirstParam(2);
        if (projectId && projectId.includes(":projectId")) {
            projectId = undefined;
        }
        // Only return the projectId if it's a valid UUID
        if (projectId && ObjectID.isValidUUID(projectId)) {
            return new ObjectID(projectId);
        }
        return null;
    }
    static setIsSubscriptionInactiveOrOverdue(data) {
        var _a;
        const currentProjectId = (_a = this.getCurrentProjectId()) === null || _a === void 0 ? void 0 : _a.toString();
        const isSubscriptionInactive = SubscriptionStatusUtil.isSubscriptionInactive(data.paymentProviderMeteredSubscriptionStatus) ||
            SubscriptionStatusUtil.isSubscriptionInactive(data.paymentProviderSubscriptionStatus);
        const isSubscriptionOverdue = SubscriptionStatusUtil.isSubscriptionOverdue(data.paymentProviderMeteredSubscriptionStatus) ||
            SubscriptionStatusUtil.isSubscriptionOverdue(data.paymentProviderSubscriptionStatus);
        // save this to local storage
        LocalStorage.setItem((currentProjectId === null || currentProjectId === void 0 ? void 0 : currentProjectId.toString()) + "_isSubscriptionInactive", isSubscriptionInactive);
        LocalStorage.setItem((currentProjectId === null || currentProjectId === void 0 ? void 0 : currentProjectId.toString()) + "_isSubscriptionOverdue", isSubscriptionOverdue);
        return isSubscriptionInactive || isSubscriptionOverdue;
    }
    static isSubscriptionInactive() {
        var _a;
        const currentProjectId = (_a = this.getCurrentProjectId()) === null || _a === void 0 ? void 0 : _a.toString();
        return (Boolean(LocalStorage.getItem((currentProjectId === null || currentProjectId === void 0 ? void 0 : currentProjectId.toString()) + "_isSubscriptionInactive")) || false);
    }
    static getCurrentProject() {
        var _a;
        const currentProjectId = (_a = this.getCurrentProjectId()) === null || _a === void 0 ? void 0 : _a.toString();
        if (!LocalStorage.getItem(`project_${currentProjectId}`)) {
            return null;
        }
        const projectJson = LocalStorage.getItem(`project_${currentProjectId}`);
        return BaseModel.fromJSON(projectJson, Project);
    }
    static setCurrentProject(project) {
        var _a, _b;
        const currentProjectId = ((_a = project._id) === null || _a === void 0 ? void 0 : _a.toString()) || ((_b = this.getCurrentProjectId()) === null || _b === void 0 ? void 0 : _b.toString());
        if (project instanceof Project) {
            project = BaseModel.toJSON(project, Project);
        }
        LocalStorage.setItem(`project_${currentProjectId}`, project);
        SessionStorage.setItem(`current_project_id`, currentProjectId);
        // Keep RUM span context in sync with the project being viewed.
        if (currentProjectId) {
            Telemetry.setGlobalAttributes({ projectId: currentProjectId });
        }
    }
    static clearCurrentProject() {
        var _a;
        const currentProjectId = (_a = this.getCurrentProjectId()) === null || _a === void 0 ? void 0 : _a.toString();
        LocalStorage.setItem(`project_${currentProjectId}`, null);
        SessionStorage.setItem(`current_project_id`, null);
    }
    static getCurrentPlan() {
        if (!BILLING_ENABLED) {
            return null;
        }
        const project = this.getCurrentProject();
        if (!project || !project.paymentProviderPlanId) {
            return null;
        }
        return SubscriptionPlan.getPlanType(project.paymentProviderPlanId, getAllEnvVars());
    }
}
//# sourceMappingURL=Project.js.map