"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CheckboxListFormControl = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = tslib_1.__importDefault(require("react"));
const core_1 = require("@material-ui/core");
const DataFormControl_1 = require("../DataFormControl");
const StringDataDefinition_1 = require("@appglue/common/dist/Common/Data/Definitions/StringDataDefinition");
const PropertyEditorBoolean_1 = require("../../../CommonUI/PropertyEditing/PropertyEditorBoolean");
const PropertyEditorTextList_1 = require("../../../CommonUI/PropertyEditing/PropertyEditorTextList");
const IDesignValidationProvider_1 = require("@appglue/common/dist/Common/IDesignValidationProvider");
const UIFormControlLabel_1 = require("../../../CommonUI/BuildlingBlocks/UIFormControlLabel");
const UICheckbox_1 = require("../../../CommonUI/BuildlingBlocks/UICheckbox");
const DataUtilities_1 = require("@appglue/common/dist/Common/DataUtilities");
class XCheckboxListEditUI 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)(PropertyEditorTextList_1.PropertyEditorTextList, { editObject: this.props.editMe, propertyName: "list", updateCallback: this.props.editMe.controlUpdate, label: "List" }), (0, jsx_runtime_1.jsx)(PropertyEditorBoolean_1.PropertyEditorBoolean, { editObject: this.props.editMe, propertyName: "requiredOneSelectionAtLeast", updateCallback: this.props.editMe.controlUpdate, label: "Required One Selection At Least" })] }));
    }
}
class CheckboxListFormControl extends DataFormControl_1.DataFormControl {
    __type = "Checkbox List";
    list = [];
    requiredOneSelectionAtLeast = false;
    getRuntimeValidationIssues() {
        const issues = super.getRuntimeValidationIssues();
        const runtimeIssues = this.validate();
        if (runtimeIssues) {
            for (let val of runtimeIssues) {
                issues.push(new IDesignValidationProvider_1.ValidationIssue(val, this.valueName, this.id));
            }
        }
        return issues;
    }
    getDesignValidationIssues() {
        const issues = super.getRuntimeValidationIssues();
        // if duplicates exists
        if (Array.from(new Set(this.list)).length !== this.list.length) {
            issues.push(new IDesignValidationProvider_1.ValidationIssue("Items should be unique", undefined, this.id));
        }
        return issues;
    }
    render() {
        let items = this.list;
        const issueData = this.getFormContext().getControlContext(this).getRuntimeIssueData();
        const issueText = issueData?.text;
        return ((0, jsx_runtime_1.jsxs)(core_1.FormControl, { component: "fieldset", "data-testid": this.valueName, error: !!issueText, children: [(0, jsx_runtime_1.jsx)(core_1.FormLabel, { component: "legend", children: this.label }), (0, jsx_runtime_1.jsx)(core_1.FormGroup, { children: items.map((item, index) => ((0, jsx_runtime_1.jsx)(UIFormControlLabel_1.UIFormControlLabel, { control: (0, jsx_runtime_1.jsx)(UICheckbox_1.StyledCheckBox, { name: item, checked: this.getCheckedByName(item), onChange: this.handleChange, color: "primary" }, index), label: item }, index))) }), (0, jsx_runtime_1.jsx)(core_1.FormHelperText, { children: issueText })] }));
    }
    renderEditUI() {
        return (0, jsx_runtime_1.jsx)(XCheckboxListEditUI, { editMe: this });
    }
    getAddedData() {
        if (!this.valueName)
            return;
        let def = new StringDataDefinition_1.StringDataDefinition();
        def.list = true;
        def.name = this.valueName;
        return def;
    }
    getStorageData() {
        let retData = super.getStorageData();
        retData = DataUtilities_1.DataUtilities.addStorageData(this, retData, "__type", "list", "requiredOneSelectionAtLeast");
        return retData;
    }
    setStorageData(data) {
        super.setStorageData(data);
        DataUtilities_1.DataUtilities.setStorageData(this, data, "__type", "list", "requiredOneSelectionAtLeast");
    }
    handleChange = (event) => {
        if (this.valueName) {
            this.setFormDataValue(this.valueName, { ...this.getValue(), [event.target.name]: event.target.checked });
        }
    };
    getValue = () => {
        let value = {};
        if (this.valueName) {
            value = this.getFormDataValue(this.valueName) || {};
        }
        return value;
    };
    getCheckedByName = (item) => {
        return this.getValue()[item];
    };
    validate = () => {
        let errorMessages = [];
        let value = this.valueName ? this.getFormDataValue(this.valueName) : undefined;
        if (value && this.requiredOneSelectionAtLeast) {
            if (!Object.keys(value).find((k) => !!value[k])) {
                errorMessages.push("Choose one or more options");
            }
        }
        return errorMessages;
    };
}
exports.CheckboxListFormControl = CheckboxListFormControl;
//# sourceMappingURL=CheckboxListFormControl.js.map