"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateAppCommand = exports.NoTemplatesError = void 0;
const ui_1 = require("../ui");
class NoTemplatesError extends Error {
    constructor() {
        super(ui_1.Text.create.error.template.noTemplates);
    }
}
exports.NoTemplatesError = NoTemplatesError;
class CreateAppCommand {
    templater;
    registerAppCommand;
    packageInstaller;
    statsig;
    constructor(templater, registerAppCommand, packageInstaller, statsig) {
        this.templater = templater;
        this.registerAppCommand = registerAppCommand;
        this.packageInstaller = packageInstaller;
        this.statsig = statsig;
    }
    async execute({ name, description, template, directory, requiredProduct, developerSpaceId, billingConsent }) {
        await this.templater.downloadAndExtract(template, directory);
        const result = await this.registerAppCommand.registerApp({ name, description, developerSpaceId, billingConsent }, directory, {
            shouldPrependAppName: true,
            requiredProduct
        });
        await this.packageInstaller.install(directory);
        return {
            analytics: { template, appId: result.id, appName: name },
            environments: result.environments.map((env) => env.key)
        };
    }
    async getAvailableTemplates() {
        const availableTemplates = await this.templater.getAvailableTemplates();
        if (!availableTemplates.length) {
            throw new NoTemplatesError();
        }
        return availableTemplates;
    }
    async filterAvailableProducts(products, teamworkGraph, crossContext) {
        const useTeamworkGraph = await this.statsig.isTeamworkGraphProductSupported();
        const useCrossProduct = await this.statsig.enableCrossProductAppInstallation();
        return products.filter((product) => {
            switch (product) {
                case teamworkGraph:
                    return useTeamworkGraph;
                case crossContext:
                    return useCrossProduct;
                default:
                    return true;
            }
        });
    }
}
exports.CreateAppCommand = CreateAppCommand;
