import { h } from "@stencil/core";
import { ElementQuestionBase } from '../../../utils/element-question-base';
import { linker } from '../../../utils/input-linker';
export class BceElementInputTemplate {
    constructor() {
        this.metadata = {};
        this.question = '';
        this.required = false;
        this.multiline = false;
        this.min = null;
        this.max = null;
        this.answer = '';
        this.hasAnswer = !!this.answer;
        this.labelQuestion = 'Formulate your question';
        this.labelRequired = 'Required';
        this.labelMultiline = 'Multiline';
        this.labelMin = 'Min';
        this.labelMax = 'Max';
        this.labelHasAnswer = 'Answer';
        this.labelAnswer = '';
        this.placeholderAnswer = 'Formulate the correct answer';
        this._link = linker(this, this.handleInput);
        this._question = this._link('question');
        this._required = this._link('required');
        this._multiline = this._link('multiline');
        this._min = this._link('min');
        this._max = this._link('max');
        this._answer = this._link('answer');
        this._hasAnswer = this._link('hasAnswer');
    }
    handleInput(name, event) {
        const input = event.target;
        if (!input)
            return;
        const { value } = input;
        if (name === 'answer' && !value)
            this.hasAnswer = false;
        if (name === 'hasAnswer' && !value)
            this.answer = '';
        if (name === 'multiline') {
            this.hasAnswer = false;
            this.answer = '';
        }
        event.cancelBubble = true;
        this[name] = value;
        this.value = this.updateValue({ [name]: value });
        this.el.dispatchEvent(new Event('input', { bubbles: true }));
    }
    updateValue(part = {}) {
        const current = {
            question: this.question,
            required: this.required,
            multiline: this.multiline,
            min: this.min,
            max: this.max,
            answer: this.answer,
            hasAnswer: this.hasAnswer,
            metadata: Object.assign({}, this.metadata)
        };
        return Object.assign({}, current, part);
    }
    componentDidLoad() {
        this.value = this.updateValue();
        this.el.dispatchEvent(new Event('input', { bubbles: true }));
    }
    render() {
        const { multiline } = this;
        return (h(ElementQuestionBase, { question: this._question, required: this._required },
            h("bce-input", Object.assign({ type: "switch" }, this._multiline)),
            h("bce-input", Object.assign({ type: "number" }, this._min)),
            h("bce-input", Object.assign({ type: "number" }, this._max)),
            h("bce-input", Object.assign({ type: "switch" }, this._hasAnswer)),
            this.hasAnswer && (h("bce-input", Object.assign({ type: multiline ? 'textarea' : 'text' }, this._answer)))));
    }
    static get is() { return "bce-element-input-template"; }
    static get properties() { return {
        "value": {
            "type": "unknown",
            "mutable": true,
            "complexType": {
                "original": "InputTemplate",
                "resolved": "undefined | { readonly metadata: ElementMetadata; readonly answer: string; readonly question: string; readonly required: boolean; readonly multiline: boolean; readonly min: number | null; readonly max: number | null; readonly hasAnswer: boolean; }",
                "references": {
                    "InputTemplate": {
                        "location": "local"
                    }
                }
            },
            "required": false,
            "optional": true,
            "docs": {
                "tags": [],
                "text": ""
            }
        },
        "metadata": {
            "type": "unknown",
            "mutable": false,
            "complexType": {
                "original": "Block.ElementMetadata",
                "resolved": "ElementMetadata",
                "references": {
                    "Block": {
                        "location": "import",
                        "path": "../../../models/block"
                    }
                }
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "defaultValue": "{}"
        },
        "question": {
            "type": "string",
            "mutable": true,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "question",
            "reflect": false,
            "defaultValue": "''"
        },
        "required": {
            "type": "boolean",
            "mutable": true,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "required",
            "reflect": false,
            "defaultValue": "false"
        },
        "multiline": {
            "type": "boolean",
            "mutable": true,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "multiline",
            "reflect": false,
            "defaultValue": "false"
        },
        "min": {
            "type": "number",
            "mutable": true,
            "complexType": {
                "original": "number | null",
                "resolved": "null | number",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "min",
            "reflect": false,
            "defaultValue": "null"
        },
        "max": {
            "type": "number",
            "mutable": true,
            "complexType": {
                "original": "number | null",
                "resolved": "null | number",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "max",
            "reflect": false,
            "defaultValue": "null"
        },
        "answer": {
            "type": "string",
            "mutable": true,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "answer",
            "reflect": false,
            "defaultValue": "''"
        },
        "hasAnswer": {
            "type": "boolean",
            "mutable": true,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "has-answer",
            "reflect": false,
            "defaultValue": "!!this.answer"
        },
        "labelQuestion": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "label-question",
            "reflect": false,
            "defaultValue": "'Formulate your question'"
        },
        "labelRequired": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "label-required",
            "reflect": false,
            "defaultValue": "'Required'"
        },
        "labelMultiline": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "label-multiline",
            "reflect": false,
            "defaultValue": "'Multiline'"
        },
        "labelMin": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "label-min",
            "reflect": false,
            "defaultValue": "'Min'"
        },
        "labelMax": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "label-max",
            "reflect": false,
            "defaultValue": "'Max'"
        },
        "labelHasAnswer": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "label-has-answer",
            "reflect": false,
            "defaultValue": "'Answer'"
        },
        "labelAnswer": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "label-answer",
            "reflect": false,
            "defaultValue": "''"
        },
        "placeholderAnswer": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "placeholder-answer",
            "reflect": false,
            "defaultValue": "'Formulate the correct answer'"
        }
    }; }
    static get elementRef() { return "el"; }
}
