"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const Log_1 = require("../core/Log");
const Utilities_1 = require("../core/Utilities");
const Database_1 = require("../minecraft/Database");
const ICondition_1 = require("./ICondition");
const IField_1 = require("./IField");
class FieldUtilities {
    static getFieldValueAsBoolean(name, defaultValue, container) {
        let value = undefined;
        if (container.dataPropertyObject !== undefined) {
            const prop = container.dataPropertyObject.getProperty(name);
            if (prop !== undefined) {
                value = prop.value;
            }
        }
        if (container.getsetPropertyObject !== undefined) {
            value = container.getsetPropertyObject.getProperty(name);
        }
        if (container.directObject !== undefined) {
            value = container.directObject[name];
        }
        if (value === undefined) {
            return defaultValue;
        }
        if (typeof value === "boolean") {
            return value;
        }
        else if (typeof value === "number") {
            if (value === 0) {
                return false;
            }
            else {
                return true;
            }
        }
        else if (typeof value === "string") {
            if (value === "false") {
                return false;
            }
            else {
                return true;
            }
        }
        return defaultValue;
    }
    static async getSubForm(field) {
        if (field.subForm) {
            return field.subForm;
        }
        if (field.subFormId) {
            const subForm = await Database_1.default.ensureFormLoadedByPath(field.subFormId);
            return subForm;
        }
        return undefined;
    }
    static getFieldValue(field, container) {
        let curVal = undefined;
        const dataObj = container.dataPropertyObject;
        if (dataObj !== undefined) {
            let prop = dataObj.getProperty(field.id);
            if (prop === undefined && field.altId !== undefined) {
                prop = dataObj.getProperty(field.altId);
            }
            if (prop !== undefined) {
                curVal = prop.value;
            }
        }
        const gsObj = container.getsetPropertyObject;
        if (gsObj !== undefined) {
            if (field.id === "__scalar") {
                const res = gsObj.getBaseValue();
                if (typeof res === "object") {
                    return undefined;
                }
                return res;
            }
            curVal = gsObj.getProperty(field.id);
            if (curVal === undefined && field.altId !== undefined) {
                curVal = gsObj.getProperty(field.altId);
            }
        }
        const dirObj = container.directObject;
        if (dirObj !== undefined) {
            if (field.id === "__scalar") {
                if (typeof dirObj === "object") {
                    return undefined;
                }
                return dirObj;
            }
            curVal = dirObj[field.id];
        }
        return curVal;
    }
    static getFieldTitle(field) {
        let title = field.id;
        if (field.title !== undefined) {
            title = field.title;
        }
        else {
            title = Utilities_1.default.humanifyMinecraftName(title);
        }
        if (title === undefined) {
            title = "Untitled";
        }
        return title;
    }
    static getFieldId(field) {
        if (field.id) {
            return field.id;
        }
        if (field.title) {
            return field.title;
        }
        return "<no id>";
    }
    static getFieldById(id, form) {
        const fields = form.fields;
        for (let i = 0; i < fields.length; i++) {
            const field = fields[i];
            if (field.id === id) {
                return field;
            }
        }
        return undefined;
    }
    static getStringKeyedFieldType(fieldType) {
        switch (fieldType) {
            case IField_1.FieldDataType.string:
                return IField_1.FieldDataType.keyedStringCollection;
            case IField_1.FieldDataType.number:
                return IField_1.FieldDataType.keyedNumberCollection;
            case IField_1.FieldDataType.stringArray:
                return IField_1.FieldDataType.keyedStringArrayCollection;
            case IField_1.FieldDataType.numberArray:
                return IField_1.FieldDataType.keyedNumberArrayCollection;
            case IField_1.FieldDataType.boolean:
                return IField_1.FieldDataType.keyedBooleanCollection;
            case IField_1.FieldDataType.object:
                return IField_1.FieldDataType.keyedObjectCollection;
        }
        return fieldType;
    }
    static evaluate(form, conditions, container, defaultField) {
        for (let condition of conditions) {
            let field;
            if (condition.field) {
                field = FieldUtilities.getFieldById(condition.field, form);
            }
            if (!field && defaultField) {
                field = defaultField;
            }
            if (!field) {
                Log_1.default.fail("Could not find field '" + condition.field + "'");
                return false;
            }
            const actualVal = FieldUtilities.getFieldValue(field, container);
            if (condition.comparison) {
                const comp = condition.comparison.toLowerCase();
                if (comp === ICondition_1.ComparisonType.equals) {
                    if (condition.value !== undefined && actualVal !== condition.value) {
                        return false;
                    }
                    if (condition.anyValues !== undefined) {
                        let foundMatch = false;
                        for (const val of condition.anyValues) {
                            if (val === actualVal) {
                                foundMatch = true;
                            }
                        }
                        if (!foundMatch) {
                            return false;
                        }
                    }
                }
                else if (comp === ICondition_1.ComparisonType.isDefined && (actualVal === undefined || actualVal === null)) {
                    return false;
                }
                else if (comp === ICondition_1.ComparisonType.isNonEmpty &&
                    (actualVal === undefined || actualVal === null || (typeof actualVal === "string" && actualVal.length <= 0))) {
                    return false;
                }
            }
        }
        return true;
    }
}
exports.default = FieldUtilities;

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