use freya_dom::prelude::{DioxusDOMAdapter, FreyaDOM};
use freya_engine::prelude::*;
use freya_layout::{Layers, SkiaMeasurer};
use torin::geometry::Area;
use crate::{layers::process_layers, viewports::calculate_viewports, ViewportsCollection};
pub fn process_layout(
fdom: &FreyaDOM,
area: Area,
font_collection: &mut FontCollection,
scale_factor: f32,
) -> (Layers, ViewportsCollection) {
let rdom = fdom.rdom();
let mut dom_adapter = DioxusDOMAdapter::new_with_cache(rdom);
let skia_measurer = SkiaMeasurer::new(rdom, font_collection);
fdom.layout().find_best_root(&mut dom_adapter);
let root_id = fdom.rdom().root_id();
fdom.layout()
.measure(root_id, area, &mut Some(skia_measurer), &mut dom_adapter);
let mut layers = Layers::default();
process_layers(
&mut layers,
rdom,
&fdom.layout(),
font_collection,
scale_factor,
);
let mut layers_nums: Vec<&i16> = layers.layers.keys().collect();
layers_nums.sort();
let viewports_collection = calculate_viewports(&layers_nums, &layers, fdom);
(layers, viewports_collection)
}