var __spreadArrays = (this && this.__spreadArrays) || function () {
    for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
    for (var r = Array(s), k = 0, i = 0; i < il; i++)
        for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
            r[k] = a[j];
    return r;
};
import { r as registerInstance, h, c as getElement } from './index-f5364a73.js';
// Temporary translations
var TRANSLATIONS = {
    match: {
        and: 'Alle',
        or: 'Een'
    },
    matchParagraph: {
        and: 'onderstaande condities zijn waar',
        or: 'van de onderstaande condities is waar'
    },
    check: {
        is: 'is',
        'is-before': 'is voor',
        'is-after': 'is na',
        'is-on-or-before': 'is op of voor',
        'is-on-or-after': 'is op of na',
        'is-empty': 'is leeg',
        'is-not-empty': 'is niet leeg',
        equals: 'is gelijk aan',
        'not-equals': 'is niet gelijk aan',
        contains: 'bevat',
        'not-contains': 'bevat niet',
        'starts-with': 'begint met',
        'not-starts-with': 'begint niet met',
        'ends-with': 'eindigd met',
        'not-ends-with': 'eindigd niet met'
    }
};
var Condition = /** @class */ (function () {
    function Condition(hostRef) {
        var _this = this;
        registerInstance(this, hostRef);
        var _a;
        this.template = [
            { name: 'date', label: 'Date', input: 'date' }
        ];
        this.value = {
            match: 'and',
            condition: [
                { data: ((_a = this.template[0]) === null || _a === void 0 ? void 0 : _a.name) || '', property: '', check: '', value: '' }
            ]
        };
        this.handleAddCondition = function () {
            var condition = __spreadArrays(_this.value.condition, [_this.createRow()]);
            var update = Object.assign(Object.assign({}, _this.value), { condition: condition });
            _this.set(update);
        };
        // private handleAddGroup = () => {
        //   const condition = [...this.value.condition, this.createGroup()];
        //   const update = { ...this.value, condition };
        //   this.set(update);
        // };
        this.handleRemoveCondition = function (_, path) {
            var update = _this.remove(path, _this.value);
            _this.set(update);
        };
        this.handleInput = function (event, path, name) {
            var _b;
            var select = event.target;
            var value = select.value || '';
            var update = _this.update(path, (_b = {}, _b[name] = value, _b), _this.value);
            _this.set(update);
        };
    }
    // private createGroup(): ConditionGroup {
    //   return { match: 'and', condition: [this.createRow()] };
    // }
    Condition.prototype.createRow = function () {
        var _a;
        return {
            data: ((_a = this.template[0]) === null || _a === void 0 ? void 0 : _a.name) || '',
            property: '',
            check: '',
            value: ''
        };
    };
    Condition.prototype.remove = function (path, current) {
        if (!path.length)
            return current;
        var index = path[0];
        var next = current.condition[index];
        var condition = __spreadArrays(current.condition);
        if (path.length === 1 || !this.isGroup(next))
            condition.splice(index, 1);
        else
            condition[index] = this.remove(path.slice(1), next);
        return Object.assign(Object.assign({}, current), { condition: condition });
    };
    Condition.prototype.set = function (value) {
        this.value = value;
        var event = new Event('input', { bubbles: true, composed: true });
        this.el.dispatchEvent(event);
    };
    Condition.prototype.update = function (path, value, current) {
        if (!path.length || !this.isGroup(current))
            return Object.assign(Object.assign({}, current), value);
        var index = path[0];
        var next = current.condition[index];
        var condition = __spreadArrays(current.condition);
        condition[index] = this.update(path.slice(1), value, next);
        return Object.assign(Object.assign({}, current), { condition: condition });
    };
    Condition.prototype.isGroup = function (c) {
        return 'condition' in c;
    };
    Condition.prototype.renderCheck = function (row, path, type) {
        var _this = this;
        var checks = [];
        switch (type) {
            case 'date':
                checks = [
                    'is',
                    'is-before',
                    'is-after',
                    'is-on-or-before',
                    'is-on-or-after',
                    'is-empty',
                    'is-not-empty'
                ];
                break;
            default:
                checks = [
                    'equals',
                    'not-equals',
                    'contains',
                    'not-contains',
                    'starts-with',
                    'not-starts-with',
                    'ends-with',
                    'not-ends-with',
                    'is-empty',
                    'is-not-empty'
                ];
                break;
        }
        return (h("bce-select", { key: row.data, type: "dropdown", value: row.check, onInput: function (e) { return _this.handleInput(e, path, 'check'); } }, checks.map(function (check) { return (h("bce-option", { value: check }, TRANSLATIONS.check[check])); })));
    };
    Condition.prototype.renderInput = function (row, path, template) {
        var _this = this;
        if (!template)
            return;
        var type = typeof template === 'string' ? template : template.type;
        switch (type) {
            // case 'dropdown':
            //   return [
            //     this.renderCheck(row, path, type),
            //     <bce-select
            //       type={type}
            //       value={row.value}
            //       onInput={e => this.handleInput(e, path, 'value')}
            //     />
            //   ];
            default:
                return [
                    this.renderCheck(row, path, type),
                    h("bce-input", { type: type, value: row.value, onInput: function (e) { return _this.handleInput(e, path, 'value'); } })
                ];
        }
    };
    Condition.prototype.renderProperty = function (row, path, template) {
        var _this = this;
        if (!(template === null || template === void 0 ? void 0 : template.length))
            return;
        var current = template.find(function (i) { return i.name === row.property; });
        return [
            h("bce-select", { key: row.data, type: "dropdown", value: row.property, onInput: function (e) { return _this.handleInput(e, path, 'property'); } }, template.map(function (v) { return (h("bce-option", { value: v.name }, v.label)); })),
            current && this.renderInput(row, path, current.input)
        ];
    };
    Condition.prototype.renderRow = function (row, path, deletable) {
        var _this = this;
        var template = this.template.find(function (t) { return t.name === row.data; });
        return (h("li", { key: path.join('-') + '-' + row.data }, h("bce-select", { type: "dropdown", value: row.data, onInput: function (e) { return _this.handleInput(e, path, 'data'); } }, this.template.map(function (v) { return (h("bce-option", { value: v.name }, v.label || v.name)); })), this.renderProperty(row, path, template === null || template === void 0 ? void 0 : template.property), this.renderInput(row, path, template === null || template === void 0 ? void 0 : template.input), deletable && (h("bce-menu", null, h("bce-button", { icon: "fas:trash", onClick: function (e) { return _this.handleRemoveCondition(e, path); } }, "Remove")))));
    };
    Condition.prototype.renderGroup = function (group, path) {
        var _this = this;
        var classes = { multiple: group.condition.length > 1 };
        return (h("ul", { class: classes }, group.condition.length > 1 && (h("header", null, h("bce-select", { type: "dropdown", value: group.match, onInput: function (e) { return _this.handleInput(e, path, 'match'); } }, h("bce-option", { value: "and" }, TRANSLATIONS.match.and), h("bce-option", { value: "or" }, TRANSLATIONS.match.or)), h("p", null, TRANSLATIONS.matchParagraph[group.match]))), group.condition.map(function (row, i) { return _this.isGroup(row)
            ? _this.renderGroup(row, __spreadArrays(path, [i]))
            : _this.renderRow(row, __spreadArrays(path, [i]), group.condition.length > 1); }), h("footer", null, h("bce-button", { design: "text", icon: "fas:plus", onClick: this.handleAddCondition }, "Conditie Toevoegen"))));
    };
    Condition.prototype.render = function () {
        return [
            this.renderGroup(this.value, [])
            // Temporary state preview
            // <br />,
            // <bce-code
            //   language="js"
            //   value={JSON.stringify(this.state, undefined, 2)}
            //   key={JSON.stringify(this.state)}
            // />
        ];
    };
    Object.defineProperty(Condition.prototype, "el", {
        get: function () { return getElement(this); },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Condition, "style", {
        get: function () { return ":host{position:relative;margin:20px 0}:host ul{margin:0;padding-left:0}:host ul header{display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center}:host ul header bce-select{width:80px}:host ul header p{margin:0 10px}:host ul ul{padding-left:82px}:host ul li{display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center}:host ul.multiple li{margin-left:82px}:host ul.multiple footer{margin-left:82px}:host bce-input,:host bce-select{margin:2px}"; },
        enumerable: true,
        configurable: true
    });
    return Condition;
}());
export { Condition as bce_condition };
