import TableColumnType from "../../../Types/AnalyticsDatabase/TableColumnType";
import OneUptimeDate from "../../../Types/Date";
import BadDataException from "../../../Types/Exception/BadDataException";
import JSONFunctions from "../../../Types/JSONFunctions";
import ObjectID from "../../../Types/ObjectID";
export default class CommonModel {
    get tableColumns() {
        return this._tableColumns;
    }
    set tableColumns(v) {
        this._tableColumns = v;
    }
    setColumnValue(columnName, value) {
        const column = this.getTableColumn(columnName);
        if (column) {
            if (column.type === TableColumnType.ObjectID &&
                (typeof value === "string" || typeof value === "object")) {
                value = new ObjectID(value);
            }
            if ((column.type === TableColumnType.Date ||
                column.type === TableColumnType.DateTime64) &&
                typeof value === "string") {
                value = OneUptimeDate.fromString(value);
            }
            if (column.type === TableColumnType.JSON && typeof value === "string") {
                try {
                    value = JSONFunctions.parse(value);
                }
                catch (_a) {
                    value = {};
                }
            }
            if (column.type === TableColumnType.JSONArray &&
                typeof value === "string") {
                try {
                    value = JSONFunctions.parseJSONArray(value);
                    if (!Array.isArray(value)) {
                        throw new BadDataException("Not an array");
                    }
                }
                catch (_b) {
                    value = [];
                }
            }
            if (column.type === TableColumnType.Number && typeof value === "string") {
                value = parseInt(value);
            }
            // decimal
            if (column.type === TableColumnType.Decimal &&
                typeof value === "string") {
                value = parseFloat(value);
            }
            // long number
            if (column.type === TableColumnType.LongNumber &&
                typeof value === "string") {
                value = parseInt(value);
            }
            return (this.data[columnName] = value);
        }
        throw new BadDataException("Column " + columnName + " does not exist");
    }
    constructor(data) {
        this.data = {};
        this._tableColumns = [];
        this.tableColumns = data.tableColumns;
    }
    getColumnValue(columnName) {
        if (this.getTableColumn(columnName)) {
            return this.data[columnName];
        }
        return undefined;
    }
    getTableColumn(name) {
        const column = this.tableColumns.find((column) => {
            return column.key === name;
        });
        if (!column) {
            return null;
        }
        return column;
    }
    getTableColumns() {
        return this.tableColumns;
    }
    static fromJSON(json, type) {
        if (Array.isArray(json)) {
            const arr = [];
            for (const item of json) {
                if (item instanceof CommonModel) {
                    arr.push(item);
                    continue;
                }
                arr.push(new type().fromJSON(item));
            }
            return arr;
        }
        if (json instanceof CommonModel) {
            return json;
        }
        return new type().fromJSON(json);
    }
    static toJSON(model, _modelType) {
        return model.toJSON();
    }
    fromJSON(json) {
        for (const key in json) {
            this.setColumnValue(key, json[key]);
        }
        return this;
    }
    toJSON() {
        const json = {};
        this.tableColumns.forEach((column) => {
            const recordValue = this.getColumnValue(column.key);
            if (recordValue instanceof CommonModel) {
                json[column.key] = recordValue.toJSON();
                return;
            }
            json[column.key] = recordValue;
        });
        return JSONFunctions.serialize(json);
    }
    static fromJSONArray(jsonArray, modelType) {
        const models = [];
        for (const json of jsonArray) {
            if (json instanceof CommonModel) {
                models.push(json);
                continue;
            }
            const model = new modelType();
            model.fromJSON(json);
            models.push(model);
        }
        return models;
    }
    static toJSONArray(models, modelType) {
        const json = [];
        models.forEach((model) => {
            json.push(this.toJSON(model, modelType));
        });
        return json;
    }
}
//# sourceMappingURL=CommonModel.js.map