import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import React from "react";
import { FormGroup, FormControl, FormLabel, FormHelperText } from "@material-ui/core";
import { BaseDataControl } from "./BaseDataControl";
import { StringDataDefinition } from "../../Common/Data/Definitions/StringDataDefinition";
import { PropertyEditorBoolean } from "../../CommonUI/PropertyEditing/PropertyEditorBoolean";
import { PropertyEditorTextList } from "../../CommonUI/PropertyEditing/PropertyEditorTextList";
import { ValidationIssue } from "../../Common/IDesignValidationProvider";
import { StyledFormControlLabel } from "../../CommonUI/Components/FormControlLabel";
import { StyledCheckBox } from "../../CommonUI/Components/Checkbox";
class XCheckboxListEditUI extends React.Component {
    render() {
        return (_jsxs(_Fragment, { children: [this.props.editMe.renderBaseDataControlEditor(), _jsx(PropertyEditorTextList, { editObject: this.props.editMe, propertyName: "list", updateCallback: this.props.editMe.controlUpdate, label: "List" }, void 0),
                _jsx(PropertyEditorBoolean, { editObject: this.props.editMe, propertyName: "requiredOneSelectionAtLeast", updateCallback: this.props.editMe.controlUpdate, label: "Required One Selection At Least" }, void 0)] }, void 0));
    }
}
export class XCheckboxList extends BaseDataControl {
    constructor() {
        super(...arguments);
        this.list = [];
        this.requiredOneSelectionAtLeast = false;
        this.handleChange = (event) => {
            if (this.valueName) {
                this.setFormDataValue(this.valueName, Object.assign(Object.assign({}, this.getValue()), { [event.target.name]: event.target.checked }));
            }
        };
        this.getValue = () => {
            let value = {};
            if (this.valueName) {
                value = this.getFormDataValue(this.valueName) || {};
            }
            return value;
        };
        this.getCheckedByName = (item) => {
            return this.getValue()[item];
        };
        this.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;
        };
    }
    getRuntimeValidationIssues() {
        const issues = super.getRuntimeValidationIssues();
        const runtimeIssues = this.validate();
        if (runtimeIssues) {
            for (let val of runtimeIssues) {
                issues.push(new 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 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 === null || issueData === void 0 ? void 0 : issueData.text;
        return (_jsxs(FormControl, Object.assign({ component: "fieldset", "data-testid": this.valueName, error: !!issueText }, { children: [_jsx(FormLabel, Object.assign({ component: "legend" }, { children: this.label }), void 0),
                _jsx(FormGroup, { children: items.map((item, index) => (_jsx(StyledFormControlLabel, { control: _jsx(StyledCheckBox, { name: item, checked: this.getCheckedByName(item), onChange: this.handleChange, color: "primary" }, index), label: item }, index))) }, void 0),
                _jsx(FormHelperText, { children: issueText }, void 0)] }), void 0));
    }
    renderEditUI() {
        return _jsx(XCheckboxListEditUI, { editMe: this }, void 0);
    }
    getAddedData() {
        if (!this.valueName)
            return;
        let def = new StringDataDefinition();
        def.list = true;
        def.name = this.valueName;
        return def;
    }
}
//# sourceMappingURL=XCheckboxList.js.map