"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSharedWorkflowIds = getSharedWorkflowIds;
exports.getSharedWorkflow = getSharedWorkflow;
exports.getWorkflowById = getWorkflowById;
exports.createWorkflow = createWorkflow;
exports.deleteWorkflow = deleteWorkflow;
exports.parseTagNames = parseTagNames;
exports.getWorkflowTags = getWorkflowTags;
exports.updateTags = updateTags;
const config_1 = require("@n8n/config");
const db_1 = require("@n8n/db");
const di_1 = require("@n8n/di");
const permissions_1 = require("@n8n/permissions");
const license_1 = require("../../../../license");
const workflow_creation_service_1 = require("../../../../workflows/workflow-creation.service");
const workflow_sharing_service_1 = require("../../../../workflows/workflow-sharing.service");
function insertIf(condition, elements) {
    return condition ? elements : [];
}
async function getSharedWorkflowIds(user, scopes, projectId) {
    if (di_1.Container.get(license_1.License).isSharingEnabled()) {
        return await di_1.Container.get(workflow_sharing_service_1.WorkflowSharingService).getSharedWorkflowIds(user, {
            scopes,
            projectId,
        });
    }
    else {
        return await di_1.Container.get(workflow_sharing_service_1.WorkflowSharingService).getSharedWorkflowIds(user, {
            workflowRoles: ['workflow:owner'],
            projectRoles: [permissions_1.PROJECT_OWNER_ROLE_SLUG],
            projectId,
        });
    }
}
async function getSharedWorkflow(user, workflowId) {
    return await di_1.Container.get(db_1.SharedWorkflowRepository).findOne({
        where: {
            ...(!['global:owner', 'global:admin'].includes(user.role.slug) && { userId: user.id }),
            ...(workflowId && { workflowId }),
        },
        relations: [
            ...insertIf(!di_1.Container.get(config_1.GlobalConfig).tags.disabled, ['workflow.tags']),
            'workflow',
        ],
    });
}
async function getWorkflowById(id) {
    return await di_1.Container.get(db_1.WorkflowRepository).findOne({
        where: { id },
    });
}
async function createWorkflow(user, body) {
    const { projectId, ...rest } = body;
    const workflow = Object.assign(new db_1.WorkflowEntity(), rest);
    return await di_1.Container.get(workflow_creation_service_1.WorkflowCreationService).createWorkflow(user, workflow, {
        projectId,
        publicApi: true,
    });
}
async function deleteWorkflow(workflow) {
    return await di_1.Container.get(db_1.WorkflowRepository).remove(workflow);
}
function parseTagNames(tags) {
    return tags.split(',').map((tag) => tag.trim());
}
async function getWorkflowTags(workflowId) {
    return await di_1.Container.get(db_1.TagRepository).find({
        select: ['id', 'name', 'createdAt', 'updatedAt'],
        where: {
            workflowMappings: {
                ...(workflowId && { workflowId }),
            },
        },
    });
}
async function updateTags(workflowId, newTags) {
    const { manager: dbManager } = di_1.Container.get(db_1.SharedWorkflowRepository);
    await dbManager.transaction(async (transactionManager) => {
        const oldTags = await transactionManager.findBy(db_1.WorkflowTagMapping, { workflowId });
        if (oldTags.length > 0) {
            await transactionManager.delete(db_1.WorkflowTagMapping, oldTags);
        }
        await transactionManager.insert(db_1.WorkflowTagMapping, newTags.map((tagId) => ({ tagId, workflowId })));
    });
}
//# sourceMappingURL=workflows.service.js.map