"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 });
exports.getPropsWithInnerUiSchema = getPropsWithInnerUiSchema;
exports.default = VirtualSchemaField;
const React = __importStar(require("react"));
const utils_1 = require("../utils");
const Context_1 = __importDefault(require("../Context"));
function getPropsWithInnerUiSchema(props) {
    return Object.assign(Object.assign({}, props), { uiSchema: (0, utils_1.getInnerUiSchema)(props.uiSchema) });
}
/**
 * Virtual SchemaFields are components which are just state transforming machines.
 */
function VirtualSchemaField(ComposedComponent) {
    class VirtualSchemaField extends ComposedComponent {
        constructor(...args) {
            super(...args);
            this.UNSAFE_componentWillReceiveProps = (props, nextContext) => {
                if (super.UNSAFE_componentWillReceiveProps) {
                    super.UNSAFE_componentWillReceiveProps(props, nextContext);
                }
                else if (this.getStateFromProps) {
                    const state = this.getStateFromProps(props);
                    if (state)
                        this.setState(state);
                }
            };
            this.getStateFromProps = (props) => {
                const propsWithInnerUiSchema = getPropsWithInnerUiSchema(props);
                const state = super.getStateFromProps ? super.getStateFromProps(propsWithInnerUiSchema, props) : propsWithInnerUiSchema;
                return Object.assign(Object.assign({}, propsWithInnerUiSchema), state);
            };
            this.render = () => {
                if (super.render)
                    return super.render();
                const SchemaField = this.props.registry.fields.SchemaField;
                const filterProps = (props) => [
                    "schema",
                    "uiSchema",
                    "idSchema",
                    "formData",
                    "errorSchema",
                    "registry",
                    "onChange",
                    "disabled",
                    "readonly",
                    "required",
                ].reduce((_props, prop) => {
                    if (prop in props)
                        _props[prop] = props[prop];
                    return _props;
                }, {});
                return (React.createElement(SchemaField, Object.assign({}, filterProps(Object.assign(Object.assign({}, this.props), this.state)))));
            };
            this.getStateFromProps = this.getStateFromProps.bind(this);
            this.state = this.getStateFromProps(args[0]);
            this.render = this.render.bind(this);
        }
        getUiOptions() {
            return (0, utils_1.getUiOptions)(this.props.uiSchema);
        }
    }
    VirtualSchemaField.displayName = (0, utils_1.getReactComponentName)(ComposedComponent);
    if (ComposedComponent.getName) {
        const name = ComposedComponent.getName();
        (0, Context_1.default)("VIRTUAL_SCHEMA_NAMES")[name] = true;
        if (process.env.NODE_ENV !== "production" && ComposedComponent.getName() !== (0, utils_1.getReactComponentName)(ComposedComponent)) {
            console.warn(`${(0, utils_1.getReactComponentName)(ComposedComponent)} getName() doesn't return it's component name! (It returned '${name}')`);
        }
    }
    else {
        console.warn(`${(0, utils_1.getReactComponentName)(ComposedComponent)} is missing static getName() method!`);
    }
    return VirtualSchemaField;
}
