import { UnitStateType, UnitTypeEnums } from '../../enums/workflowEnums';
import { DateTime } from 'luxon';
import { formatDateToLocaleString } from '../input/DateUtilities';
export const getWorkflowTaskIconByActionType = (type) => {
    let iconName = 'ClipboardListIcon';
    switch (type) {
        case UnitTypeEnums.Rest:
            iconName = 'CloudImportExportIcon';
            break;
        case UnitTypeEnums.Sms:
            iconName = 'MessageFillIcon';
            break;
        case UnitTypeEnums.Mail:
            iconName = 'MailSolidIcon';
            break;
        case UnitTypeEnums.Scene:
            iconName = 'ReleaseGateIcon';
            break;
        default:
            break;
    }
    return iconName;
};
export const getWorkflowTaskStateIconByStateType = (type) => {
    let iconName = '';
    switch (type) {
        case UnitStateType.Initial:
            iconName = 'StopwatchIcon';
            break;
        case UnitStateType.Started:
            iconName = 'ProgressLoopOuterIcon';
            break;
        case UnitStateType.Completed:
            iconName = 'CheckMarkIcon';
            break;
        case UnitStateType.Cancelled:
            iconName = 'CancelIcon';
            break;
        case UnitStateType.Failed:
        default:
            iconName = 'ErrorBadgeIcon';
            break;
    }
    return iconName;
};
export const getWorkflowTaskStateIconColorByStateType = (type) => {
    let color = '';
    switch (type) {
        case UnitStateType.Initial:
            color = '#ffa726';
            break;
        case UnitStateType.Started:
            color = '#29b6f6';
            break;
        case UnitStateType.Completed:
            color = '#66bb6a';
            break;
        case UnitStateType.Cancelled:
            color = '#f44336';
            break;
        case UnitStateType.Failed:
        default:
            color = '#f44336';
            break;
    }
    return color;
};
const SELECTABLE_NODE_TYPES = [
    UnitTypeEnums.Sms,
    UnitTypeEnums.Mail,
    UnitTypeEnums.Scene
];
export const transformWorkflowPlayTree = (node) => {
    if (!node)
        return null;
    const label = node.name || '';
    const expanded = 'expanded';
    const startedAt = node.startedAt
        ? formatDateToLocaleString(node.startedAt, DateTime.DATETIME_SHORT)
        : null;
    const updatedNode = {
        id: node.id,
        parentId: node.parentId,
        isExpanded: expanded,
        isSelectable: !!node.type && SELECTABLE_NODE_TYPES.includes(node.type),
        properties: {
            defaultIcon: {
                type: '',
                name: getWorkflowTaskIconByActionType(node.type),
                color: ''
            },
            conditionalIcon: {
                type: '',
                name: !!node.type && getWorkflowTaskStateIconByStateType(node.state),
                color: !!node.type && getWorkflowTaskStateIconColorByStateType(node.state)
            },
            nodeLabel: label,
            nodeSubLabel: node.parentId ? node.description : startedAt || '',
            state: node.state,
            type: node.type,
            interruptPages: node.interruptPages,
            playSceneId: node.playSceneId,
            startedAt: node.startedAt,
            endedAt: node.endedAt,
            dueDate: node.dueDate,
            variables: node.variables,
            sms: node.type === UnitTypeEnums.Sms
                ? {
                    templateData: node.templateData,
                    to: node.to,
                    from: node.from,
                    body: node.body
                }
                : null,
            email: node.type === UnitTypeEnums.Mail
                ? {
                    templateData: node.templateData,
                    to: node.to,
                    from: node.from,
                    subject: node.subject,
                    text: node.text,
                    html: node.html
                }
                : null
        },
        children: []
    };
    for (const child of node.children) {
        const updatedChild = transformWorkflowPlayTree(child);
        if (updatedChild) {
            updatedNode.children.push(updatedChild);
        }
    }
    return updatedNode;
};
export const updateWorkflowPlayNodeState = (node, nodeId, nodeState) => {
    if (!node)
        return null;
    // Check if the current node matches the nodeId
    if (node.id === nodeId) {
        node.properties.state = nodeState;
        node.properties.defaultIcon = {
            type: '',
            name: getWorkflowTaskIconByActionType(node.properties.type),
            color: ''
        };
        node.properties.conditionalIcon = {
            type: '',
            name: !!node.properties.type &&
                getWorkflowTaskStateIconByStateType(nodeState),
            color: !!node.properties.type &&
                getWorkflowTaskStateIconColorByStateType(nodeState)
        };
        return node; // Return the updated node
    }
    // Recursively check and update children nodes if any
    if (node.children && node.children.length) {
        for (const child of node.children) {
            const updatedChild = updateWorkflowPlayNodeState(child, nodeId, nodeState);
            if (updatedChild) {
                return node; // Return the updated node
            }
        }
    }
    return null; // If no matching nodeId is found
};
//# sourceMappingURL=workflowUtilities.js.map