import { defineComponent, provide, h } from 'vue';
import { Widget, REGION_LIST } from '../../core';
import EngineView from '../views/EngineView.vue';

export default defineComponent({
  name: 'WidgetRenderer',
  props: {
    engine: Object
  },
  setup(props) {
    const { engine } = props;
    const context: Record<string, Widget[]> = {};

    REGION_LIST.map((region) => {
      context[region] = engine?.skeleton?.widgets
        .filter((n: Widget) => n.region === region)
        .sort((a: Widget, b: Widget) => (a.order || 0) - (b.order || 0));
    });

    provide('$engine', props.engine);
    provide('$region', context);

    return () => h(EngineView);
  }
});
