import { Operator, } from "@limetech/lime-web-components";
/**
 * Get the subheading text for a filter group based on its operator and expression count
 *
 * @param operator - The group's operator (AND or OR)
 * @param expressionCount - Number of child expressions
 * @returns Subheading text, or empty string if 0 or 1 expressions
 */
export function getFilterGroupSubheading(operator, expressionCount) {
    if (expressionCount <= 1) {
        return '';
    }
    return operator === Operator.AND
        ? 'All of these conditions are met'
        : 'Any of these conditions are met';
}
/**
 * Create a new empty comparison expression
 *
 * @returns A new comparison expression with empty key and value
 */
export function createEmptyComparison() {
    return {
        key: '',
        op: Operator.EQUALS,
        exp: '',
    };
}
/**
 * Create a new nested group with the opposite operator from the parent
 *
 * @param parentOperator - The parent group's operator
 * @returns A new group with the opposite operator and one empty comparison
 */
export function createNestedGroup(parentOperator) {
    const oppositeOp = parentOperator === Operator.AND ? Operator.OR : Operator.AND;
    return {
        op: oppositeOp,
        exp: [createEmptyComparison()],
    };
}
/**
 * Add a new expression to a group immutably
 *
 * @param group - The existing group
 * @param newExpression - The expression to add
 * @returns A new group with the expression added to the end
 */
export function addExpressionToGroup(group, newExpression) {
    return {
        op: group.op,
        exp: [...group.exp, newExpression],
    };
}
/**
 * Toggle a group's operator between AND and OR
 *
 * @param group - The group to toggle
 * @returns A new group with the toggled operator
 */
export function toggleGroupOperator(group) {
    const newOperator = group.op === Operator.AND ? Operator.OR : Operator.AND;
    return {
        op: newOperator,
        exp: [...group.exp],
    };
}
/**
 * Update a child expression in a group, handling deletion and unwrapping
 *
 * This function handles three scenarios:
 * 1. Update: Replace a child expression with a new one
 * 2. Delete: Remove a child (when updatedChild is undefined)
 *    - If last child is deleted, return 'removed' (group should be deleted)
 *    - If deletion leaves one child, return 'unwrapped' (unwrap to that child)
 *    - Otherwise, return 'updated' with the remaining children
 *
 * @param group - The group containing the child
 * @param childIndex - Index of the child to update
 * @param updatedChild - The new child expression, or undefined to delete
 * @returns Result object with type and resulting expression
 */
export function updateChildExpression(group, childIndex, updatedChild) {
    const expressions = [...group.exp];
    if (!updatedChild) {
        // Deletion - remove the child
        expressions.splice(childIndex, 1);
        if (expressions.length === 0) {
            // No children left - remove the entire group
            return {
                type: 'removed',
                expression: undefined,
            };
        }
        if (expressions.length === 1) {
            // One child left - unwrap to that child
            return {
                type: 'unwrapped',
                expression: expressions[0],
            };
        }
        // Multiple children remain - return updated group
        return {
            type: 'updated',
            expression: {
                op: group.op,
                exp: expressions,
            },
        };
    }
    // Update - replace the child
    expressions[childIndex] = updatedChild;
    return {
        type: 'updated',
        expression: {
            op: group.op,
            exp: expressions,
        },
    };
}
