import { h } from "@stencil/core";
import { Operator, } from "@limetech/lime-web-components";
/**
 * Lime Query Filter Builder
 *
 * Entry component for building Lime Query filter expressions.
 * This component wraps the expression router and adds promotion logic
 * for handling Add buttons and expression structure.
 *
 * Unlike the expression router (which is pure routing), this component
 * contains business logic for promoting expressions and managing the
 * filter structure.
 *
 * @private
 */
export class LimeQueryFilterBuilderComponent {
    constructor() {
        this.handleAddFirstCondition = () => {
            const newExpression = {
                key: '',
                op: Operator.EQUALS,
                exp: '',
            };
            this.expressionChange.emit(newExpression);
        };
        this.handlePromoteAndAdd = () => {
            if (!this.expression) {
                return;
            }
            // Wrap current expression in AND and add a new comparison
            const newExpression = {
                op: Operator.AND,
                exp: [
                    this.expression,
                    {
                        key: '',
                        op: Operator.EQUALS,
                        exp: '',
                    },
                ],
            };
            this.expressionChange.emit(newExpression);
        };
        this.handleExpressionChange = (event) => {
            var _a;
            event.stopPropagation();
            this.expressionChange.emit((_a = event.detail) !== null && _a !== void 0 ? _a : undefined);
        };
    }
    render() {
        if (!this.expression) {
            return this.renderEmptyState();
        }
        // Check if we need to show promotion UI (comparison or NOT)
        if (this.needsPromotion()) {
            return this.renderWithPromotionButton();
        }
        // AND/OR groups handle their own Add buttons
        return (h("limebb-lime-query-filter-expression", { platform: this.platform, context: this.context, limetype: this.limetype, activeLimetype: this.activeLimetype, expression: this.expression, onExpressionChange: this.handleExpressionChange }));
    }
    needsPromotion() {
        if (!this.expression) {
            return false;
        }
        // Basic comparison needs promotion
        if ('key' in this.expression) {
            return true;
        }
        // NOT needs promotion
        return this.expression.op === Operator.NOT;
    }
    renderEmptyState() {
        return (h("limel-button", { label: "Condition", icon: {
                name: 'plus_math',
                title: 'Add',
            }, onClick: this.handleAddFirstCondition }));
    }
    renderWithPromotionButton() {
        return (h("div", { class: "expression-with-promotion" }, h("limebb-lime-query-filter-expression", { platform: this.platform, context: this.context, limetype: this.limetype, activeLimetype: this.activeLimetype, expression: this.expression, onExpressionChange: this.handleExpressionChange }), h("limel-button", { label: "Add another condition", icon: "plus_math", onClick: this.handlePromoteAndAdd })));
    }
    static get is() { return "limebb-lime-query-filter-builder"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["lime-query-filter-builder.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["lime-query-filter-builder.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": "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"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The filter expression to build"
                },
                "getter": false,
                "setter": false
            }
        };
    }
    static get events() {
        return [{
                "method": "expressionChange",
                "name": "expressionChange",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the filter expression changes\nEmits undefined when expression should be removed"
                },
                "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"
                        }
                    }
                }
            }];
    }
}
