"use strict";
const di_1 = require("@n8n/di");
const response_error_1 = require("../../../../errors/response-errors/abstract/response.error");
const community_packages_lifecycle_service_1 = require("../../../../modules/community-packages/community-packages.lifecycle.service");
const community_packages_mapper_1 = require("./community-packages.mapper");
const global_middleware_1 = require("../../shared/middlewares/global.middleware");
function sendResponseError(res, error) {
    return res.status(error.httpStatusCode).json({ message: error.message });
}
module.exports = {
    installPackage: [
        (0, global_middleware_1.publicApiScope)('communityPackage:install'),
        async (req, res) => {
            const lifecycle = di_1.Container.get(community_packages_lifecycle_service_1.CommunityPackagesLifecycleService);
            try {
                const installedPackage = await lifecycle.install({ name: req.body.name, version: req.body.version, verify: req.body.verify ?? false }, req.user, 'publicApi');
                return res.json((0, community_packages_mapper_1.mapToCommunityPackage)(installedPackage));
            }
            catch (error) {
                if (error instanceof response_error_1.ResponseError) {
                    return sendResponseError(res, error);
                }
                throw error;
            }
        },
    ],
    getInstalledPackages: [
        (0, global_middleware_1.publicApiScope)('communityPackage:list'),
        async (_req, res) => {
            const lifecycle = di_1.Container.get(community_packages_lifecycle_service_1.CommunityPackagesLifecycleService);
            const packages = await lifecycle.listInstalledPackages();
            return res.json((0, community_packages_mapper_1.mapToCommunityPackageList)(packages));
        },
    ],
    updatePackage: [
        (0, global_middleware_1.publicApiScope)('communityPackage:update'),
        async (req, res) => {
            const lifecycle = di_1.Container.get(community_packages_lifecycle_service_1.CommunityPackagesLifecycleService);
            try {
                const updated = await lifecycle.update({
                    name: req.params.name,
                    version: req.body?.version,
                }, req.user, 'notFound');
                return res.json((0, community_packages_mapper_1.mapToCommunityPackage)(updated));
            }
            catch (error) {
                if (error instanceof response_error_1.ResponseError) {
                    return sendResponseError(res, error);
                }
                throw error;
            }
        },
    ],
    uninstallPackage: [
        (0, global_middleware_1.publicApiScope)('communityPackage:uninstall'),
        async (req, res) => {
            const lifecycle = di_1.Container.get(community_packages_lifecycle_service_1.CommunityPackagesLifecycleService);
            try {
                await lifecycle.uninstall(req.params.name, req.user, 'notFound');
                return res.status(204).send();
            }
            catch (error) {
                if (error instanceof response_error_1.ResponseError) {
                    return sendResponseError(res, error);
                }
                throw error;
            }
        },
    ],
};
//# sourceMappingURL=community-packages.handler.js.map