import { h } from "@stencil/core";
import { GroupNodeView } from "./group-node-view";
/**
 * Render a `Rule` node and recurse into its children.
 * @param props
 */
export const RuleNodeView = (props) => {
    const { node, path, ctx } = props;
    const group = extractGroup(node);
    if (!group) {
        return null;
    }
    return (h(GroupNodeView, { node: group.node, path: path, isNegated: group.isNegated, ctx: ctx }));
};
const isNegatedGroup = (node) => node.type === 'not' &&
    (node.rule.type === 'all' || node.rule.type === 'any');
/**
 * If `node` is a group (`all`/`any`) or a negated group, return the
 * inner group together with its negation flag. Otherwise `null`.
 * Used by the dispatcher and by the editor's input normalization.
 * @param node
 */
export function extractGroup(node) {
    if (node.type === 'all' || node.type === 'any') {
        return { node, isNegated: false };
    }
    if (isNegatedGroup(node)) {
        return { node: node.rule, isNegated: true };
    }
    if (node.type === 'not') {
        const inner = extractGroup(node.rule);
        if (inner) {
            return { node: inner.node, isNegated: !inner.isNegated };
        }
    }
    return null;
}
export const IssuesView = ({ issues }) => {
    if (issues.length === 0) {
        return null;
    }
    return issues.map((issue) => (h("div", { class: "rule-node__issue" }, issue.message)));
};
