/**
 * Copyright schukai GmbH and contributors 2023. All Rights Reserved.
 * Node module: @schukai/monster
 * This file is licensed under the AGPLv3 License.
 * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
 */

import { validateObject } from "../types/validate.mjs";

export { deepFreeze };

/**
 * Deep freeze a object
 *
 * @param {object} object object to be freeze
 * @license AGPLv3
 * @since 1.0.0
 * @returns {object}
 * @memberOf Monster.Util
 * @copyright schukai GmbH
 * @throws {TypeError} value is not a object
 */
function deepFreeze(object) {
	validateObject(object);

	// Retrieve the defined property names of the object
	let propNames = Object.getOwnPropertyNames(object);

	// Freeze properties before freezing yourself
	for (const name of propNames) {
		const value = object[name];

		object[name] =
			value && typeof value === "object" ? deepFreeze(value) : value;
	}

	return Object.freeze(object);
}