"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const React = __importStar(require("react"));
const PropTypes = __importStar(require("prop-types"));
const utils_1 = require("../../utils");
const AnyToBooleanField_1 = __importDefault(require("./AnyToBooleanField"));
const utils_2 = require("@rjsf/utils");
const components_1 = require("../components");
class MultiAnyToBooleanField extends React.Component {
    constructor(props) {
        super(props);
        this.getStateFromProps = (props) => {
            let { groups } = (0, utils_1.getUiOptions)(props.uiSchema) || [];
            const { formData } = props;
            const trueValues = groups.map(group => group.trueValue);
            const falseValues = groups.map(group => group.falseValue);
            const groupsFormData = [];
            if (formData) {
                formData.forEach(value => {
                    if (value === undefined) {
                        return;
                    }
                    const trueIndex = trueValues.findIndex(trueValue => props.formContext.utils.formDataEquals(value, trueValue, props.idSchema.$id));
                    if (trueIndex !== -1) {
                        groupsFormData[trueIndex] = value;
                    }
                    else {
                        const falseIndex = falseValues.findIndex(falseValue => props.formContext.utils.formDataEquals(value, falseValue, props.idSchema.$id));
                        if (falseIndex !== -1) {
                            groupsFormData[falseIndex] = value;
                        }
                    }
                });
            }
            return { groupsFormData: groupsFormData };
        };
        this.onChange = (index) => (value) => {
            const groupsFormData = this.state.groupsFormData;
            groupsFormData[index] = value;
            this.setState({ groupsFormData: groupsFormData });
            const formData = groupsFormData.reduce((arr, val) => {
                if (val !== undefined) {
                    arr.push(val);
                }
                return arr;
            }, []);
            this.props.onChange(formData);
        };
        this.onSelectAll = () => {
            const { groups } = (0, utils_1.getUiOptions)(this.props.uiSchema) || [];
            const groupsFormData = groups.map(group => group.trueValue);
            this.setState({ groupsFormData });
            const formData = groupsFormData.reduce((arr, val) => {
                if (val !== undefined) {
                    arr.push(val);
                }
                return arr;
            }, []);
            this.props.onChange(formData);
        };
        this.state = this.getStateFromProps(props);
    }
    UNSAFE_componentWillReceiveProps(props) {
        this.setState(this.getStateFromProps(props));
    }
    render() {
        const TitleFieldTemplate = (0, utils_2.getTemplate)("TitleFieldTemplate", this.props.registry, (0, utils_1.getUiOptions)(this.props.uiSchema));
        const { "ui:title": _title } = this.props.uiSchema || {};
        let { groups, selectAll } = (0, utils_1.getUiOptions)(this.props.uiSchema) || [];
        return (React.createElement(React.Fragment, null,
            React.createElement(TitleFieldTemplate, { title: _title !== undefined ? _title : this.props.schema.title, schema: this.props.schema, uiSchema: this.props.uiSchema, registry: this.props.registry }),
            React.createElement("div", { className: "checkbox-row" }, groups.map((group, idx) => {
                const { "ui:help": help, "ui:helpHoverable": helpHoverable, helpPlacement } = group, _group = __rest(group, ["ui:help", "ui:helpHoverable", "helpPlacement"]);
                const groupProps = Object.assign(Object.assign({}, this.props), { schema: Object.assign(Object.assign({}, this.props.schema.items), { title: group.label }), uiSchema: Object.assign(Object.assign({}, this.props.uiSchema), { "ui:options": Object.assign({ falseValue: undefined }, _group), "ui:help": help, "ui:helpHoverable": helpHoverable, "ui:helpPlacement": helpPlacement }), formData: this.state.groupsFormData[idx], onChange: this.onChange(idx) });
                return (React.createElement(React.Fragment, { key: idx },
                    React.createElement(AnyToBooleanField_1.default, Object.assign({}, groupProps))));
            })),
            selectAll && (React.createElement(components_1.Button, { onClick: this.onSelectAll, style: { marginTop: "10px" } }, this.props.formContext.translations.SelectAll))));
    }
}
MultiAnyToBooleanField.propTypes = {
    uiSchema: PropTypes.shape({
        "ui:options": PropTypes.shape({
            groups: PropTypes.arrayOf(PropTypes.shape({
                label: PropTypes.string,
                trueValue: PropTypes.any,
                falseValue: PropTypes.any,
                allowUndefined: PropTypes.bool
            })),
            selectAll: PropTypes.bool
        })
    }),
    schema: PropTypes.shape({
        type: PropTypes.oneOf(["array"])
    }).isRequired,
    formData: PropTypes.array
};
exports.default = MultiAnyToBooleanField;
