"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MablMcpContext = void 0;
const mablApiClientFactory_1 = require("@mablhq-internal/runtime/api/mablApiClientFactory");
const cliConfigProvider_1 = require("@mablhq-internal/runtime/providers/cliConfigProvider");
const common_1 = require("../utils/common");
const PlanHistory_1 = require("./PlanHistory");
const INITIALIZATION_ERROR_MESSAGE = 'MablMcpContext not initialized. Call initialize() first.';
class MablMcpContext {
    constructor() {
        this.initialized = false;
        this.isMablEmployee = false;
        this.planHistory = new PlanHistory_1.PlanHistory();
    }
    async getInitialWorkspaceId() {
        if (!this.mablApiClient) {
            throw new Error(INITIALIZATION_ERROR_MESSAGE);
        }
        const currentWorkspace = await cliConfigProvider_1.CliConfigProvider.getWorkspace();
        if (currentWorkspace?.id) {
            return currentWorkspace.id;
        }
        await this.mablApiClient.getSelf();
        const workspace = await cliConfigProvider_1.CliConfigProvider.getWorkspace();
        return workspace?.id;
    }
    async setLabsFeatures() {
        if (!this.mablApiClient || !this.workspaceId) {
            throw new Error(INITIALIZATION_ERROR_MESSAGE);
        }
        this.labsFeatures =
            await this.mablApiClient.getEnabledLabsFeaturesForWorkspace(this.workspaceId);
    }
    async setAccountFeatures() {
        if (!this.mablApiClient || !this.workspaceId) {
            throw new Error(INITIALIZATION_ERROR_MESSAGE);
        }
        this.accountFeatures =
            await this.mablApiClient.getEnabledAccountFeaturesByWorkspaceId(this.workspaceId);
    }
    async setInitialFeatureFlags() {
        await this.setAccountFeatures();
        await this.setLabsFeatures();
    }
    async setIsMablEmployee() {
        if (!this.mablApiClient || !this.workspaceId) {
            throw new Error(INITIALIZATION_ERROR_MESSAGE);
        }
        const userEmail = (await cliConfigProvider_1.CliConfigProvider.getCliConfig()).email;
        this.isMablEmployee = (0, common_1.validateMablEmail)(userEmail ?? '');
    }
    async initialize(maybeApiKey) {
        if (this.initialized) {
            return { success: true };
        }
        try {
            this.mablApiClient = process.env.CI
                ? await mablApiClientFactory_1.MablApiClientFactory.createApiClientFromOptionalApiKey()
                : await mablApiClientFactory_1.MablApiClientFactory.createUserApiClient(false);
        }
        catch (error) {
            const errorMessage = maybeApiKey
                ? `The mabl MCP no longer supports authentication via apiKey.
          Please authenticate the CLI by running 'mabl auth login'
          `
                : `
        The CLI is not authenticated.
        Please authenticate the CLI by running 'mabl auth login'
        `;
            return { success: false, errorMessage };
        }
        try {
            this.workspaceId = await this.getInitialWorkspaceId();
            await this.setInitialFeatureFlags();
            await this.setIsMablEmployee();
        }
        catch (error) {
            return {
                success: false,
                errorMessage: 'Could not determine workspaceId from the CLI config.',
            };
        }
        this.initialized = true;
        return { success: true };
    }
    async setWorkspaceId(newWorkspaceId) {
        this.workspaceId = newWorkspaceId;
        await this.setLabsFeatures();
    }
    featureIsAvailable(featureName) {
        const accountHasFeature = !!this.accountFeatures?.featureFlags.has(featureName);
        const labsHasFeature = !!this.labsFeatures?.featureFlags.has(featureName);
        return accountHasFeature || labsHasFeature;
    }
    getCurrentContext() {
        if (!this.mablApiClient || !this.workspaceId) {
            throw new Error(INITIALIZATION_ERROR_MESSAGE);
        }
        return {
            mablApiClient: this.mablApiClient,
            workspaceId: this.workspaceId,
            isMablEmployee: this.isMablEmployee,
            planHistory: this.planHistory,
        };
    }
    isInitialized() {
        return this.initialized;
    }
}
exports.MablMcpContext = MablMcpContext;
