"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
const React = __importStar(require("react"));
const utils_1 = require("../../utils");
const utils_2 = require("@luomus/laji-map/lib/utils");
const ContextInjectionField_1 = require("./ContextInjectionField");
const core_1 = require("@rjsf/core");
const types_1 = require("../../types");
class _SchemaField extends React.Component {
    applyFunction(props) {
        let _a = (props.uiSchema || {}), { "ui:functions": functions, "ui:childFunctions": childFunctions, "ui:annotations": annotations, "ui:multiLanguage": multiLanguage } = _a, _uiSchema = __rest(_a, ["ui:functions", "ui:childFunctions", "ui:annotations", "ui:multiLanguage"]);
        functions = functions ? (0, utils_1.asArray)(functions) : [];
        if (childFunctions) {
            functions = [
                { "ui:field": "UiFieldMapperArrayField", "ui:options": { functions: childFunctions } },
                ...functions
            ];
        }
        if (annotations) {
            functions = [
                { "ui:field": "AnnotationField", "ui:options": ((0, utils_2.isObject)(annotations) ? annotations : {}) },
                ...functions
            ];
        }
        if (multiLanguage) {
            functions = [
                { "ui:field": "MultiLanguageField" },
                ...functions
            ];
        }
        if (!functions.length)
            return props;
        let _props = Object.assign(Object.assign({}, props), { uiSchema: _uiSchema });
        let [uiFn, ...restUiFns] = functions;
        const { "ui:injections": injections } = uiFn;
        if (injections) {
            const injectedUiSchema = (0, ContextInjectionField_1.getInjectedUiSchema)(uiFn, injections, props.registry.formContext.uiSchemaContext);
            uiFn = Object.assign(Object.assign({}, injectedUiSchema), { "ui:injections": undefined });
        }
        const buttons = !_props.uiSchema["ui:field"] ? (0, utils_1.getUiOptions)(_props.uiSchema).buttons : undefined;
        const uiButtons = !_props.uiSchema["ui:field"] ? (0, utils_1.getUiOptions)(_props.uiSchema)["ui:buttons"] : undefined;
        _props = Object.assign(Object.assign({}, _props), { uiSchema: (_props.uiSchema["ui:field"] || _props.uiSchema["ui:widget"])
                ? Object.assign(Object.assign({}, uiFn), { uiSchema: _props.uiSchema }) : Object.assign(Object.assign({}, _props.uiSchema), uiFn) });
        if (buttons || uiButtons) {
            _props = Object.assign(Object.assign({}, _props), { uiSchema: Object.assign(Object.assign({}, _props.uiSchema), { "ui:options": Object.assign(Object.assign({}, _props.uiSchema["ui:options"]), { buttons: [...(_props.uiSchema.buttons || []), ...(buttons || [])], "ui:buttons": [...(_props.uiSchema["ui:buttons"] || []), ...(uiButtons || [])] }) }) });
        }
        return Object.assign(Object.assign({}, _props), { uiSchema: Object.assign(Object.assign({}, _props.uiSchema), { "ui:functions": restUiFns }) });
    }
    render() {
        var _a, _b;
        const props = this.applyFunction(this.props);
        let { schema, uiSchema = {}, formContext, registry } = props, _props = __rest(props, ["schema", "uiSchema", "formContext", "registry"]);
        const { formContext: _formContext } = registry;
        // rjsf displays a duplicate label if 'uniqueItems' is true in some cases. We prevent that here.
        // Example of when it shows duplicate is http://localhost:8083/?id=JX.652&local=true, "Elinympäristö" on gathering
        // level.
        if ((0, types_1.isJSONSchemaArray)(schema) && ((_a = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema.items) === null || _a === void 0 ? void 0 : _a["ui:field"])) {
            schema = Object.assign(Object.assign({}, schema), { uniqueItems: false });
        }
        const options = (0, utils_1.getUiOptions)(uiSchema);
        if (typeof options.label === "string") {
            schema = Object.assign(Object.assign({}, schema), { title: options.label });
        }
        const { "ui:injections": injections } = uiSchema;
        if (injections) {
            const injectedUiSchema = (0, ContextInjectionField_1.getInjectedUiSchema)(uiSchema, injections, _formContext.uiSchemaContext);
            uiSchema = Object.assign(Object.assign({}, injectedUiSchema), { "ui:injections": undefined });
        }
        // Remove unnecessary formDataTransformers in order to prevent unnecessary rendering.
        if (_formContext && _formContext.formDataTransformers && _formContext.formDataTransformers.length) {
            const removeIdxs = /\d+_?/g;
            const idWithoutIdxs = this.props.idSchema.$id.replace(removeIdxs, "");
            const filtered = _formContext.formDataTransformers.filter(({ targets }) => targets.some(target => target.replace(removeIdxs, "").startsWith(idWithoutIdxs)));
            if (filtered.length !== _formContext.formDataTransformers.length) {
                registry = Object.assign(Object.assign({}, registry), { formContext: Object.assign(Object.assign({}, _formContext), { formDataTransformers: filtered }) });
            }
        }
        const idPointer = props.idSchema.$id ? (0, utils_1.idSchemaIdToJSONPointer)(props.idSchema.$id) : undefined;
        if (idPointer && ((_b = _formContext.uiSchemaContext.additionalClassNames) === null || _b === void 0 ? void 0 : _b[idPointer])) {
            const additionalClassNames = _formContext.uiSchemaContext.additionalClassNames[idPointer];
            uiSchema = Object.assign(Object.assign({}, uiSchema), { "ui:classNames": (0, utils_1.classNames)(uiSchema["ui:classNames"], additionalClassNames) });
        }
        const { SchemaField } = (0, core_1.getDefaultRegistry)().fields;
        return React.createElement(SchemaField, Object.assign({}, _props, { registry: registry, schema: schema, uiSchema: uiSchema }));
    }
}
exports.default = _SchemaField;
