"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get assert () {
        return assert;
    },
    get columnFactory () {
        return columnFactory;
    },
    get extendByMetaType () {
        return extendByMetaType;
    },
    get isNullish () {
        return isNullish;
    }
});
const _object = require("../../helpers/object");
const _errors = require("../../helpers/errors");
const _registry = require("../../cellTypes/registry");
/**
 * Checks if the given property can be overwritten.
 *
 * @param {string} propertyName The property name to check.
 * @param {object} metaObject The current object meta settings.
 * @returns {boolean}
 */ function canBeOverwritten(propertyName, metaObject) {
    if (propertyName === 'CELL_TYPE') {
        return false;
    }
    return metaObject._automaticallyAssignedMetaProps?.has(propertyName) || !(0, _object.hasOwnProperty)(metaObject, propertyName);
}
function extendByMetaType(metaObject, settings, settingsToCompareWith = metaObject) {
    const validType = typeof settings.type === 'string' ? (0, _registry.getCellType)(settings.type) : settings.type;
    if (metaObject._automaticallyAssignedMetaProps) {
        (0, _object.objectEach)(settings, (value, key)=>void metaObject._automaticallyAssignedMetaProps.delete(key));
    }
    if (!(0, _object.isObject)(validType)) {
        return;
    }
    if (settingsToCompareWith === metaObject && !metaObject._automaticallyAssignedMetaProps) {
        metaObject._automaticallyAssignedMetaProps = new Set();
    }
    const expandedType = {};
    (0, _object.objectEach)(validType, (value, property)=>{
        if (canBeOverwritten(property, settingsToCompareWith)) {
            expandedType[property] = value;
            metaObject._automaticallyAssignedMetaProps?.add(property);
        }
    });
    (0, _object.extend)(metaObject, expandedType);
}
function columnFactory(TableMeta, conflictList = []) {
    // Do not use ES6 "class extends" syntax here. It seems that the babel produces code
    // which drastically decreases the performance of the ColumnMeta class creation.
    /**
   * Base "class" for column meta.
   */ function ColumnMeta() {}
    (0, _object.inherit)(ColumnMeta, TableMeta);
    // Clear conflict settings
    for(let i = 0; i < conflictList.length; i++){
        ColumnMeta.prototype[conflictList[i]] = undefined;
    }
    return ColumnMeta;
}
function assert(condition, errorMessage) {
    if (!condition()) {
        (0, _errors.throwWithCause)(`Assertion failed: ${errorMessage}`);
    }
}
function isNullish(variable) {
    return variable === null || variable === undefined;
}
