export function flatten(obj: Record<string, any>, path?: string, result?: Record<string, any>) {
	result = result ?? {};

	for (const [key, value] of Object.entries(obj)) {
		if (typeof value === 'object') {
			flatten(value, (path ?? '') + key + '.', result);
		} else {
			result[(path ?? '') + key] = value;
		}
	}

	return result;
}

export function expand(obj: Record<string, any>) {
	const result: Record<string, any> = {};

	for (const [key, value] of Object.entries(obj)) {
		const elements = key.split('.');

		let current = result;
		for (const element of elements.slice(0, elements.length - 1)) {
			current[element] = current[element] ?? {};
			current = current[element];
		}
		current[elements[elements.length - 1]] = value;
	}

	return result;
}
