var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
    if (!privateMap.has(receiver)) {
        throw new TypeError("attempted to get private field on non-instance");
    }
    return privateMap.get(receiver);
};
var _handler;
import { h } from "@stencil/core";
import { InputHandler } from '../../utils/input-handler';
import { getTranslator } from '../../utils/translator';
export class BceElement {
    constructor() {
        this.languages = [];
        this.t = getTranslator();
        _handler.set(this, new InputHandler(this.el));
        this.handleInput = (event) => {
            this.value = event.target.value;
            event.stopPropagation();
            const e = new globalThis.Event('input', { bubbles: true, composed: true });
            this.el.dispatchEvent(e);
        };
    }
    watchLanguage() {
        __classPrivateFieldGet(this, _handler).setLanguage(this.languages, this.language || '');
    }
    async validate(silent = false) {
        const VALIDATION_ELEMENTS = [
            'checkbox',
            'input',
            'matrix',
            'radio',
            'slider'
        ].map(type => `bce-element-${type}`);
        const root = this.el.shadowRoot;
        const query = root.querySelectorAll(VALIDATION_ELEMENTS.join(', '));
        const inputs = Array.from(query).filter(el => 'validate' in el);
        const inputErr = await Promise.all(inputs.map(el => el.validate(silent)));
        const icons = Array.from(root.querySelectorAll('bce-element-icon'));
        const iconErr = await Promise.all(icons.map(el => el.validate(silent)));
        return [].concat(...[...inputErr, ...iconErr]);
    }
    componentWillLoad() {
        this.watchLanguage();
    }
    render() {
        const BceElement = `bce-element-${this.type}`;
        return (h(BceElement, { allowNull: this.allowNull, disabled: this.disabled, handler: __classPrivateFieldGet(this, _handler), language: this.language, shuffle: this.shuffle, template: this.template, value: this.value, onInput: this.handleInput }));
    }
    static get is() { return "bce-element"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() { return {
        "$": ["bce-element.scss"]
    }; }
    static get styleUrls() { return {
        "$": ["bce-element.css"]
    }; }
    static get properties() { return {
        "allowNull": {
            "type": "boolean",
            "mutable": false,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean | undefined",
                "references": {}
            },
            "required": false,
            "optional": true,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "allow-null",
            "reflect": false
        },
        "disabled": {
            "type": "boolean",
            "mutable": false,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean | undefined",
                "references": {}
            },
            "required": false,
            "optional": true,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "disabled",
            "reflect": true
        },
        "language": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string | undefined",
                "references": {}
            },
            "required": false,
            "optional": true,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "language",
            "reflect": false
        },
        "languages": {
            "type": "unknown",
            "mutable": false,
            "complexType": {
                "original": "BlockLanguages",
                "resolved": "{ key: string; label: string; }[]",
                "references": {
                    "BlockLanguages": {
                        "location": "import",
                        "path": "../../models/block-languages"
                    }
                }
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "defaultValue": "[]"
        },
        "shuffle": {
            "type": "boolean",
            "mutable": false,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean | undefined",
                "references": {}
            },
            "required": false,
            "optional": true,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "shuffle",
            "reflect": true
        },
        "template": {
            "type": "unknown",
            "mutable": false,
            "complexType": {
                "original": "BlockElement",
                "resolved": "AudioElement | CheckboxElement | DownloadElement | HeaderElement | IconElement | ImageElement | InputElement | LinkElement | MatrixElement | RadioElement | SliderElement | SortElement | TableElement | TextElement | VideoElement",
                "references": {
                    "BlockElement": {
                        "location": "import",
                        "path": "../../models/element"
                    }
                }
            },
            "required": true,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            }
        },
        "t": {
            "type": "unknown",
            "mutable": false,
            "complexType": {
                "original": "I18n.Translator<string>",
                "resolved": "Translator<string>",
                "references": {
                    "I18n": {
                        "location": "import",
                        "path": "@bce-technology/i18n"
                    }
                }
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "defaultValue": "getTranslator()"
        },
        "type": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "BlockElement.Type",
                "resolved": "\"audio\" | \"checkbox\" | \"download\" | \"header\" | \"icon\" | \"image\" | \"input\" | \"link\" | \"matrix\" | \"radio\" | \"slider\" | \"sort\" | \"table\" | \"text\" | \"video\"",
                "references": {
                    "BlockElement": {
                        "location": "import",
                        "path": "../../models/element"
                    }
                }
            },
            "required": true,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "type",
            "reflect": true
        },
        "value": {
            "type": "any",
            "mutable": true,
            "complexType": {
                "original": "any",
                "resolved": "any",
                "references": {}
            },
            "required": false,
            "optional": true,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "value",
            "reflect": false
        }
    }; }
    static get methods() { return {
        "validate": {
            "complexType": {
                "signature": "(silent?: boolean) => Promise<ValidatorError[]>",
                "parameters": [{
                        "tags": [],
                        "text": ""
                    }],
                "references": {
                    "Promise": {
                        "location": "global"
                    },
                    "ValidatorError": {
                        "location": "import",
                        "path": "@bcase/core"
                    }
                },
                "return": "Promise<ValidatorError[]>"
            },
            "docs": {
                "text": "",
                "tags": []
            }
        }
    }; }
    static get elementRef() { return "el"; }
    static get watchers() { return [{
            "propName": "language",
            "methodName": "watchLanguage"
        }, {
            "propName": "languages",
            "methodName": "watchLanguage"
        }]; }
}
_handler = new WeakMap();
