'use strict';

var index = require('./index-C_OnxXhP.js');
var index_esm = require('./index.esm-C1i_NocP.js');

const limeQueryFilterComparisonCss = () => `:host(limebb-lime-query-filter-comparison){display:flex;flex-direction:column;gap:0.5rem}.expression-container{position:relative;display:grid;grid-template-columns:repeat(auto-fit, minmax(10rem, 1fr));grid-gap:0.5rem;padding-right:2.75rem}.remove{position:absolute;right:0;top:0.125rem}`;

const LIME_QUERY_OPERATORS = [
    {
        value: index_esm.nt.EQUALS,
        label: 'webclient.operator.equals',
        icon: '-lime-filter-equal',
    },
    {
        value: index_esm.nt.NOT_EQUALS,
        label: 'webclient.operator.not-equals',
        icon: '-lime-filter-equal-not',
    },
    {
        value: index_esm.nt.GREATER,
        label: 'webclient.operator.greater-than',
        icon: '-lime-filter-greater',
        applicableTypes: ['integer', 'decimal', 'date', 'time'],
    },
    {
        value: index_esm.nt.GREATER_OR_EQUAL,
        label: 'webclient.operator.greater-than-or-equal-to',
        icon: '-lime-filter-greater-equal',
        applicableTypes: ['integer', 'decimal', 'date', 'time'],
    },
    {
        value: index_esm.nt.LESS,
        label: 'webclient.operator.less-than',
        icon: '-lime-filter-less',
        applicableTypes: ['integer', 'decimal', 'date', 'time'],
    },
    {
        value: index_esm.nt.LESS_OR_EQUAL,
        label: 'webclient.operator.less-than-or-equal-to',
        icon: '-lime-filter-less-equal',
        applicableTypes: ['integer', 'decimal', 'date', 'time'],
    },
    {
        value: index_esm.nt.IN,
        label: 'webclient.operator.in-filter',
        icon: '-lime-filter-set-included',
    },
    {
        value: index_esm.nt.LIKE,
        label: 'webclient.operator.contains',
        icon: '-lime-filter-contain',
        applicableTypes: ['string', 'text'],
    },
    {
        value: index_esm.nt.BEGINS,
        label: 'webclient.operator.begins-with',
        icon: '-lime-filter-starts-with',
        applicableTypes: ['string', 'text'],
    },
    {
        value: index_esm.nt.ENDS,
        label: 'webclient.operator.ends-with',
        icon: '-lime-filter-ends-with',
        applicableTypes: ['string', 'text'],
    },
];
const LimeQueryFilterComparisonComponent = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.expressionChange = index.createEvent(this, "expressionChange");
        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 === index_esm.nt.IN && !Array.isArray(convertedValue)) {
                // Switching TO IN operator: wrap non-array value in array
                convertedValue =
                    convertedValue !== undefined && convertedValue !== ''
                        ? [convertedValue]
                        : [];
            }
            else if (oldOperator === index_esm.nt.IN &&
                newOperator !== index_esm.nt.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 (index.h(index.Host, { key: '29ddf92b2e83eafe695faeb9a4ce6b8d29aa973e' }, this.renderHeader(), index.h("div", { key: '258f8aeee98a2435d324a1e5b816f111188cf3cc', class: "expression-container" }, this.renderPropertySelector(), this.renderOperator(), this.renderValueInput(), index.h("limel-icon-button", { key: 'c9e4cf456908fb82507e2599af631691e96ab8e4', class: "remove", icon: "trash", label: "Remove condition", onClick: this.removeExpression }))));
    }
    renderHeader() {
        if (!this.label) {
            return;
        }
        return index.h("limel-header", { subheading: this.label });
    }
    renderPropertySelector() {
        return (index.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 (index.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 (index.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(index_esm.n.Translate);
    }
};
LimeQueryFilterComparisonComponent.style = limeQueryFilterComparisonCss();

const limeQueryFilterExpressionCss = () => `@charset "UTF-8";.expression{display:flex;flex-direction:column;margin-bottom:1rem;gap:1rem}`;

const LimeQueryFilterExpressionComponent = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.expressionChange = index.createEvent(this, "expressionChange");
        this.handleExpressionChange = (event) => {
            event.stopPropagation();
            this.expressionChange.emit(event.detail);
        };
    }
    render() {
        if (!this.expression) {
            return;
        }
        const props = {
            platform: this.platform,
            context: this.context,
            label: this.label,
            limetype: this.limetype,
            activeLimetype: this.activeLimetype,
        };
        if ('key' in this.expression) {
            return (index.h("limebb-lime-query-filter-comparison", Object.assign({}, props, { expression: this.expression, onExpressionChange: this.handleExpressionChange })));
        }
        if (this.expression.op === index_esm.nt.AND ||
            this.expression.op === index_esm.nt.OR) {
            return (index.h("limebb-lime-query-filter-group", Object.assign({}, props, { expression: this.expression, onExpressionChange: this.handleExpressionChange })));
        }
        if (this.expression.op === index_esm.nt.NOT) {
            return (index.h("limebb-lime-query-filter-not", Object.assign({}, props, { expression: this.expression, onExpressionChange: this.handleExpressionChange })));
        }
    }
};
LimeQueryFilterExpressionComponent.style = limeQueryFilterExpressionCss();

exports.limebb_lime_query_filter_comparison = LimeQueryFilterComparisonComponent;
exports.limebb_lime_query_filter_expression = LimeQueryFilterExpressionComponent;
