"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const Database_1 = require("./../minecraft/Database");
const Log_1 = require("./../core/Log");
const IField_1 = require("../dataform/IField");
class ComponentProperty {
    id;
    _item;
    _form;
    _field;
    isCoreProp = false;
    isInstanceProp = false;
    constructor(item, id) {
        this._item = item;
        this.id = id;
    }
    load() {
        if (Database_1.default.uxCatalog === null || this._form !== undefined) {
            return;
        }
        const result = Database_1.default.getComponentFormField(this.id);
        if (result !== undefined) {
            this._form = result.form;
            if (this._form.id === "entityCore") {
                this.isCoreProp = true;
            }
            else if (this._form.id === "entityInstance") {
                this.isInstanceProp = true;
            }
            this._field = result.field;
        }
        else {
            Log_1.default.debugAlert("Form field " + this.id + " not found.");
        }
    }
    get type() {
        if (this._field !== undefined) {
            return this._field.dataType;
        }
        return IField_1.FieldDataType.string;
    }
    get defaultValue() {
        if (this._field === undefined) {
            Log_1.default.fail("Unbacked component property '" + this.id + "' found.");
            return undefined;
        }
        return this._field.defaultValue;
    }
    get value() {
        if (this._field === undefined) {
            Log_1.default.fail("Unbacked component property '" + this.id + "' found.");
            return undefined;
        }
        if (this.isCoreProp && this._field.groupId !== undefined) {
            const componentId = this._field.groupId;
            const component = this._item.getComponent(componentId);
            if (this._field.dataType === IField_1.FieldDataType.boolean) {
                if (component !== undefined) {
                    return true;
                }
                else {
                    return false;
                }
            }
            else if (this._field.dataType === IField_1.FieldDataType.number ||
                this._field.dataType === IField_1.FieldDataType.int ||
                this._field.dataType === IField_1.FieldDataType.intBoolean ||
                this._field.dataType === IField_1.FieldDataType.intValueLookup) {
                if (component !== undefined) {
                    const val = component.value;
                    return val;
                }
            }
        }
        else if (!this.isCoreProp && this._form !== undefined && this._field !== undefined && this._form.id) {
            const componentId = this._form.id;
            const component = this._item.getComponent(componentId);
            if (component !== undefined && typeof component !== "object") {
                return component[this._field.id];
            }
        }
        return this.defaultValue;
    }
    asBoolean(defaultVal) {
        const val = this.value;
        if (val === null || val === undefined) {
            return defaultVal;
        }
        if (typeof val === "boolean") {
            return val;
        }
        else if (typeof val === "number") {
            return val !== 0;
        }
        else if (typeof val === "string") {
            return val === "true" || val === "1";
        }
        return defaultVal;
    }
    asString(defaultVal) {
        const val = this.value;
        if (val === null || val === undefined) {
            return defaultVal;
        }
        return val.toString();
    }
    asNumber(defaultVal) {
        const val = this.value;
        if (val === null || val === undefined) {
            return defaultVal;
        }
        if (typeof val === "number") {
            return val;
        }
        else if (typeof val === "string") {
            return parseInt(val);
        }
        else if (typeof val === "boolean") {
            if (val) {
                return 1;
            }
            else {
                return 0;
            }
        }
        return defaultVal;
    }
    set value(newValue) {
        const val = this.value;
        if (val !== newValue) {
        }
    }
}
exports.default = ComponentProperty;

//# sourceMappingURL=../maps/minecraft/ComponentProperty.js.map
