"use strict";
const api_types_1 = require("@n8n/api-types");
const di_1 = require("@n8n/di");
const bad_request_error_1 = require("../../../../errors/response-errors/bad-request.error");
const conflict_error_1 = require("../../../../errors/response-errors/conflict.error");
const data_table_service_1 = require("../../../../modules/data-table/data-table.service");
const data_table_column_name_conflict_error_1 = require("../../../../modules/data-table/errors/data-table-column-name-conflict.error");
const data_table_system_column_name_conflict_error_1 = require("../../../../modules/data-table/errors/data-table-system-column-name-conflict.error");
const global_middleware_1 = require("../../shared/middlewares/global.middleware");
module.exports = {
    listDataTableColumns: [
        (0, global_middleware_1.publicApiScope)('dataTableColumn:read'),
        (0, global_middleware_1.projectScope)('dataTable:readColumn', 'dataTable'),
        async (req, res) => {
            const { dataTableId } = req.params;
            const projectId = await di_1.Container.get(data_table_service_1.DataTableService).getProjectIdForDataTable(dataTableId);
            return res.json(await di_1.Container.get(data_table_service_1.DataTableService).getColumns(dataTableId, projectId));
        },
    ],
    createDataTableColumn: [
        (0, global_middleware_1.publicApiScope)('dataTableColumn:create'),
        (0, global_middleware_1.projectScope)('dataTable:writeColumn', 'dataTable'),
        async (req, res) => {
            const { dataTableId } = req.params;
            const payload = api_types_1.AddDataTableColumnDto.safeParse(req.body);
            if (!payload.success) {
                throw new bad_request_error_1.BadRequestError(payload.error.errors[0]?.message);
            }
            const projectId = await di_1.Container.get(data_table_service_1.DataTableService).getProjectIdForDataTable(dataTableId);
            try {
                const column = await di_1.Container.get(data_table_service_1.DataTableService).addColumn(dataTableId, projectId, payload.data);
                return res.status(201).json(column);
            }
            catch (error) {
                if (error instanceof data_table_column_name_conflict_error_1.DataTableColumnNameConflictError ||
                    error instanceof data_table_system_column_name_conflict_error_1.DataTableSystemColumnNameConflictError) {
                    throw new conflict_error_1.ConflictError(error.message);
                }
                throw error;
            }
        },
    ],
    deleteDataTableColumn: [
        (0, global_middleware_1.publicApiScope)('dataTableColumn:delete'),
        (0, global_middleware_1.projectScope)('dataTable:writeColumn', 'dataTable'),
        async (req, res) => {
            const { dataTableId, columnId } = req.params;
            const projectId = await di_1.Container.get(data_table_service_1.DataTableService).getProjectIdForDataTable(dataTableId);
            await di_1.Container.get(data_table_service_1.DataTableService).deleteColumn(dataTableId, projectId, columnId);
            return res.status(204).send();
        },
    ],
    updateDataTableColumn: [
        (0, global_middleware_1.publicApiScope)('dataTableColumn:update'),
        (0, global_middleware_1.projectScope)('dataTable:writeColumn', 'dataTable'),
        async (req, res) => {
            try {
                const { dataTableId, columnId } = req.params;
                const payload = api_types_1.updateDataTableColumnSchema.safeParse(req.body);
                if (!payload.success) {
                    throw new bad_request_error_1.BadRequestError(payload.error.errors[0]?.message);
                }
                const { name, index } = payload.data;
                const service = di_1.Container.get(data_table_service_1.DataTableService);
                const projectId = await service.getProjectIdForDataTable(dataTableId);
                if (name !== undefined) {
                    await service.renameColumn(dataTableId, projectId, columnId, { name });
                }
                if (index !== undefined) {
                    await service.moveColumn(dataTableId, projectId, columnId, { targetIndex: index });
                }
                const updatedColumn = await service.getColumnById({ projectId, dataTableId, columnId });
                return res.json(updatedColumn);
            }
            catch (error) {
                if (error instanceof data_table_column_name_conflict_error_1.DataTableColumnNameConflictError ||
                    error instanceof data_table_system_column_name_conflict_error_1.DataTableSystemColumnNameConflictError) {
                    throw new conflict_error_1.ConflictError(error.message);
                }
                throw error;
            }
        },
    ],
};
//# sourceMappingURL=data-tables.columns.handler.js.map