import { h } from "@stencil/core";
export class BceElementMatrix {
    constructor() {
        this.value = this.template
            ? Array(this.template.rows.length).fill(null)
            : [];
        this.handleInput = (event) => {
            event.cancelBubble = true;
            const target = event.target;
            if (!target.value)
                return;
            // console.log(target.value);
            const clone = [...this.value];
            const [row, column] = target.value.split(',');
            const index = parseInt(row, 10);
            // console.log([row, column]);
            const value = column === undefined ? null : parseInt(column, 10);
            clone[index] = value;
            this.value = this.template.rows.map((_, i) => {
                // console.log(clone[i]);
                return clone[i] === undefined ? null : clone[i];
            });
            console.log(this.value);
            this.el.dispatchEvent(new Event('input', { bubbles: true }));
        };
    }
    renderRow(r) {
        if (!this.template)
            return null;
        const value = this.value[r] != null ? `${r},${this.value[r]}` : null;
        return (h("bce-input", { type: "radio", value: value, validation: this.template.required ? 'required' : '', onInput: this.handleInput }, this.template.columns.map((_, c) => (h("bce-option", { value: `${r},${c}` })))));
    }
    render() {
        if (!this.template)
            return null;
        const tl = this.template.columns.map(col => h("label", null, col.text));
        const sl = this.template.rows.map(row => h("label", null, row.text));
        const rb = this.template.rows.map((_, r) => this.renderRow(r));
        return [
            h("p", null, this.template.question),
            h("div", { class: "tl" }, tl),
            h("div", { class: "sl" }, sl),
            h("div", { class: "rb" }, rb)
        ];
    }
    static get is() { return "bce-element-matrix"; }
    static get originalStyleUrls() { return {
        "$": ["bce-element-matrix.scss"]
    }; }
    static get styleUrls() { return {
        "$": ["bce-element-matrix.css"]
    }; }
    static get properties() { return {
        "template": {
            "type": "unknown",
            "mutable": false,
            "complexType": {
                "original": "Block.ElementMatrix.Template",
                "resolved": "undefined | { readonly metadata: ElementMetadata; readonly question: string; readonly required: boolean; readonly rows: SelectElementOption[]; readonly columns: SelectElementOption[]; }",
                "references": {
                    "Block": {
                        "location": "import",
                        "path": "../../../models/block"
                    }
                }
            },
            "required": false,
            "optional": true,
            "docs": {
                "tags": [],
                "text": ""
            }
        },
        "value": {
            "type": "unknown",
            "mutable": false,
            "complexType": {
                "original": "(number | null)[]",
                "resolved": "(number | null)[]",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "defaultValue": "this.template\n    ? Array(this.template.rows.length).fill(null)\n    : []"
        }
    }; }
    static get elementRef() { return "el"; }
}
