"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProcessorFixupLevel = void 0;
const Utilities_1 = require("../core/Utilities");
const IField_1 = require("./IField");
var ProcessorFixupLevel;
(function (ProcessorFixupLevel) {
    ProcessorFixupLevel[ProcessorFixupLevel["none"] = 0] = "none";
    ProcessorFixupLevel[ProcessorFixupLevel["perField"] = 1] = "perField";
    ProcessorFixupLevel[ProcessorFixupLevel["full"] = 9] = "full";
})(ProcessorFixupLevel = exports.ProcessorFixupLevel || (exports.ProcessorFixupLevel = {}));
class DataFormProcessor {
    static async process(obj, form, fixupLevel, path, issues) {
        if (path === undefined) {
            path = "";
        }
        else {
            path += ".";
        }
        if (issues === undefined) {
            issues = [];
        }
        for (const key in obj) {
            const val = obj[key];
            let foundField = false;
            for (const field of form.fields) {
                if (field.id === key) {
                    foundField = true;
                    if (field.dataType === IField_1.FieldDataType.objectArray && field.subForm) {
                        if (val && Array.isArray(val)) {
                            for (const obj of val) {
                                await DataFormProcessor.process(obj, field.subForm, fixupLevel, path + key, issues);
                            }
                        }
                    }
                    if (field.dataType === IField_1.FieldDataType.keyedObjectCollection && field.subForm) {
                        if (val && typeof val === "object") {
                            for (const subKey in val) {
                                const subObj = val[subKey];
                                await DataFormProcessor.process(subObj, field.subForm, fixupLevel, path + key + "." + subKey, issues);
                            }
                        }
                    }
                    else if ((field.dataType === IField_1.FieldDataType.object && field.subForm) ||
                        field.dataType === IField_1.FieldDataType.minecraftEventTrigger ||
                        field.dataType === IField_1.FieldDataType.minecraftFilter) {
                        if (val && typeof val === "object") {
                            let keyCount = 0;
                            for (const key in obj) {
                                if (key) {
                                    keyCount++;
                                }
                            }
                            if (keyCount === 0 &&
                                (field.undefinedIfEmpty ||
                                    field.dataType === IField_1.FieldDataType.minecraftEventTrigger ||
                                    field.dataType === IField_1.FieldDataType.minecraftFilter)) {
                                issues.push({
                                    subject: key,
                                    message: "Object is defined but empty; should be undefined.",
                                    path: path,
                                });
                                if (fixupLevel === ProcessorFixupLevel.perField ||
                                    fixupLevel === ProcessorFixupLevel.full ||
                                    field.dataType === IField_1.FieldDataType.minecraftEventTrigger ||
                                    field.dataType === IField_1.FieldDataType.minecraftFilter) {
                                    if (Utilities_1.default.isUsableAsObjectKey(key)) {
                                        obj[key] = undefined;
                                    }
                                }
                            }
                            else if (field.dataType === IField_1.FieldDataType.object && field.subForm) {
                                await DataFormProcessor.process(val, field.subForm, fixupLevel, path + key, issues);
                            }
                        }
                    }
                    else if (field.dataType === IField_1.FieldDataType.keyedStringArrayCollection ||
                        field.dataType === IField_1.FieldDataType.keyedKeyedStringArrayCollection) {
                        if (val && typeof val === "object") {
                            let keyCount = 0;
                            for (const key in val) {
                                if (key) {
                                    keyCount++;
                                }
                            }
                            if (keyCount === 0 && field.undefinedIfEmpty) {
                                issues.push({
                                    subject: key,
                                    message: "Object is defined but empty; should be undefined.",
                                    path: path,
                                });
                                if (fixupLevel === ProcessorFixupLevel.perField || fixupLevel === ProcessorFixupLevel.full) {
                                    obj[key] = undefined;
                                }
                            }
                        }
                    }
                }
            }
            if (!foundField) {
                issues.push({
                    subject: key,
                    message: "Could not find field in data definition.",
                    path: path,
                });
                if (fixupLevel === ProcessorFixupLevel.full) {
                    obj[key] = undefined;
                }
            }
        }
    }
}
exports.default = DataFormProcessor;

//# sourceMappingURL=../maps/dataform/DataFormProcessor.js.map
