import { Operator, PlatformServiceName, } from "@limetech/lime-web-components";
import { Host, h } from "@stencil/core";
const LIME_QUERY_OPERATORS = [
    {
        value: Operator.EQUALS,
        label: 'webclient.operator.equals',
        icon: '-lime-filter-equal',
    },
    {
        value: Operator.NOT_EQUALS,
        label: 'webclient.operator.not-equals',
        icon: '-lime-filter-equal-not',
    },
    {
        value: Operator.GREATER,
        label: 'webclient.operator.greater-than',
        icon: '-lime-filter-greater',
        applicableTypes: ['integer', 'decimal', 'date', 'time'],
    },
    {
        value: Operator.GREATER_OR_EQUAL,
        label: 'webclient.operator.greater-than-or-equal-to',
        icon: '-lime-filter-greater-equal',
        applicableTypes: ['integer', 'decimal', 'date', 'time'],
    },
    {
        value: Operator.LESS,
        label: 'webclient.operator.less-than',
        icon: '-lime-filter-less',
        applicableTypes: ['integer', 'decimal', 'date', 'time'],
    },
    {
        value: Operator.LESS_OR_EQUAL,
        label: 'webclient.operator.less-than-or-equal-to',
        icon: '-lime-filter-less-equal',
        applicableTypes: ['integer', 'decimal', 'date', 'time'],
    },
    {
        value: Operator.IN,
        label: 'webclient.operator.in-filter',
        icon: '-lime-filter-set-included',
    },
    {
        value: Operator.LIKE,
        label: 'webclient.operator.contains',
        icon: '-lime-filter-contain',
        applicableTypes: ['string', 'text'],
    },
    {
        value: Operator.BEGINS,
        label: 'webclient.operator.begins-with',
        icon: '-lime-filter-starts-with',
        applicableTypes: ['string', 'text'],
    },
    {
        value: Operator.ENDS,
        label: 'webclient.operator.ends-with',
        icon: '-lime-filter-ends-with',
        applicableTypes: ['string', 'text'],
    },
];
/**
 * Lime Query Filter Comparison Component
 *
 * Renders a single comparison expression with three parts:
 * 1. **Property** - The field to compare (e.g., "dealstatus", "company.name")
 * 2. **Operator** - The comparison operator (=, !=, >, <, >=, <=, IN, ?, =?, =$)
 * 3. **Value** - The value to compare against
 *
 * This is the most basic building block of a filter. Examples:
 * - dealstatus = "won"
 * - value > 100000
 * - name ? "Big" (contains "Big")
 * - dealstatus IN ["won", "tender"]
 *
 * The component displays:
 * - A text input for the property (Phase 0 - will be replaced with property-path in Phase 1)
 * - A dropdown for the operator
 * - A value input (text/number based on operator)
 * - A trash icon button to remove this condition
 *
 * @private
 */
export class LimeQueryFilterComparisonComponent {
    constructor() {
        this.handlePropertyChange = (event) => {
            event.stopPropagation();
            this.expressionChange.emit(Object.assign(Object.assign({}, this.expression), { key: event.detail }));
        };
        this.handleOperatorChange = (event) => {
            event.stopPropagation();
            const newOperator = event.detail.value;
            const oldOperator = this.expression.op;
            let convertedValue = this.expression.exp;
            // Convert value when switching between IN and other operators
            // IN operator expects an array, other operators expect a single value
            if (newOperator === Operator.IN && !Array.isArray(convertedValue)) {
                // Switching TO IN operator: wrap non-array value in array
                convertedValue =
                    convertedValue !== undefined && convertedValue !== ''
                        ? [convertedValue]
                        : [];
            }
            else if (oldOperator === Operator.IN &&
                newOperator !== Operator.IN &&
                Array.isArray(convertedValue)) {
                // Switching FROM IN operator: extract first element from array
                convertedValue = convertedValue.length > 0 ? convertedValue[0] : '';
            }
            this.expressionChange.emit({
                key: this.expression.key,
                op: newOperator,
                exp: convertedValue,
            });
        };
        this.handleValueChange = (event) => {
            event.stopPropagation();
            this.expressionChange.emit(Object.assign(Object.assign({}, this.expression), { exp: event.detail }));
        };
        this.removeExpression = () => {
            this.expressionChange.emit(undefined);
        };
    }
    render() {
        return (h(Host, { key: '29ddf92b2e83eafe695faeb9a4ce6b8d29aa973e' }, this.renderHeader(), h("div", { key: '258f8aeee98a2435d324a1e5b816f111188cf3cc', class: "expression-container" }, this.renderPropertySelector(), this.renderOperator(), this.renderValueInput(), h("limel-icon-button", { key: 'c9e4cf456908fb82507e2599af631691e96ab8e4', class: "remove", icon: "trash", label: "Remove condition", onClick: this.removeExpression }))));
    }
    renderHeader() {
        if (!this.label) {
            return;
        }
        return h("limel-header", { subheading: this.label });
    }
    renderPropertySelector() {
        return (h("limebb-property-selector", { platform: this.platform, context: this.context, label: "Property", limetype: this.limetype, value: this.expression.key, required: true, onChange: this.handlePropertyChange }));
    }
    renderOperator() {
        const operatorHasBeenSet = !!this.expression.op;
        const options = LIME_QUERY_OPERATORS.map((op) => {
            var _a, _b;
            return ({
                text: (_b = (_a = this.translator) === null || _a === void 0 ? void 0 : _a.get(op.label)) !== null && _b !== void 0 ? _b : op.label,
                value: op.value,
                icon: op.icon ? { name: op.icon } : undefined,
            });
        });
        const selectedOption = options.find((o) => o.value === this.expression.op);
        return (h("limel-select", { class: "operator", label: "Operator", value: selectedOption, options: options, disabled: !operatorHasBeenSet && !this.expression.key, onChange: this.handleOperatorChange }));
    }
    renderValueInput() {
        if (!this.expression.op) {
            return null;
        }
        return (h("limebb-lime-query-value-input", { platform: this.platform, context: this.context, limetype: this.limetype, activeLimetype: this.activeLimetype, propertyPath: this.expression.key, operator: this.expression.op, value: this.expression.exp, onChange: this.handleValueChange }));
    }
    get translator() {
        var _a;
        return (_a = this.platform) === null || _a === void 0 ? void 0 : _a.get(PlatformServiceName.Translate);
    }
    static get is() { return "limebb-lime-query-filter-comparison"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["lime-query-filter-comparison.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["lime-query-filter-comparison.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
            },
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Optional label for the comparison"
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "label"
            },
            "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": "ComparisonExpression",
                    "resolved": "{ key: string; op: BasicOperator; exp: ExpressionValue; } | { key: string; op: Operator.IN; exp: ExpressionValue[]; } | { type: \"filter\"; key: string; op: Operator.IN; exp: string; }",
                    "references": {
                        "ComparisonExpression": {
                            "location": "global",
                            "id": "global::ComparisonExpression"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The comparison expression to render"
                },
                "getter": false,
                "setter": false
            }
        };
    }
    static get events() {
        return [{
                "method": "expressionChange",
                "name": "expressionChange",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the comparison expression changes (property/operator/value modified)\nEmits undefined when the expression should be removed"
                },
                "complexType": {
                    "original": "ComparisonExpression | undefined",
                    "resolved": "undefined | { key: string; op: BasicOperator; exp: ExpressionValue; } | { key: string; op: Operator.IN; exp: ExpressionValue[]; } | { type: \"filter\"; key: string; op: Operator.IN; exp: string; }",
                    "references": {
                        "ComparisonExpression": {
                            "location": "global",
                            "id": "global::ComparisonExpression"
                        }
                    }
                }
            }];
    }
}
