"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FormGenerator = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const React = tslib_1.__importStar(require("react"));
const lodash_1 = tslib_1.__importDefault(require("lodash"));
const AnyOf_1 = tslib_1.__importDefault(require("./components/Fields/AnyOf/AnyOf.js"));
const Array_1 = tslib_1.__importDefault(require("./components/Fields/Array/Array.js"));
const Boolean_1 = tslib_1.__importDefault(require("./components/Fields/Boolean/Boolean.js"));
const Number_1 = tslib_1.__importDefault(require("./components/Fields/Number/Number.js"));
const Object_1 = tslib_1.__importDefault(require("./components/Fields/Object/Object.js"));
const OneOf_1 = tslib_1.__importDefault(require("./components/Fields/OneOf/OneOf.js"));
const Select_1 = tslib_1.__importDefault(require("./components/Fields/Select/Select.js"));
const Text_1 = tslib_1.__importDefault(require("./components/Fields/Text/Text.js"));
const TextArea_1 = tslib_1.__importDefault(require("./components/Fields/TextArea/TextArea.js"));
const cn_1 = require("./utils/cn.js");
const common_1 = require("./utils/common.js");
const b = (0, cn_1.formGeneratorCn)('form-generator');
const FormGenerator = ({ blockConfig, onUpdateByKey, onUpdate, contentConfig, }) => {
    const inputs = blockConfig;
    const onDataUpdate = React.useCallback((key, value) => {
        if (onUpdateByKey) {
            onUpdateByKey(key, value);
        }
        if (onUpdate && contentConfig) {
            const newContentConfig = lodash_1.default.cloneDeep(contentConfig);
            lodash_1.default.set(newContentConfig, key, value);
            onUpdate(newContentConfig);
        }
    }, [onUpdateByKey, onUpdate, contentConfig]);
    const getData = React.useCallback((variable) => {
        if (variable.startsWith('block.')) {
            const purePath = variable.replace('block.', '');
            return lodash_1.default.get(contentConfig, purePath);
        }
        if ((variable.startsWith(`'`) && variable.endsWith(`'`)) ||
            (variable.startsWith(`"`) && variable.endsWith(`"`))) {
            // @ts-ignore TODO: replaceAll types
            return variable.replaceAll(`'`, '').replaceAll(`"`, '');
        }
        return undefined;
    }, [contentConfig]);
    const decide = React.useCallback((showIf) => {
        const parts = showIf.split(' ');
        if (!(parts.length === 3)) {
            // eslint-disable-next-line no-console
            console.log('Something bad happened in showIf, ignored');
            return true;
        }
        const [firstVariable, equals, secondVariable] = parts;
        const data1 = getData(firstVariable);
        const data2 = getData(secondVariable);
        if (equals === '===') {
            return data1 === data2;
        }
        else {
            return data1 !== data2;
        }
    }, [getData]);
    const renderInput = React.useCallback((input) => {
        const fieldPath = input.name;
        const fieldValue = (0, common_1.getContent)(contentConfig, input.name);
        if (input.showIf) {
            const decision = decide(input.showIf);
            if (!decision) {
                return (0, jsx_runtime_1.jsxs)("div", { children: ["Hidden Field: ", input.name] });
            }
        }
        // Text, Select, Boolean and etc
        const onSimpleDynamicFieldUpdate = (value) => {
            onDataUpdate(fieldPath, value);
        };
        // Array and Objects
        const onComplexDynamicFieldUpdate = (key, value) => {
            onDataUpdate((0, common_1.getFullPath)(fieldPath, key), value);
        };
        switch (input.type) {
            case 'text': {
                return ((0, jsx_runtime_1.jsx)(Text_1.default, { onRefresh: (value) => onDataUpdate(fieldPath, value), title: input.title, value: fieldValue, onUpdate: onSimpleDynamicFieldUpdate }));
            }
            case 'boolean': {
                return ((0, jsx_runtime_1.jsx)(Boolean_1.default, { onRefresh: (value) => onDataUpdate(fieldPath, value), title: input.title, value: fieldValue, onUpdate: onSimpleDynamicFieldUpdate }));
            }
            case 'textarea': {
                return ((0, jsx_runtime_1.jsx)(TextArea_1.default, { onRefresh: (value) => onDataUpdate(fieldPath, value), title: input.title, value: fieldValue, onUpdate: onSimpleDynamicFieldUpdate }));
            }
            case 'select': {
                return ((0, jsx_runtime_1.jsx)(Select_1.default, { onRefresh: (value) => onDataUpdate(fieldPath, value), input: input, value: fieldValue, onUpdate: onSimpleDynamicFieldUpdate }));
            }
            case 'number': {
                return ((0, jsx_runtime_1.jsx)(Number_1.default, { onRefresh: (value) => onDataUpdate(fieldPath, value), title: input.title, value: fieldValue, onUpdate: onSimpleDynamicFieldUpdate }));
            }
            case 'object': {
                if (!input || !('properties' in input)) {
                    return null;
                }
                return ((0, jsx_runtime_1.jsx)(Object_1.default, { onRefresh: (value) => onDataUpdate(fieldPath, value), blockConfig: input.properties, title: input.title, value: fieldValue, onUpdate: onComplexDynamicFieldUpdate }));
            }
            case 'array': {
                return ((0, jsx_runtime_1.jsx)(Array_1.default, { blockConfig: input, title: input.title, values: fieldValue, onUpdate: onComplexDynamicFieldUpdate }));
            }
            case 'oneOf': {
                if (!input || !('options' in input)) {
                    return null;
                }
                return ((0, jsx_runtime_1.jsx)(OneOf_1.default, { inputConfig: input, contentConfig: contentConfig, onUpdate: onComplexDynamicFieldUpdate }));
            }
            case 'anyOf': {
                if (!input || !('options' in input)) {
                    return null;
                }
                return ((0, jsx_runtime_1.jsx)(AnyOf_1.default, { inputConfig: input, contentConfig: contentConfig, onUpdate: onComplexDynamicFieldUpdate }));
            }
            default: {
                return (0, jsx_runtime_1.jsxs)("div", { children: ["Ignore ", JSON.stringify(input)] });
            }
        }
    }, [contentConfig, decide, onDataUpdate]);
    const sortedInputs = inputs.sort((x, y) => {
        const nestingFieldTypes = ['object', 'array', 'oneOf', 'anyOf'];
        if (nestingFieldTypes.includes(x.type)) {
            return 1;
        }
        if (nestingFieldTypes.includes(y.type)) {
            return -1;
        }
        return 0;
    });
    return ((0, jsx_runtime_1.jsx)("div", { className: b(), children: sortedInputs.map((input, index) => ((0, jsx_runtime_1.jsx)(React.Fragment, { children: renderInput(input) }, index))) }));
};
exports.FormGenerator = FormGenerator;
exports.default = exports.FormGenerator;
//# sourceMappingURL=FormGenerator.js.map
