"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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
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 VirtualSchemaField_1 = __importDefault(require("../VirtualSchemaField"));
const utils_1 = require("../../utils");
const functionPropType = PropTypes.shape({
    "ui:field": PropTypes.string.isRequired,
    "ui:options": PropTypes.object
});
let UiFieldApplierField = class UiFieldApplierField extends React.Component {
    constructor(props) {
        super(props);
        this.getFunctions = () => {
            let { functions } = this.getUiOptions();
            if (!Array.isArray(functions)) {
                functions = [functions];
            }
            return functions;
        };
        this.onChange = (formData) => {
            const { field } = this.getUiOptions();
            this.onChangeFormData = formData;
            this.functions[this.functions.length - 1].fn.onChange((0, utils_1.parseJSONPointer)(formData, field));
        };
        this.functions = [];
    }
    static getName() { return "UiFieldApplierField"; }
    getStateFromProps(props) {
        if (this.functions.length !== this.getFunctions().length) {
            this.functions = this.functions.slice(0, this.getFunctions().length);
        }
        let { field } = this.getUiOptions();
        const state = this.getFunctions().reduce((props, options, idx) => {
            let { "ui:field": uiField, "ui:options": uiOptions } = options;
            const uiSchemaForField = (0, utils_1.parseJSONPointer)(props.uiSchema, (0, utils_1.uiSchemaJSONPointer)(props.schema, field));
            const propsForField = {
                schema: (0, utils_1.parseJSONPointer)(props.schema, (0, utils_1.schemaJSONPointer)(props.schema, field)),
                uiSchema: Object.assign(Object.assign({}, uiSchemaForField), { "ui:options": Object.assign(Object.assign({}, (0, utils_1.getUiOptions)(uiSchemaForField)), uiOptions) }),
                formData: (0, utils_1.parseJSONPointer)(props.formData, field, !!"safe"),
                idSchema: (0, utils_1.parseJSONPointer)(props.idSchema, field),
                errorSchema: (0, utils_1.parseJSONPointer)(props.errorSchema, field, !!"safe") || {},
                registry: props.registry,
                formContext: props.formContext,
                onChange: this.functions[idx] && this.functions[idx].uiField === uiField
                    ? this.functions[idx].fn.props.onChange
                    : (formData) => {
                        idx === 0
                            ? this.combinedOnChange(formData)
                            : this.functions[idx - 1].fn.onChange(formData);
                    }
            };
            let fn, state;
            if (this.functions[idx] && this.functions[idx].uiField === uiField) {
                fn = this.functions[idx].fn;
                state = fn.getStateFromProps(propsForField);
            }
            else {
                fn = new props.registry.fields[uiField](propsForField);
                this.functions[idx] = { uiField, fn };
                state = fn.state || {};
            }
            let computedProps = {};
            if ("schema" in state) {
                computedProps.schema = (0, utils_1.updateSafelyWithJSONPointer)(props.schema, state.schema, (0, utils_1.schemaJSONPointer)(props.schema, field));
            }
            if ("uiSchema" in state) {
                computedProps.uiSchema = (0, utils_1.updateSafelyWithJSONPointer)(props.uiSchema, state.uiSchema, (0, utils_1.uiSchemaJSONPointer)(props.schema, field));
            }
            ["formData", "idSchema", "errorSchema"].forEach(prop => {
                if (prop in state) {
                    computedProps[prop] = (0, utils_1.updateSafelyWithJSONPointer)(props[prop] || {}, state[prop], field);
                }
            });
            return Object.assign(Object.assign({}, props), computedProps);
        }, props);
        state.onChange = this.onChange;
        return state;
    }
    combinedOnChange(formData) {
        const { field } = this.getUiOptions();
        this.props.onChange((0, utils_1.updateSafelyWithJSONPointer)(this.onChangeFormData, formData, field));
    }
};
UiFieldApplierField.propTypes = {
    schema: PropTypes.shape({
        type: PropTypes.oneOf(["object"])
    }).isRequired,
    uiSchema: PropTypes.shape({
        "ui:options": PropTypes.shape({
            field: PropTypes.string,
            functions: PropTypes.oneOfType([functionPropType, PropTypes.arrayOf(functionPropType)])
        })
    }),
    formData: PropTypes.object
};
UiFieldApplierField = __decorate([
    VirtualSchemaField_1.default
], UiFieldApplierField);
exports.default = UiFieldApplierField;
