import { h, Host, } from "@stencil/core";
import { Operator, } from "@limetech/lime-web-components";
import { addExpressionToGroup, createEmptyComparison, createNestedGroup, getFilterGroupSubheading, toggleGroupOperator, updateChildExpression, } from "./filter-group-logic";
/**
 * Lime Query Filter Group Component
 *
 * Renders a group of filter expressions with AND/OR logic that can be toggled.
 * Users can click the header to toggle between "All conditions" (AND) and
 * "Any condition" (OR).
 *
 * This component displays:
 * - A clickable header showing the current logic (AND/OR)
 * - A list of child expressions (which can be comparisons or nested groups)
 * - An "Add" button to add more expressions
 *
 * Clicking the header toggles between AND and OR logic.
 * If a child component emits an ExpressionChange event with undefined, the
 * child is removed from the group.
 *
 * @private
 */
export class LimeQueryFilterGroupComponent {
    constructor() {
        this.options = [
            {
                text: 'All',
                secondaryText: 'AND operator',
                value: 'and',
            },
            {
                text: 'Any',
                secondaryText: 'OR operator',
                value: 'or',
            },
        ];
        this.renderChildExpression = (expression, childIndex) => (h("limebb-lime-query-filter-expression", { role: "listitem", platform: this.platform, context: this.context, limetype: this.limetype, activeLimetype: this.activeLimetype, expression: expression, onExpressionChange: this.handleExpressionChange(childIndex) }));
        this.handleDeleteGroup = () => {
            this.expressionChange.emit();
        };
        this.handleToggleOperator = (event) => {
            const selectedOption = Array.isArray(event.detail)
                ? event.detail[0]
                : event.detail;
            this.value = selectedOption;
            const newOp = selectedOption.value === 'and' ? Operator.AND : Operator.OR;
            if (newOp !== this.expression.op) {
                const newGroup = toggleGroupOperator(this.expression);
                this.expressionChange.emit(newGroup);
            }
        };
        this.handleAddChildExpression = () => {
            const newChild = createEmptyComparison();
            const newGroup = addExpressionToGroup(this.expression, newChild);
            this.expressionChange.emit(newGroup);
        };
        this.handleAddChildGroup = () => {
            const newChild = createNestedGroup(this.expression.op);
            const newGroup = addExpressionToGroup(this.expression, newChild);
            this.expressionChange.emit(newGroup);
        };
        this.handleExpressionChange = (updatedChildIndex) => (event) => {
            event.stopPropagation();
            const updatedExpression = event.detail;
            const result = updateChildExpression(this.expression, updatedChildIndex, updatedExpression);
            this.expressionChange.emit(result.expression);
        };
    }
    // Initialize value to match current operator
    componentWillLoad() {
        this.value =
            this.options.find((option) => option.value ===
                (this.expression.op === Operator.AND ? 'and' : 'or')) || this.options[0];
    }
    render() {
        return (h(Host, { key: '4b1647c741b78253b4ce4ed1bd347800da394563', style: {
                '--limebb-lime-query-filter-group-operator': `"${this.expression.op === Operator.AND ? 'AND' : 'OR'}"`,
            } }, h("div", { key: '47fd3000275e17ef70d86af5e6abcb492806ffba', class: "expression" }, this.renderHeader(), h("ul", { key: '224249a4b305eb2b20c1aedad98d2c3191d47e83' }, this.expression.exp.map(this.renderChildExpression), this.renderAddButton()), this.renderAddGroupButton())));
    }
    renderHeader() {
        const subheading = this.getSubheading();
        if (!subheading) {
            return null;
        }
        return (h("limel-header", { subheading: subheading }, this.renderOperators(), this.renderDeleteGroupButton()));
    }
    renderOperators() {
        return (h("limel-select", { class: "operator-select", slot: "actions", value: this.value, options: this.options, onChange: this.handleToggleOperator }));
    }
    renderDeleteGroupButton() {
        return (h("limel-icon-button", { class: "delete-group", slot: "actions", icon: "trash", label: "Delete This Group", onClick: this.handleDeleteGroup }));
    }
    getSubheading() {
        return getFilterGroupSubheading(this.expression.op, this.expression.exp.length);
    }
    renderAddButton() {
        return (h("limel-button", { role: "listitem", label: "Condition", icon: {
                name: 'plus_math',
                title: 'Add',
            }, onClick: this.handleAddChildExpression }));
    }
    renderAddGroupButton() {
        return (h("limel-button", { class: "add-group", label: "Group", icon: {
                name: 'plus_math',
                title: 'Add',
            }, onClick: this.handleAddChildGroup }));
    }
    static get is() { return "limebb-lime-query-filter-group"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["lime-query-filter-group.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["lime-query-filter-group.css"]
        };
    }
    static get properties() {
        return {
            "platform": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "LimeWebComponentPlatform",
                    "resolved": "LimeWebComponentPlatform",
                    "references": {
                        "LimeWebComponentPlatform": {
                            "location": "import",
                            "path": "@limetech/lime-web-components",
                            "id": "node_modules::LimeWebComponentPlatform",
                            "referenceLocation": "LimeWebComponentPlatform"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Platform service provider"
                },
                "getter": false,
                "setter": false
            },
            "context": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "LimeWebComponentContext",
                    "resolved": "LimeWebComponentContext",
                    "references": {
                        "LimeWebComponentContext": {
                            "location": "import",
                            "path": "@limetech/lime-web-components",
                            "id": "node_modules::LimeWebComponentContext",
                            "referenceLocation": "LimeWebComponentContext"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Component context"
                },
                "getter": false,
                "setter": false
            },
            "limetype": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The limetype being queried (e.g., \"deal\", \"company\")"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "limetype"
            },
            "activeLimetype": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The limetype of the active object (for %activeObject% placeholders)"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "active-limetype"
            },
            "expression": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "AndOrExpression",
                    "resolved": "{ op: Operator.AND | Operator.OR; exp: Expression[]; }",
                    "references": {
                        "AndOrExpression": {
                            "location": "import",
                            "path": "@limetech/lime-web-components",
                            "id": "node_modules::AndOrExpression",
                            "referenceLocation": "AndOrExpression"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The group expression (AND or OR)"
                },
                "getter": false,
                "setter": false
            }
        };
    }
    static get states() {
        return {
            "value": {}
        };
    }
    static get events() {
        return [{
                "method": "expressionChange",
                "name": "expressionChange",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the group expression changes\nEmits undefined when expression should be removed\nCan emit any Expression type when unwrapping (single child)"
                },
                "complexType": {
                    "original": "Expression | undefined",
                    "resolved": "undefined | { key: string; op: BasicOperator; exp: ExpressionValue; } | { key: string; op: Operator.IN; exp: ExpressionValue[]; } | { op: Operator.AND | Operator.OR; exp: Expression[]; } | { op: Operator.NOT; exp: Expression; } | { type: \"filter\"; key: string; op: Operator.IN; exp: string; }",
                    "references": {
                        "Expression": {
                            "location": "import",
                            "path": "@limetech/lime-web-components",
                            "id": "node_modules::Expression",
                            "referenceLocation": "Expression"
                        }
                    }
                }
            }];
    }
}
