"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 __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 NestField_1 = require("./NestField");
const ReactContext_1 = __importDefault(require("../../ReactContext"));
const utils_2 = require("@rjsf/utils");
class SplitField extends React.Component {
    constructor() {
        super(...arguments);
        this.renderSplitField = ({ fields }) => {
            const { SchemaField } = this.props.registry.fields;
            // TODO TS fix typing after NestField ts conversion.
            const _props = (0, NestField_1.getPropsForFields)(this.props, fields);
            return (React.createElement(SchemaField, Object.assign({}, this.props, _props, { onChange: this.onChange(fields), name: "", onBlur: this.props.onBlur, onFocus: this.props.onFocus, disabled: this.props.disabled, readonly: this.props.readonly })));
        };
        this.onChange = (fields) => (formData) => {
            this.props.onChange(fields.reduce((updatedFormData, field) => {
                return Object.assign(Object.assign({}, updatedFormData), { [field]: formData[field] });
            }, this.props.formData));
        };
    }
    render() {
        var _a;
        const TitleFieldTemplate = (0, utils_2.getTemplate)("TitleFieldTemplate", this.props.registry, (0, utils_1.getUiOptions)(this.props.uiSchema));
        const DescriptionFieldTemplate = (0, utils_2.getTemplate)("DescriptionFieldTemplate", this.props.registry, (0, utils_1.getUiOptions)(this.props.uiSchema));
        const { "ui:title": _title } = this.props.uiSchema || {};
        const { Row, Col } = this.context.theme;
        return (React.createElement("div", null,
            React.createElement(TitleFieldTemplate, { id: `${this.props.idSchema.$id}__title`, title: _title !== undefined ? _title : this.props.title, required: this.props.required || ((_a = this.props.uiSchema) === null || _a === void 0 ? void 0 : _a["ui:required"]), schema: this.props.schema, uiSchema: this.props.uiSchema, registry: this.props.registry }),
            React.createElement(DescriptionFieldTemplate, { id: `${this.props.idSchema.$id}__description`, schema: this.props.schema, description: this.props.description, registry: this.props.registry }),
            React.createElement(Row, null, (0, utils_1.getUiOptions)(this.props.uiSchema).splits.map((split, i) => React.createElement(Col, { md: split.md, lg: split.lg, xs: split.xs, sm: split.sm, key: i }, this.renderSplitField(split))))));
    }
}
SplitField.contextType = ReactContext_1.default;
SplitField.propTypes = {
    uiSchema: PropTypes.shape({
        "ui:options": PropTypes.shape({
            splits: PropTypes.arrayOf(PropTypes.shape({
                fields: PropTypes.arrayOf(PropTypes.string).isRequired,
                uiSchema: PropTypes.object,
                name: PropTypes.string,
                lg: PropTypes.number,
                md: PropTypes.number,
                sm: PropTypes.number,
                xs: PropTypes.number
            })).isRequired
        }).isRequired
    }),
    schema: PropTypes.shape({
        type: PropTypes.oneOf(["object"])
    }).isRequired,
    formData: PropTypes.object.isRequired
};
exports.default = SplitField;
