"use strict";
const api_types_1 = require("@n8n/api-types");
const di_1 = require("@n8n/di");
const n8n_workflow_1 = require("n8n-workflow");
const folder_not_found_error_1 = require("../../../../errors/folder-not-found.error");
const bad_request_error_1 = require("../../../../errors/response-errors/bad-request.error");
const not_found_error_1 = require("../../../../errors/response-errors/not-found.error");
const folder_service_1 = require("../../../../services/folder.service");
const global_middleware_1 = require("../../shared/middlewares/global.middleware");
const utils_service_1 = require("../../shared/services/utils.service");
const folderHandlers = {
    createFolder: [
        (0, global_middleware_1.isLicensed)('feat:folders'),
        (0, global_middleware_1.apiKeyHasScopeWithGlobalScopeFallback)({ scope: 'folder:create' }),
        async (req, res) => {
            const { projectId } = req.params;
            await (0, utils_service_1.assertProjectScope)(req.user, projectId, ['folder:create']);
            const payload = api_types_1.CreateFolderDto.safeParse(req.body);
            if (payload.error) {
                throw new bad_request_error_1.BadRequestError(payload.error.errors[0].message);
            }
            try {
                const folder = await di_1.Container.get(folder_service_1.FolderService).createFolder(payload.data, projectId);
                return res.status(201).json(folder);
            }
            catch (e) {
                if (e instanceof folder_not_found_error_1.FolderNotFoundError)
                    throw new not_found_error_1.NotFoundError(e.message);
                throw e;
            }
        },
    ],
    getFolders: [
        (0, global_middleware_1.isLicensed)('feat:folders'),
        (0, global_middleware_1.apiKeyHasScopeWithGlobalScopeFallback)({ scope: 'folder:list' }),
        async (req, res) => {
            const { projectId } = req.params;
            await (0, utils_service_1.assertProjectScope)(req.user, projectId, ['folder:list']);
            const query = api_types_1.ListFolderQueryDto.safeParse(req.query);
            if (query.error) {
                throw new bad_request_error_1.BadRequestError(query.error.errors[0].message);
            }
            const [data, count] = await di_1.Container.get(folder_service_1.FolderService).getManyAndCount(projectId, query.data);
            return res.json({ count, data });
        },
    ],
    deleteFolder: [
        (0, global_middleware_1.isLicensed)('feat:folders'),
        (0, global_middleware_1.apiKeyHasScopeWithGlobalScopeFallback)({ scope: 'folder:delete' }),
        async (req, res) => {
            const { projectId, folderId } = req.params;
            await (0, utils_service_1.assertProjectScope)(req.user, projectId, ['folder:delete']);
            const query = api_types_1.DeleteFolderDto.safeParse(req.query);
            if (query.error) {
                throw new bad_request_error_1.BadRequestError(query.error.errors[0].message);
            }
            try {
                await di_1.Container.get(folder_service_1.FolderService).deleteFolder(req.user, folderId, projectId, query.data);
                return res.status(204).send();
            }
            catch (e) {
                if (e instanceof folder_not_found_error_1.FolderNotFoundError)
                    throw new not_found_error_1.NotFoundError(e.message);
                if (e instanceof n8n_workflow_1.UserError)
                    throw new bad_request_error_1.BadRequestError(e.message);
                throw e;
            }
        },
    ],
    getFolder: [
        (0, global_middleware_1.isLicensed)('feat:folders'),
        (0, global_middleware_1.apiKeyHasScopeWithGlobalScopeFallback)({ scope: 'folder:read' }),
        async (req, res) => {
            const { projectId } = req.params;
            await (0, utils_service_1.assertProjectScope)(req.user, projectId, ['folder:read']);
            try {
                const { folder, totalSubFolders, totalWorkflows } = await di_1.Container.get(folder_service_1.FolderService).findFolderWithContentCounts(req.params.folderId, projectId);
                return res.json({ ...folder, totalSubFolders, totalWorkflows });
            }
            catch (e) {
                if (e instanceof folder_not_found_error_1.FolderNotFoundError)
                    throw new not_found_error_1.NotFoundError(e.message);
                throw e;
            }
        },
    ],
    updateFolder: [
        (0, global_middleware_1.isLicensed)('feat:folders'),
        (0, global_middleware_1.apiKeyHasScopeWithGlobalScopeFallback)({ scope: 'folder:update' }),
        async (req, res) => {
            const { projectId } = req.params;
            await (0, utils_service_1.assertProjectScope)(req.user, projectId, ['folder:update']);
            const payload = api_types_1.UpdateFolderDto.safeParse(req.body);
            if (payload.error) {
                throw new bad_request_error_1.BadRequestError(payload.error.errors[0].message);
            }
            try {
                const folder = await di_1.Container.get(folder_service_1.FolderService).updateFolder(req.params.folderId, projectId, payload.data);
                return res.json(folder);
            }
            catch (e) {
                if (e instanceof folder_not_found_error_1.FolderNotFoundError)
                    throw new not_found_error_1.NotFoundError(e.message);
                if (e instanceof n8n_workflow_1.UserError)
                    throw new bad_request_error_1.BadRequestError(e.message);
                throw e;
            }
        },
    ],
};
module.exports = folderHandlers;
//# sourceMappingURL=folders.handler.js.map