import type { Component } from 'vue' import { getConfigApp, isBoursecuProject } from '@/utils/config' import type { HomeLayoutConfig, HomeLayoutId } from './types' import { defaultHomeLayoutConfig } from './configs/default' import { boursecuHomeLayoutConfig } from './configs/boursecu' import HomeLayoutRenderer from './HomeLayoutRenderer.vue' const HOME_LAYOUT_CONFIGS: Record = { default: defaultHomeLayoutConfig, boursecu: boursecuHomeLayoutConfig, } const HOME_LAYOUT_COMPONENTS: Record = { default: HomeLayoutRenderer, boursecu: HomeLayoutRenderer, } export const DEFAULT_HOME_LAYOUT_ID: HomeLayoutId = 'default' export function isHomeLayoutId(value: unknown): value is HomeLayoutId { return typeof value === 'string' && value in HOME_LAYOUT_CONFIGS } export function getHomeLayoutId(): HomeLayoutId { const adminLayout = getConfigApp('home_layout') if (isHomeLayoutId(adminLayout)) { return adminLayout } if (isBoursecuProject()) { return 'boursecu' } return DEFAULT_HOME_LAYOUT_ID } export function getHomeLayoutConfig(layoutId = getHomeLayoutId()): HomeLayoutConfig { return HOME_LAYOUT_CONFIGS[layoutId] ?? HOME_LAYOUT_CONFIGS[DEFAULT_HOME_LAYOUT_ID] } export function resolveHomeLayoutComponent(layoutId = getHomeLayoutId()): Component { return HOME_LAYOUT_COMPONENTS[layoutId] ?? HOME_LAYOUT_COMPONENTS[DEFAULT_HOME_LAYOUT_ID] } export function registerHomeLayoutConfig(layoutId: HomeLayoutId, config: HomeLayoutConfig) { HOME_LAYOUT_CONFIGS[layoutId] = config }