
function withParentProps(item, parent, childrenKey, prop, currentValue) {
	if (!Array.isArray(parent)) {
		return;
	}

	let currentPropValue = currentValue || parent[prop];

	for (const entry of parent) {
		if (entry[prop]) {
			currentPropValue = entry[prop];
		}

		if (entry === item) {
			return [item, currentPropValue];
		}

		if (entry[childrenKey]) {
			const res = withParentProps(item, entry[childrenKey], childrenKey, prop, currentPropValue);
            if (res[1]) {
              return res;
            }
		}
	}
	return [];
}

module.exports = withParentProps;
