<script lang="ts" setup>
import { enumPageComponentTemplates } from "@/definitions/enum";
import { Articles, Navigations, Collections, Sections, Categories, Advertisings, Others } from "./index";

const _props = defineProps<{
  settings: any;
  component?: any;
  content?: any;
}>();

const definedDynamicComponent: Record<string, any> = {
  [enumPageComponentTemplates.ARTICLE]: Articles,
  [enumPageComponentTemplates.NAVIGATION]: Navigations,
  [enumPageComponentTemplates.COLLECTION]: Collections,
  [enumPageComponentTemplates.SECTION]: Sections,
  [enumPageComponentTemplates.CATEGORY]: Categories,
  [enumPageComponentTemplates.ADVERTISING]: Advertisings,
  [enumPageComponentTemplates.OTHER]: Others,
};

const getCurrentComponent = computed(() => _props.component?.taxonomy);
const GET_PROPS = computed(() => {
  return () => {
    let props: any = {};
    if (_props.settings) {
      for (const [key, value] of Object.entries(_props.settings)) {
        props = {
          ...props,
          [key]: value,
        };
      }
      return props;
    }
  };
});
</script>

<template>
  <component 
    v-if="definedDynamicComponent[getCurrentComponent]"
    :is="definedDynamicComponent[getCurrentComponent]" 
    v-bind="{ ...GET_PROPS(), component: _props.component, settings: _props.settings, content: _props.content }" 
  />
</template>
