/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { FormFieldComponentDelegate } from '../core';
import { CHECKBOX_CONSTANTS } from './checkbox-constants';
export class CheckboxComponentDelegate extends FormFieldComponentDelegate {
    constructor(config) {
        super(config);
    }
    get value() {
        return this.checked;
    }
    set value(value) {
        this.checked = value;
    }
    get checked() {
        return this._inputElement.checked;
    }
    set checked(value) {
        this._inputElement.checked = value;
    }
    get disabled() {
        return this._inputElement.disabled;
    }
    set disabled(value) {
        this._inputElement.disabled = value;
    }
    get inputElement() {
        return this._inputElement;
    }
    get labelElement() {
        return this._labelElement;
    }
    onChange(listener) {
        this._inputElement.addEventListener('change', evt => listener(evt.target.checked));
    }
    onFocus(listener) {
        this._inputElement.addEventListener('focus', evt => listener(evt));
    }
    onBlur(listener) {
        this._inputElement.addEventListener('blur', evt => listener(evt));
    }
    setLabel(text) {
        if (text) {
            if (this._labelElement) {
                this._labelElement.textContent = text;
            }
            else {
                this._createLabel(text);
            }
        }
        else if (this._labelElement) {
            this._element.removeChild(this._labelElement);
            this._labelElement = undefined;
        }
    }
    _build() {
        const checkbox = document.createElement(CHECKBOX_CONSTANTS.elementName);
        this._inputElement = this._buildInputElement();
        checkbox.appendChild(this._inputElement);
        return checkbox;
    }
    _configure() {
        var _a;
        if (typeof ((_a = this._config.options) === null || _a === void 0 ? void 0 : _a.label) === 'string') {
            this._createLabel(this._config.options.label);
        }
    }
    _createLabel(text) {
        var _a;
        this._labelElement = document.createElement('label');
        this._labelElement.textContent = text;
        if ((_a = this._config.options) === null || _a === void 0 ? void 0 : _a.id) {
            this._labelElement.setAttribute('for', this._config.options.id);
        }
        this._element.appendChild(this._labelElement);
    }
    _buildInputElement() {
        var _a, _b;
        const inputElement = document.createElement('input');
        inputElement.type = 'checkbox';
        if (((_a = this._config.options) === null || _a === void 0 ? void 0 : _a.checked) !== undefined) {
            inputElement.checked = this._config.options.checked;
        }
        if ((_b = this._config.options) === null || _b === void 0 ? void 0 : _b.id) {
            inputElement.id = this._config.options.id;
        }
        return inputElement;
    }
}
