var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { UMB_UFM_CONTEXT } from '../contexts/ufm.context.js';
import { property, state } from '../../../external/lit/index.js';
import { UmbLitElement } from '../../core/lit-element/index.js';
export class UmbUfmElementBase extends UmbLitElement {
    #filterFuncArgs;
    #ufmContext;
    set filters(value) {
        this.#filters = value;
        this.#filterFuncArgs = value
            ?.split('|')
            .filter((item) => item)
            .map((item) => {
            const [alias, ...args] = item.split(':').map((x) => x.trim());
            return { alias, args };
        });
    }
    get filters() {
        return this.#filters;
    }
    #filters;
    constructor() {
        super();
        this.consumeContext(UMB_UFM_CONTEXT, (ufmContext) => {
            this.#ufmContext = ufmContext;
        });
    }
    render() {
        if (!this.isConnected)
            return;
        let values = Array.isArray(this.value) ? this.value : [this.value];
        if (this.#filterFuncArgs) {
            const missing = new Set();
            for (const item of this.#filterFuncArgs) {
                const filter = this.#ufmContext?.getFilterByAlias(item.alias);
                if (filter) {
                    values = values.map((value) => filter(value, ...item.args));
                }
                else {
                    missing.add(item.alias);
                }
            }
            if (missing.size > 0) {
                console.warn(`UFM filters with aliases "${Array.from(missing).join('", "')}" were not found.`);
            }
        }
        return values.join(', ');
    }
}
__decorate([
    property()
], UmbUfmElementBase.prototype, "filters", null);
__decorate([
    state()
], UmbUfmElementBase.prototype, "value", void 0);
