"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CheckboxFormControl = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const FormControlLabel_1 = tslib_1.__importDefault(require("@material-ui/core/FormControlLabel"));
const react_1 = tslib_1.__importDefault(require("react"));
const Checkbox_1 = tslib_1.__importDefault(require("@material-ui/core/Checkbox"));
const DataFormControl_1 = require("../DataFormControl");
const BooleanDataDefinition_1 = require("@appglue/common/dist/Common/Data/Definitions/BooleanDataDefinition");
const PropertyEditorBoolean_1 = require("../../../CommonUI/PropertyEditing/PropertyEditorBoolean");
const DataUtilities_1 = require("@appglue/common/dist/Common/DataUtilities");
class XCheckboxEditUI extends react_1.default.Component {
    render() {
        return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [this.props.editMe.renderBaseDataControlEditor(), (0, jsx_runtime_1.jsx)(PropertyEditorBoolean_1.PropertyEditorBoolean, { editObject: this.props.editMe, propertyName: "defaultValue", label: "Default Value", updateCallback: () => {
                        this.props.editMe.setDefaultValue();
                        this.props.editMe.controlUpdate();
                    } })] }));
    }
}
class CheckboxFormControl extends DataFormControl_1.DataFormControl {
    __type = "Checkbox";
    defaultValue = false;
    render() {
        return ((0, jsx_runtime_1.jsx)(FormControlLabel_1.default, { control: (0, jsx_runtime_1.jsx)(Checkbox_1.default, { name: this.label, color: "primary", checked: this.getValue(), onChange: this.handleChange, "data-testid": this.valueName, inputRef: this.ref }), label: this.label }));
    }
    componentDidMount() {
        if (this.valueName) {
            let val = this.getFormDataValue(this.valueName);
            if (val === null || val === undefined) {
                this.setFormDataValue(this.valueName, this.defaultValue);
            }
        }
    }
    setDefaultValue() {
        if (this.valueName) {
            let val = this.getFormDataValue(this.valueName);
            if (val === null || val === undefined) {
                this.setFormDataValue(this.valueName, this.defaultValue);
            }
        }
    }
    getValue() {
        if (this.valueName) {
            let val = this.getFormDataValue(this.valueName);
            if (val === undefined || val === null) {
                return this.defaultValue;
            }
            return val;
        }
        return this.defaultValue;
    }
    renderEditUI() {
        return (0, jsx_runtime_1.jsx)(XCheckboxEditUI, { editMe: this });
    }
    getAddedData() {
        if (!this.valueName)
            return;
        let def = new BooleanDataDefinition_1.BooleanDataDefinition();
        def.name = this.valueName;
        return def;
    }
    getStorageData() {
        let retData = super.getStorageData();
        retData = DataUtilities_1.DataUtilities.addStorageData(this, retData, "__type", "defaultValue");
        return retData;
    }
    setStorageData(data) {
        super.setStorageData(data);
        DataUtilities_1.DataUtilities.setStorageData(this, data, "__type", "defaultValue");
    }
    handleChange = (event) => {
        if (this.valueName) {
            this.setFormDataValue(this.valueName, event.currentTarget.checked);
        }
    };
}
exports.CheckboxFormControl = CheckboxFormControl;
//# sourceMappingURL=CheckboxFormControl.js.map