#include "moverlay.h"

#include "widget_utils/layout_utils.h"

void moverlay::arrange_children(const geometry_t& in_allotted_geometry) {
	const auto& container_size = in_allotted_geometry.get_local_size();
	if (container_size.x() <= 0 || container_size.y() <= 0) {
		return;
	}

	for (auto& slot: get_slots()) {
		const auto& child_widget = slot.get();
		if (!child_widget) {
			continue;
		}

		if (!has_any_flag(child_widget->get_visibility(), visibility_t::any_layout)) {
			continue;
		}

		arrange_single_child(in_allotted_geometry, child_widget, slot.h_alignment(), slot.v_alignment(), slot.margin());
	}
}

auto moverlay::compute_desired_size(float in_layout_scale_multiplier) const -> Eigen::Vector2f
{
	Eigen::Vector2f result;
	for (const auto& slot : get_slots())
	{
		const auto& child_widget = slot.get();
		if (!child_widget) {
			continue;
		}

		if (!has_any_flag(child_widget->get_visibility(), visibility_t::any_layout)) {
			continue;
		}

		const auto& child_desired_size = child_widget->compute_desired_size(in_layout_scale_multiplier);
		result = result.cwiseMax(child_desired_size);
	}
	return result;
}
